Documentation
¶
Index ¶
- Variables
- func BindingTagToContentTypes(bindingTag astTraversal.BindingTagType) []string
- func Collector(rootPath string) map[string]MethodInfo
- func Contains[S ~[]E, E comparable](s S, v E) bool
- func ContentTypeToBindingTag(contentType string) astTraversal.BindingTagType
- func ExtraMethodAndName(testString string) (string, string, error)
- func ExtractBindingTags(fields map[string]Field) (bindingTags []astTraversal.BindingTagType, uniqueBindings bool)
- func Index[S ~[]E, E comparable](s S, v E) int
- func IsAcceptedType(t string) bool
- func ParseAndStoreMethods(filename string) map[string]MethodInfo
- type BodyParam
- type CLIMode
- type Config
- type ConfigBuilder
- func (c *ConfigBuilder) Build() (*Config, error)
- func (c *ConfigBuilder) SetBasePath(basePath string) *ConfigBuilder
- func (c *ConfigBuilder) SetContact(contact Contact) *ConfigBuilder
- func (c *ConfigBuilder) SetDescription(description string) *ConfigBuilder
- func (c *ConfigBuilder) SetHost(host string) *ConfigBuilder
- func (c *ConfigBuilder) SetLicense(license License) *ConfigBuilder
- func (c *ConfigBuilder) SetPort(port int) *ConfigBuilder
- func (c *ConfigBuilder) SetSecure(secure bool) *ConfigBuilder
- func (c *ConfigBuilder) SetTitle(title string) *ConfigBuilder
- func (c *ConfigBuilder) SetVersion(version string) *ConfigBuilder
- type Contact
- type ContextFuncBuilder
- func (c *ContextFuncBuilder) Build(mapper func(*Route, []any) (*Route, error)) (*Route, error)
- func (c *ContextFuncBuilder) ExpressionResult() *ContextFuncBuilder
- func (c *ContextFuncBuilder) Ignored() *ContextFuncBuilder
- func (c *ContextFuncBuilder) StatusCode() *ContextFuncBuilder
- func (c *ContextFuncBuilder) Value() *ContextFuncBuilder
- type CustomFunc
- type Field
- type IOConfiguration
- type IOConfigurationKey
- type Input
- type InputMode
- type License
- type MethodInfo
- type Option
- func WithConfig(config *Config) Option
- func WithCustomFunc(customFunc CustomFunc) Option
- func WithCustomLogLevel(level string) Option
- func WithCustomLogger(logger zerolog.Logger) Option
- func WithCustomTypeMapping(customTypeMap map[string]TypeFormat) Option
- func WithCustomTypeMappingSingle(key string, valueType string, valueFormat string) Option
- func WithCustomWorkDir(wd string) Option
- func WithPathDenyList(denyList string) Option
- func WithPathDenyListFunc(denyList func(string) bool) Option
- func WithPathDenyListRegex(regex *regexp.Regexp) Option
- type Output
- type OutputMode
- type Param
- type Processable
- type ReturnType
- type Route
- type Service
- func (s *Service) AddRoute(route Route)
- func (s *Service) Cache() error
- func (s *Service) Clean() error
- func (s *Service) ClearCache() error
- func (s *Service) CompleteParse() error
- func (s *Service) CreateRoutes() error
- func (s *Service) Generate() error
- func (s *Service) GetMainPackageName() (string, error)
- func (s *Service) GetTypeMapping(key string, pkg string) (TypeFormat, bool)
- func (s *Service) HandleSubstituteTypes(component *Field)
- func (s *Service) LoadCache() error
- func (s *Service) LoadCacheFromCustomPath(cachePath string) error
- func (s *Service) MoveTempOutputDir(dirPath, outputDir string) error
- func (s *Service) Parse() error
- func (s *Service) ParseRoutes() error
- func (s *Service) ReplaceRoute(route Route)
- func (s *Service) SetConfig(config *Config) *Service
- func (s *Service) Setup() error
- func (s *Service) SetupParse() error
- func (s *Service) SetupTempOutputDir(dirPath string) (string, error)
- func (s *Service) Teardown() error
- type ServiceFunction
- type TypeFormat
Constants ¶
This section is empty.
Variables ¶
var ( ErrConfigNotFound = errors.New("config not found") ErrConfigPortRequired = errors.New("config port is required") ErrInputModeNotFound = errors.New("input mode not found") ErrOutputModeNotFound = errors.New("output mode not found") ErrOutputFilePathRequired = errors.New("output file path is required") ErrOutputDirectoryPathRequired = errors.New("output directory path is required") )
This file contains all the errors that can be returned by the generator.
var AcceptedTypes = []string{
"nil",
"string",
"int",
"int8",
"int16",
"int32",
"int64",
"uint",
"uint8",
"uint16",
"uint32",
"uint64",
"float",
"float32",
"float64",
"bool",
"byte",
"rune",
"struct",
"map",
"slice",
"any",
"file",
}
AcceptedTypes is a list of all accepted types for the astra package. Everything else is considered a type that has to be processed.
var PredefinedTypeMap = map[string]TypeFormat{
"string": {
Type: "string",
},
"int": {
Type: "integer",
Format: "int32",
},
"int8": {
Type: "integer",
Format: "int8",
},
"int16": {
Type: "integer",
Format: "int16",
},
"int32": {
Type: "integer",
Format: "int32",
},
"int64": {
Type: "integer",
Format: "int64",
},
"uint": {
Type: "integer",
Format: "uint",
},
"uint8": {
Type: "integer",
Format: "uint8",
},
"uint16": {
Type: "integer",
Format: "uint16",
},
"uint32": {
Type: "integer",
Format: "uint32",
},
"uint64": {
Type: "integer",
Format: "uint64",
},
"float32": {
Type: "number",
Format: "float32",
},
"float64": {
Type: "number",
Format: "float64",
},
"bool": {
Type: "boolean",
},
"byte": {
Type: "string",
Format: "byte",
},
"rune": {
Type: "string",
Format: "rune",
},
"struct": {
Type: "object",
},
"map": {
Type: "object",
},
"slice": {
Type: "array",
},
"any": {
Type: "",
},
"nil": {
Type: "",
},
"time.Time": {
Type: "string",
Format: "date-time",
},
"github.com/google/uuid.UUID": {
Type: "string",
Format: "uuid",
},
"file": {
Type: "string",
Format: "binary",
},
}
PredefinedTypeMap is the map of the standard go types that are accepted by OpenAPI. It contains the go type as a string and the corresponding OpenAPI type as the value - also including the format.
Functions ¶
func BindingTagToContentTypes ¶
func BindingTagToContentTypes(bindingTag astTraversal.BindingTagType) []string
func Collector ¶ added in v1.23.4
func Collector(rootPath string) map[string]MethodInfo
func Contains ¶
func Contains[S ~[]E, E comparable](s S, v E) bool
func ContentTypeToBindingTag ¶
func ContentTypeToBindingTag(contentType string) astTraversal.BindingTagType
func ExtraMethodAndName ¶ added in v1.23.4
func ExtractBindingTags ¶
func ExtractBindingTags(fields map[string]Field) (bindingTags []astTraversal.BindingTagType, uniqueBindings bool)
func Index ¶
func Index[S ~[]E, E comparable](s S, v E) int
func IsAcceptedType ¶
func ParseAndStoreMethods ¶ added in v1.23.4
func ParseAndStoreMethods(filename string) map[string]MethodInfo
Types ¶
type BodyParam ¶
type BodyParam struct { Name string `json:"name,omitempty" yaml:"name,omitempty"` Field Field `json:"type,omitempty" yaml:"type,omitempty"` ContentType string `json:"contentType,omitempty" yaml:"contentType,omitempty"` IsRequired bool `json:"isRequired,omitempty" yaml:"isRequired,omitempty"` IsArray bool `json:"isArray,omitempty" yaml:"isArray,omitempty"` IsMap bool `json:"isMap,omitempty" yaml:"isMap,omitempty"` IsBound bool `json:"isBound,omitempty" yaml:"isBound,omitempty"` // I.e. is a struct reference }
type CLIMode ¶
type CLIMode string
CLIMode is the mode the CLI is running in.
const ( CLIModeNone CLIMode = "" // Not running in CLI mode CLIModeSetup CLIMode = "setup" // Running in setup mode - used in the project code to setup the routes CLIModeBuilder CLIMode = "builder" // Running in builder mode - used in the project code to build the routes and generate the types (this is not needed to be used by other developers, the CLI will use this mode) )
type Config ¶
type Config struct { Title string `json:"title"` Description string `json:"description"` Version string `json:"version"` Contact Contact `json:"contact"` License License `json:"license"` Secure bool `json:"secure"` Host string `json:"host"` BasePath string `json:"basePath"` Port int `json:"port"` }
Config is the configuration for the generator. It matches very closely to the OpenAPI specification.
type ConfigBuilder ¶
type ConfigBuilder struct {
// contains filtered or unexported fields
}
func NewConfigBuilder ¶
func NewConfigBuilder() *ConfigBuilder
func (*ConfigBuilder) Build ¶
func (c *ConfigBuilder) Build() (*Config, error)
func (*ConfigBuilder) SetBasePath ¶
func (c *ConfigBuilder) SetBasePath(basePath string) *ConfigBuilder
func (*ConfigBuilder) SetContact ¶
func (c *ConfigBuilder) SetContact(contact Contact) *ConfigBuilder
func (*ConfigBuilder) SetDescription ¶
func (c *ConfigBuilder) SetDescription(description string) *ConfigBuilder
func (*ConfigBuilder) SetHost ¶
func (c *ConfigBuilder) SetHost(host string) *ConfigBuilder
func (*ConfigBuilder) SetLicense ¶
func (c *ConfigBuilder) SetLicense(license License) *ConfigBuilder
func (*ConfigBuilder) SetPort ¶
func (c *ConfigBuilder) SetPort(port int) *ConfigBuilder
func (*ConfigBuilder) SetSecure ¶
func (c *ConfigBuilder) SetSecure(secure bool) *ConfigBuilder
func (*ConfigBuilder) SetTitle ¶
func (c *ConfigBuilder) SetTitle(title string) *ConfigBuilder
func (*ConfigBuilder) SetVersion ¶
func (c *ConfigBuilder) SetVersion(version string) *ConfigBuilder
type ContextFuncBuilder ¶
type ContextFuncBuilder struct { Route *Route Traverser *astTraversal.CallExpressionTraverser // contains filtered or unexported fields }
func NewContextFuncBuilder ¶
func NewContextFuncBuilder(route *Route, traverser *astTraversal.CallExpressionTraverser) *ContextFuncBuilder
func (*ContextFuncBuilder) ExpressionResult ¶
func (c *ContextFuncBuilder) ExpressionResult() *ContextFuncBuilder
func (*ContextFuncBuilder) Ignored ¶
func (c *ContextFuncBuilder) Ignored() *ContextFuncBuilder
func (*ContextFuncBuilder) StatusCode ¶
func (c *ContextFuncBuilder) StatusCode() *ContextFuncBuilder
func (*ContextFuncBuilder) Value ¶
func (c *ContextFuncBuilder) Value() *ContextFuncBuilder
type CustomFunc ¶
type CustomFunc func(contextVarName string, contextFuncBuilder *ContextFuncBuilder) (*Route, error)
type Field ¶
type Field struct { Package string `json:"package,omitempty" yaml:"package,omitempty"` Type string `json:"type,omitempty" yaml:"type,omitempty"` Name string `json:"name,omitempty" yaml:"name,omitempty"` EnumValues []any `json:"enumValues,omitempty" yaml:"enumValues,omitempty"` IsRequired bool `json:"isRequired,omitempty" yaml:"isRequired,omitempty"` IsEmbedded bool `json:"isEmbedded,omitempty" yaml:"isEmbedded,omitempty"` SliceType string `json:"sliceType,omitempty" yaml:"sliceType,omitempty"` ArrayType string `json:"arrayType,omitempty" yaml:"arrayType,omitempty"` ArrayLength int64 `json:"arrayLength,omitempty" yaml:"arrayLength,omitempty"` MapKeyPackage string `json:"mapKeyPackage,omitempty" yaml:"mapKeyPackage,omitempty"` MapKeyType string `json:"mapKeyType,omitempty" yaml:"mapKeyType,omitempty"` MapValueType string `json:"mapValueType,omitempty" yaml:"mapValueType,omitempty"` StructFields map[string]Field `json:"structFields,omitempty" yaml:"structFields,omitempty"` StructFieldBindingTags astTraversal.BindingTagMap `json:"structFieldBindingTags,omitempty" yaml:"structFieldBindingTags,omitempty"` StructFieldValidationTags astTraversal.ValidationTagMap `json:"structFieldValidationTags,omitempty" yaml:"structFieldValidationTags,omitempty"` Doc string `json:"doc,omitempty" yaml:"doc,omitempty"` }
Field is a field in a struct. It contains the package, type, and name of the field (the type is slice, map or struct in the case of a slice, map or struct). It also contains whether the field is required and whether it is embedded. If the field is a slice, it contains the type of the slice (package is the package of the type). If the field is a map, it contains the key and value types of the map (and the key package, we treat the package as the value package). If the field is a struct, it contains the fields of the struct.
func AddComponent ¶
AddComponent adds a component to a slice of components if it doesn't already exist. It uses the field type and package to determine if the component already exists.
func ParseResultToField ¶
func ParseResultToField(result astTraversal.Result) Field
ParseResultToField changes a result from the AST traversal to a local field.
type IOConfiguration ¶
type IOConfiguration map[IOConfigurationKey]any
type IOConfigurationKey ¶
type IOConfigurationKey string
const ( IOConfigurationKeyFilePath IOConfigurationKey = "filePath" IOConfigurationKeyDirectoryPath IOConfigurationKey = "directoryPath" )
type Input ¶
type Input struct { Mode InputMode `json:"mode"` CreateRoutes ServiceFunction `json:"-"` ParseRoutes ServiceFunction `json:"-"` }
Input is the input for the generator.
type MethodInfo ¶ added in v1.23.4
type MethodInfo struct { MethodName string FileName string Line int MethodReceiver string PkgName string }
func (*MethodInfo) Id ¶ added in v1.23.9
func (m *MethodInfo) Id() string
type Option ¶
type Option func(*Service)
Option is a function that can be used to configure the generator.
func WithConfig ¶
WithConfig sets the configuration for the generator in option pattern.
func WithCustomFunc ¶
func WithCustomFunc(customFunc CustomFunc) Option
func WithCustomLogLevel ¶
WithCustomLogLevel enables the custom log level for the generator. This allows for easier debugging and controlling of logs.
func WithCustomLogger ¶
WithCustomLogger enables the custom logger for the generator. It needs to pass in a zerolog.Logger.
func WithCustomTypeMapping ¶
func WithCustomTypeMapping(customTypeMap map[string]TypeFormat) Option
WithCustomTypeMapping adds a custom type mapping to the predefined type map.
func WithCustomTypeMappingSingle ¶
WithCustomTypeMappingSingle adds a custom type mapping to the predefined type map.
func WithCustomWorkDir ¶
WithCustomWorkDir is an option to set the working directory of the service to a custom directory.
func WithPathDenyList ¶
func WithPathDenyListFunc ¶
func WithPathDenyListRegex ¶
type Output ¶
type Output struct { Mode OutputMode `json:"mode"` Generate ServiceFunction `json:"-"` Configuration IOConfiguration `json:"configuration"` }
Output is the output for the generator It takes in a configuration object to allow the caching mechanism to store the file paths for the outputs.
type OutputMode ¶
type OutputMode string
type Param ¶
type Param struct { Name string `json:"name,omitempty" yaml:"name,omitempty"` Field Field `json:"type,omitempty" yaml:"type,omitempty"` IsRequired bool `json:"isRequired,omitempty" yaml:"isRequired,omitempty"` IsArray bool `json:"isArray,omitempty" yaml:"isArray,omitempty"` IsMap bool `json:"isMap,omitempty" yaml:"isMap,omitempty"` IsBound bool `json:"isBound,omitempty" yaml:"isBound,omitempty"` // I.e. is a struct reference. }
Param is a parameter for a route. It contains the name, type, and whether it is required. It also contains an IsBound field, which is used to denote whether the param is a struct reference.
type Processable ¶
Processable is a struct that is processable by the astra package. It just contains the name of the type and the package it came from.
type ReturnType ¶
type ReturnType struct { StatusCode int `json:"statusCode,omitempty" yaml:"statusCode,omitempty"` ContentType string `json:"contentType,omitempty" yaml:"contentType,omitempty"` Field Field `json:"field,omitempty" yaml:"field,omitempty"` }
ReturnType is a return type for a route. It contains the status code and the field that is returned.
func AddReturnType ¶
func AddReturnType(prev []ReturnType, n ...ReturnType) []ReturnType
AddReturnType adds a return type to a slice of return types if it doesn't already exist. It uses the field type, package and status code to determine if the return type already exists.
type Route ¶
type Route struct { Handler string `json:"handler" yaml:"handler"` File string `json:"file" yaml:"file"` LineNo int `json:"lineNo" yaml:"lineNo"` Method string `json:"method" yaml:"method"` Path string `json:"path" yaml:"path"` PathParams []Param `json:"params,omitempty" yaml:"params,omitempty"` // for now, we use :param in the path to denote a required path param, and *param to denote an optional path param. QueryParams []Param `json:"queryParams,omitempty" yaml:"queryParams,omitempty"` Body []BodyParam `json:"body,omitempty" yaml:"body,omitempty"` ReturnTypes []ReturnType `json:"returnTypes,omitempty" yaml:"returnTypes,omitempty"` Doc string `json:"doc,omitempty" yaml:"doc,omitempty"` OperationID string `json:"operationId,omitempty" yaml:"operationId,omitempty"` RequestHeaders []Param `json:"requestHeaders,omitempty" yaml:"requestHeaders,omitempty"` ResponseHeaders []Param `json:"responseHeaders,omitempty" yaml:"responseHeaders,omitempty"` }
Route is a route in the service and all of its potential options.
type Service ¶
type Service struct { Inputs []Input `json:"inputs" yaml:"inputs"` Outputs []Output `json:"outputs" yaml:"outputs"` Log zerolog.Logger `json:"-"` Config *Config `json:"config" yaml:"config"` Routes []Route `json:"routes" yaml:"routes"` Components []Field `json:"components" yaml:"components"` AstMethodInfo map[string]MethodInfo WorkDir string `json:"-" yaml:"-"` CacheEnabled bool `json:"-"` CachePath string `json:"-"` CLIMode CLIMode `json:"-"` PathDenyList []func(string) bool `json:"-" yaml:"-"` CustomFuncs []CustomFunc `json:"-" yaml:"-"` // CustomTypeMapping is a map of custom types to their OpenAPI type and format CustomTypeMapping map[string]TypeFormat `json:"custom_type_mapping" yaml:"custom_type_mapping"` // contains filtered or unexported fields }
Service is the main struct for the generator.
func New ¶
New creates a new generator service. It takes in a list of options that can be used to configure the generator. It will also setup the logger for the generator and setup the slices that are used to store the routes, inputs, outputs and components.
func (*Service) Clean ¶
Clean cleans up the structs. At the moment it only changes the package name of the main package to "main". It also handles the "special" types. It also caches the service after cleaning.
func (*Service) CompleteParse ¶
CompleteParse completes the parse by calling the ParseRoutes function, Process function, Clean function and Generate function. It will parse the routes from the files identified, process the found definitions, clean up the structs and generate the outputs. CompleteParse should be called after SetupParse.
func (*Service) CreateRoutes ¶
CreateRoutes creates routes from the inputs.
func (*Service) GetMainPackageName ¶
GetMainPackageName returns the name of the temporary main package.
func (*Service) GetTypeMapping ¶
func (s *Service) GetTypeMapping(key string, pkg string) (TypeFormat, bool)
GetTypeMapping returns the type mapping for the given key.
func (*Service) HandleSubstituteTypes ¶
HandleSubstituteTypes handles substitute types.
func (*Service) LoadCache ¶
LoadCache Load the service from a file cache. If the file does not exist, it will not return an error.
func (*Service) LoadCacheFromCustomPath ¶
LoadCacheFromCustomPath Load the service from a file cache. If the file does not exist, it will return an error. Requires the path to the cache file.
func (*Service) MoveTempOutputDir ¶
func (*Service) Parse ¶
Parse parses the inputs and generates the outputs. It will call the SetupParse function and then the CompleteParse function. It should be used if you don't intend to use the CLI.
func (*Service) ParseRoutes ¶
ParseRoutes iterates over the inputs and parses the routes from them. CreateRoutes should be called before ParseRoutes.
func (*Service) ReplaceRoute ¶
ReplaceRoute replaces a route in the service using the path and method as indexes.
func (*Service) Setup ¶
Setup sets up the service by creating the temp dir and caching. Setup should be called before anything else in the service.
func (*Service) SetupParse ¶
SetupParse sets up the parse by calling the Setup function and then the CreateRoutes function. It will create the routes from the inputs but not parse them. SetupParse should be called before CompleteParse. SetupParse should be used if you are using the CLI or if you want to parse the routes yourself.
func (*Service) SetupTempOutputDir ¶
type ServiceFunction ¶
ServiceFunction is the function used by the inputs/outputs to interact with the service.
type TypeFormat ¶
TypeFormat is the types of the standard go types that are accepted by OpenAPI.
Source Files
¶
- accepted.go
- bindingTags.go
- cache.go
- clean.go
- collector.go
- config.go
- configBuilder.go
- contextFuncBuilder.go
- createRoutes.go
- dir.go
- errors.go
- formatTypes.go
- generate.go
- input.go
- io.go
- listing.go
- logger.go
- mainPackage.go
- new.go
- options.go
- output.go
- parse.go
- parseRoutes.go
- routes.go
- safePushing.go
- service.go
- setup.go
- substituteTypes.go
- teardown.go
- types.go
- utils.go