Package godocgen
import "gitlab.com/zoralab/godocgen"
Package godocgen is to generate Go module package's documentations.
Its primary intension was to restore the ability to host the documents via a web server for Go Modules packages using other third-party hosting solutions. With Godocgen, one can generate the web materials (e.g. Markdown or HTML) and freely use his/her own hosting solution to publish the module's documentations. Long story short, Godocgen facilitates freedom to developer while not compromising one of Go's beauty of auto-documentation feature.
MINIMUM VERSIONS
Godocgen should be used with Go 1.14.1
and above and it's not backwards
compatible (Reason: Example rendering API only available starting from
Go 1.14.1
).
Constants
App constants are meant for configuring godocgen to work in a specific manner
const (
// VERSION is the version number godocgen is current are.
VERSION = "0.0.1"
// DefaultFilename is the default filename in the event
// where filename is left out when it is needed.
DefaultFilename = "index.txt"
)
Supported file extension formats
const (
// Markdown file extension
Markdown = ".md"
// Terminal
Terminal = "terminal"
// Text file extension
Text = ".txt"
)
Working modes
const (
// AppMode is to set the app to operate as normal app mode.
// It allows one not only to generate the package
// documentation data but also rendering the output.
AppMode = uint(0)
// MockMode is the set the app to operate as a mocking
// medium. This is useful for developers to unit-test his/her
// own packages without needing to write too many mock codes
// when using godocgen as `LibraryMode`. MockMode will make
// `App` exits immediately after initialized, allowing
// developer to mock the elements inside it.
MockMode = uint(1)
// LibraryMode is the set the app to operate as a Go package
// library. This allows developer to import godocgen to
// generate the packages' documentations data without needing
// to render them out. That being said, LibraryMode will not
// run rendering at all.
LibraryMode = uint(2)
)
Error constants are the values used for error identifications via application programming.
const (
// ErrorTag is the error message tag for printing out error
// messages.
ErrorTag = "ERROR"
// WarningTag is the warning message tag for printing out
// less critical error messages.
WarningTag = "WARNING"
// ErrorCodeNone is the error code that has no errors.
ErrorCodeNone = 0
// ErrorCodeCommon is the error code for common errors.
ErrorCodeCommon = 1
)
Error messages are the string values used for error object text output.
const (
// ErrorBadInputPath is the error message for a completely
// bad and unusable input path.
ErrorBadInputPath = "bad input path"
// ErrorBadOutputPath is the error message for a completely
// bad and unusable output path.
ErrorBadOutputPath = "bad output path"
// ErrorBadTemplatePath is the error message for a
// completely bad and unusable template path.
ErrorBadTemplatePath = "bad template path"
// ErrorConfigureDocData is the error message for unable to
// configure package's raw DocData
ErrorConfigureDocData = "unable to configure raw DocData"
// ErrorMissingInputPath is the error message for completely
// missing input path.
ErrorMissingInputPath = "missing input path"
// ErrorMissingOutputPath is the error message for
// completely missing output path.
ErrorMissingOutputPath = "missing output path"
// ErrorMissingTemplatePath is the error message for
// completely missing template path.
ErrorMissingTemplatePath = "missing template path"
// ErrorInputPathNotDirectory is the error message for the
// given input path is not a directory.
ErrorInputPathNotDirectory = "given input path is not a directory"
// ErrorInputPathWithoutGo is the error message for the
// given input path does not contain any directory.
ErrorInputPathWithoutGo = "given input path has no go packages"
// ErrorOutputPathIsAFile is the error message for the given
// output path is a file and is not ready for output usage.
ErrorOutputPathIsAFile = "output path is a file"
// ErrorOutputPermission is the error message for unable to
// read/write into output.
ErrorOutputPermission = "unable to read/write to output"
// ErrorTemplatePathIsADir is the error message for the
// given output path is a directory and is not a file for
// template rendering.
ErrorTemplatePathIsADir = "template path is a directory"
// ErrorInTemplate is the error message for a given template
// having its internal errors.
ErrorInTemplate = "template is having error"
// ErrorUnknownOutputFormat is the error message for
// unidentifiable filetype based on the given filename.
ErrorUnknownOutputFormat = "unknown filetype from the given filename"
// ErrorUnknownWorkMode is the error message for
// unidentifiable work mode.
ErrorUnknownWorkMode = "unknown work mode"
)
App
type App struct {
// user input
InputPath string
OutputPath string
Filename string
TemplatePath string
WorkMode uint
Width uint
// generated output
Targets []*Data
Log []string
ExitCode int
// contains filtered or unexported fields
}
App is the master control for godocgen go package.
It has private variables that require initialization. Therefore, to create
one safely, please use NewApp()
function.
Before calling its App.Run()
function, you need to ensure the following
elements are fulfilled.
COMPULSORY elements:
OutputPath
- location for storing the generated output (directory).InputPath
- location for reading the go packages (directory). Append...
in the end for recursive generations.WorkMode
- how App should works such as: as a test mocking unit, package library, or as app mode.
OPTIONAL elements:
TemplatePath
- location to the template file for output rendering.Filename
- output filename with extension. If this is set,TemplatePath
must be specified.Width
- to specify the maximum width of the document. Default is80
for terminal and70
for files.
For OUTPUT, depending on App.WorkMode
, they are different. For example,
- in
godocgen.MockMode
, nothing is executed.App
exited immediately. - in
godocgen.LibraryMode
, the output, system log, and exit code are stored insideApp.Targets
,App.Log
, andApp.Exit
respectively. Rendering process is not executed in this mode, allowing one to useApp.Targets
Data
list for his/her own Go rendering. - in
godocgen.AppMode
, it will run the full-fletch executions including rendering.
Func NewApp() *App
NewApp is the function for creating the godocgen App structure object.
It returns the memory pointer of the created App object. Since App structure contains uninitialized private elements, it is best to use this function to create App object.
Func (c *App) Run()
Run is the function for starting the godocgen process.
It executes the processing accordingly based on its WorkingMode. For AppMode, the rendering is inclusive and will be rendered automatically.
Data
type Data struct {
Header string
ImportPath string
Synopsis string
RelativePath string
Filepath string
Codes []string
Examples *DataElement
Constants *DataElement
Variables *DataElement
Functions *DataElement
Methods *DataElement
Types []*Data
}
Data is the data structure holding Package
or Type
level.
This structure contains elements that needs initialization. Hence, to be
on the safe side, you should use NewData()
function if you ever need to
create one.
For Package
subject, the elements’ representations are as follow:
Header
- name of the packageImportPath
- the import path statementSynopsis
- the descriptions of the packageRelativePath
- the relative path of the package to its root directoryFilepath
- the output filepath (for file rendering)Codes
- not applicable: eithernil
or emptyExamples
- the element holding theList
of examplesConstants
- the element holding theList
of constantsVariables
- the element holding theList
of variablesFunctions
- the element holding theList
of functionsMethods
- not applicable: eithernil
or emptyTypes
- array ofData
holdingType
details
For Type
subject, the elements’ representations are as follow:
Header
- name of theType
ImportPath
- not applicable: it is always emptySynopsis
- the descriptions of theType
RelativePath
- not applicable: it is always emptyFilepath
- not applicable: it is always emptyCodes
- Codes in multiple lineExamples
- the element holding theList
of examplesConstants
- the element holding theList
of constantsVariables
- the element holding theList
of variablesFunctions
- the element holding theList
of functionsMethods
- the element holding theList
of functions with receiverTypes
- not applicable: eithernil
or empty
Func NewData() *Data
NewData creates a new Data object and return its pointer as output.
This is the recommended function to create new *Data structure type over the
conventional &Data{...}
method.
Unless you're using App as LibraryMode for development or testing, you do
not need to use this function since App
will generate the Data
objects
automatically. This function was meant to facilitate Developers to create
mocking data for unit testing.
DataElement
type DataElement struct {
Header string
Synopsis string
Receiver string
Value string
Codes []string
Examples []*DataElement
List []*DataElement
}
DataElement is the data structure holding specific data values for Data
.
The structure contains elements that require initialization. Hence, it is
not safe to create using struct{}
method.
Depending on its associated relationship with its Data
structure,
DataElement can be represented in many ways, including towards itself.
For Examples
association from Data
:
DataElement
'sExamples
holds all theData
's examples in itsList
, which is called Examples Group.- For each elements in Examples Group‘s
List
, it is called Example Elements.
EXAMPLES GROUP
Header
- not applicable: it is emptyReceiver
- not applicable: it is emptySynopsis
- the synopsis of the examplesValue
- not applicable: it is emptyCodes
- not applicable: eithernil
or emptyExamples
- list of Example Elements.List
- not applicable: eithernil
or empty
EXAMPLES ELEMENTS
Header
- suffix of the example (_second
,_third
, etc.)Receiver
- name of the example with trimmed suffixSynopsis
- the descriptions of the exampleValue
- output value of the exampleCodes
- the lines of codes describing the example (trimmed newline and tab characters)Examples
- not applicable: eithernil
or emptyList
- not applicable: eithernil
or empty
For Constants
and Variables
association from Data
:
DataElement
'sList
holds all theData
's constants/variables group in a list ofDataElements
called Constants/Variables Group.- In each “constant/variable group”, its
List
has all the constants/variables called Constant/Variable Elements.
CONSTANT/VARIABLES GROUP
Header
- group type in code form. Eitherconst
orvar
Receiver
- not applicable: it is emptySynopsis
- the description of the example groupValue
- not applicable: it is emptyCodes
- not applicable: eithernil
or emptyExamples
- not applicable: eithernil
or emptyList
- list of Constant/variable elements
CONSTANT/VARIABLE ELEMENTS
Header
- suffix of the example (_second
,_third
, etc.)Receiver
- name of the example with trimmed suffixSynopsis
- the descriptions of the exampleValue
- output value of the exampleCodes
- the lines of codes describing the example (trimmed newline and tab characters)Examples
- not applicable: eithernil
or emptyList
- not applicable: eithernil
or empty
For Functions
and Methods
association from Data
:
- The
Functions
/Methods
holds all its functions/methods in itsList
called Functions/Methods Group. - For each elements in Functions/Methods Group, it holds the data for the function/method called Function/Method Elements.
FUNCTIONS/METHODS GROUP
Header
- not applicable: it is emptyReceiver
- not applicable: it is emptySynopsis
- the description of the functions/methods groupValue
- not applicable: it is emptyCodes
- not applicable: eithernil
or emptyExamples
- not applicable: eithernil
or emptyList
- list of Function Elements
FUNCTION/METHOD ELEMENTS
Header
- synopsis of the functionsReceiver
- Only applicable formethod
which is function receiver typeSynopsis
- not applicable: it is emptyValue
- function name withReceiver
if exists, withoutfunc
keyword)Codes
- not applicable: eithernil
or emptyExamples
- list of associated examplesList
- not applicable: eithernil
or empty