Functions
Mr. Smith imports all the available functions in the go-sproute package, and also includes a series of its own functions that will be described in this document.
Parser Functions
toYaml
Converts a Go data structure into a slice of strings formatted as YAML, where each element represents a line of the resulting YAML. This function is useful for rendering data in a format readable for templates.
Signature:
Parameters:
data
: The data structure to be converted to YAML.
Returns:
A slice of strings containing the YAML representation of the data.
Example
Grouping Functions
groupByKey
Organizes a slice of maps based on a specified key. It returns a map where the keys are unique values found in the specified key in the input items, and the values are slices of items that share that key value.
Signature:
Parameters:
data
: A slice of maps where each map represents an item with key-value pairs.key
: The key used to group items in the data slice.
Returns:
A map where each key represents a unique value of the specified key in the data slice, and the corresponding value is a slice of items that have that key value.
Example
OpenAPI Functions
getRefFrom
Extracts the reference identifier from an OpenAPI 2.0 object if it exists. The function expects the reference to be in the form of a JSON pointer within the OpenAPI specification.
Signature:
Parameters:
object
: A map representing an OpenAPI object that may contain a $ref field.
Returns:
A string containing the reference identifier or an empty string if no valid reference is found.
Example
getPathsByTag
Filters OpenAPI paths by a specified tag. It returns a subset of paths that are associated with the provided tag, useful for generating documentation for specific sections of an API.
Signature:
Parameters:
paths
: A map where the keys are path names and the values are path item objects.tag
: The tag used to filter paths.
Returns:
A map containing the paths that include the specified tag.
Example
getParamsByType
Filters parameters based on the type of the in
field (e.g., query, header, path, formData). This function helps extract parameters of a specific type from an OpenAPI operation.
Signature:
Parameters:
params
: A slice of parameter objects.paramType
: The type of parameters to filter (e.g., “query”).
Returns:
A slice of parameters that match the specified type.
Example
GetAllTags
GetAllTags extracts all unique tags from OpenAPI paths. It processes each operation within each path to collect tags and returns them as a slice of strings.
Signature:
Parameters:
paths
: A map where keys are path names and values are path item objects.
Returns:
A slice of strings containing all unique tags found in the paths.
Example
GetRefsList
GetRefsList extracts all unique reference identifiers from an OpenAPI 2.0 paths object. It recursively traverses the paths object and collects all $ref values, removing duplicates.
Signature:
Parameters:
paths
: A map representing OpenAPI paths and their operations
Returns:
A slice of unique reference identifiers found in the paths object.
Example
File Functions
getFileContent
Reads and returns the content of a file provided as a string. It is useful for inserting the content of files into Go templates.
Signature:
Parameters:
filePath
: The full path of the file to be read.
Returns:
- The content of the file as a string.
- An empty string if an error occurs.
Example
getFilesList
Returns a list of file names in a directory. It can be used to list files in a directory within a Go template.
Signature:
Parameters:
path
: The path of the directory where the files will be listed.
Returns:
- A slice of strings containing the names of the files in the directory.
- An empty slice if an error occurs.
Example
getFileExtension
Returns the extension of a file from its path. Useful for conditioning template logic based on the file type.
Signature:
Parameters:
filePath
: The full path of the file.
Returns:
- The file extension as a string.
- An empty string if the file has no extension.
Example
getFileName
Returns the file name, excluding the path. It can be used to display only the file name in templates.
Signature:
Parameters:
filePath
: The full path of the file.
Returns:
- The file name as a string.
Example
getFilePath
Returns the directory where the file is located. It can be used to access the directory path within a Go template.
Signature:
Parameters:
filePath
: The full path of the file.
Returns:
- The directory path as a string.
Example
fileExists
Checks if a file exists in the filesystem. It is useful for verifying the existence of files before using them in Go templates.
Signature:
Parameters:
filePath
: The full path of the file.
Returns:
true
if the file exists,false
otherwise.
Example
isDirectory
Checks if a path is a directory. It can be used to differentiate directories from files in a Go template.
Signature:
Parameters:
filePath
: The full path to be checked.
Returns:
true
if the path is a directory,false
otherwise.
Example
isFile
Checks if a path is a file. It can be used to differentiate files from directories in a Go template.
Signature:
Parameters:
filePath
: The full path to be checked.
Returns:
true
if the path is a file,false
otherwise.
Example
getFileSize
Returns the size of a file in bytes.
Signature:
Parameters:
filePath
: The full path of the file.
Returns:
- The size of the file in bytes.
0
if an error occurs.
Example