Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrFunctionNotFound = errors.New("function not found")
Functions ¶
This section is empty.
Types ¶
type Definition ¶
type Definition struct {
// Type specifies the data type of the schema.
Type DataType `json:"type,omitempty"`
// Description is the description of the schema.
Description string `json:"description,omitempty"`
// Enum is used to restrict a value to a fixed set of values. It must be an array with at least
// one element, where each element is unique. You will probably only use this with strings.
Enum []string `json:"enum,omitempty"`
// Properties describes the properties of an object, if the schema type is Object.
Properties map[string]*Definition `json:"properties,omitempty"`
// Required specifies which properties are required, if the schema type is Object.
Required []string `json:"required,omitempty"`
// Items specifies which data type an array contains, if the schema type is Array.
Items *Definition `json:"items,omitempty"`
}
Definition holds the name and type of a function parameter.
type FunctionDetails ¶
type FunctionDetails struct {
// Name of the function.
Name string `json:"name"`
// Description of the function.
Description string `json:"description"`
// Parameters of the function.
Parameters *Definition `json:"parameters"`
}
FunctionDetails describes a Go function.
func ParseFunction ¶
func ParseFunction(filePath string, funcName string) (*FunctionDetails, error)
ParseFunction parses the Go source code for a specific function.
Example ¶
package main
import (
"encoding/json"
"log"
"os"
"github.com/tmc/go2oapi"
)
func main() {
// Assuming we have a file named 'example.go' in the current directory
// with a function 'HelloWorld' that we want to parse.
filePath := "testdata/sample-a"
functionName := "NewWidgetFactory"
// Parse the function to get the details
funcDetails, err := go2oapi.ParseFunction(filePath, functionName)
if err != nil {
log.Fatalf("Error parsing function: %v\n", err)
}
// Output the function details
// (In a real test, this would be used to validate the output against expected results)
json.NewEncoder(os.Stdout).Encode(funcDetails)
}
Output: {"name":"NewWidgetFactory","description":"NewWidgetFactory creates a new widget factory.","parameters":{"type":"object","properties":{"Category":{"type":"string","description":"Category","enum":["foo","bar"]},"FactoryName":{"type":"string","description":"The name of the factory"},"InventoryLevels":{"type":"array","description":"InventoryLevels","items":{"type":"integer"}},"Operational":{"type":"boolean"}},"required":["FactoryName","Category","InventoryLevels","Operational"]}}
Click to show internal directories.
Click to hide internal directories.