gen

package
v0.8.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2024 License: Apache-2.0 Imports: 29 Imported by: 3

Documentation

Index

Constants

View Source
const (
	ExtensionKclType        = "x-kcl-type"
	ExtensionKclDecorators  = "x-kcl-decorators"
	ExtensionKclUnionTypes  = "x-kcl-union-types"
	ExtensionKclDictKeyType = "x-kcl-dict-key-type"
)

Variables

This section is empty.

Functions

func ExportOpenAPITypeToSchema added in v0.7.2

func ExportOpenAPITypeToSchema(ty *KclOpenAPIType) *openapi3.SchemaRef

ExportOpenAPITypeToSchema exports open api v3 schema ref from the kcl type.

func ExportOpenAPIV3Spec added in v0.7.2

func ExportOpenAPIV3Spec(pkgPath string) (*openapi3.T, error)

ExportOpenAPIV3Spec exports open api v3 spec of a kcl package

func ExportSwaggerV2SpecString added in v0.7.2

func ExportSwaggerV2SpecString(pkgPath string) (string, error)

ExportSwaggerV2SpecString exports swagger v2 spec of a kcl package

func GenGo

func GenGo(w io.Writer, filename string, src interface{}, opts *GenGoOptions) error

GenGo translate kcl schema type to go struct.

func GenKcl

func GenKcl(w io.Writer, filename string, src interface{}, opts *GenKclOptions) error

GenKcl translate other formats to kcl schema code. Now support go struct and json schema.

func GenProto

func GenProto(filename string, src interface{}, opt *Options) (string, error)

GenProto translate kcl schema type to protobuf message.

func GetPkgDir added in v0.5.6

func GetPkgDir(base string, pkgName string) string

func IsLitType added in v0.5.4

func IsLitType(typ *pb.KclType) (ok bool, basicTyp, litValue string)

func PackageName added in v0.5.6

func PackageName(pkgPath string, t *kcl.KclType) string

PackageName resolves the package name from the PkgPath and the PkgRoot of the type

func Ref2SchemaId added in v0.7.0

func Ref2SchemaId(ref string) string

func SchemaId added in v0.5.4

func SchemaId(pkgPath string, t *kcl.KclType) string

func SchemaId2Ref added in v0.7.0

func SchemaId2Ref(id string) string

func SwaggerV2ToOpenAPIV3Spec added in v0.8.2

func SwaggerV2ToOpenAPIV3Spec(s *SwaggerV2Spec) *openapi3.T

SwaggerV2ToOpenAPIV3Spec converts swagger v2 spec to open api v3 spec.

Types

type Format added in v0.5.4

type Format string
const (
	Html     Format = "html"
	Markdown Format = "md"
	OpenAPI  Format = "openapi"
)

type GenContext added in v0.5.4

type GenContext struct {
	// PackagePath is the package path to the package or module to generate docs for
	PackagePath string
	// Format is the doc format to output
	Format Format
	// Target is the target directory to output the docs
	Target string
	// IgnoreDeprecated defines whether to generate documentation for deprecated schemas
	IgnoreDeprecated bool
	// EscapeHtml defines whether to escape html symbols when the output format is markdown
	EscapeHtml bool
	// SchemaDocTmpl defines the content of the schemaDoc template
	SchemaDocTmpl string
	// PackageDocTmpl defines the content of the packageDoc template
	PackageDocTmpl string
	// SchemaListDocTmpl defines the content of the schemaListDoc template
	SchemaListDocTmpl string
	// Template is the doc render template
	Template *template.Template
}

GenContext defines the context during the generation

func (*GenContext) GenDoc added in v0.5.4

func (g *GenContext) GenDoc() error

GenDoc generate document files from KCL source files

type GenGoOptions

type GenGoOptions struct {
	Package  string
	AnyType  string
	UseValue bool
}

type GenKclOptions

type GenKclOptions struct {
	Mode         Mode
	ParseFromTag bool
}

type GenOpts added in v0.5.4

type GenOpts struct {
	// Path is the path to the directory or file to generate docs for
	Path string
	// Format is the doc format to output
	Format string
	// Target is the target directory to output the docs
	Target string
	// IgnoreDeprecated defines whether to generate documentation for deprecated schemas
	IgnoreDeprecated bool
	// EscapeHtml defines whether to escape html symbols when the output format is markdown
	EscapeHtml bool
	// TemplateDir defines the relative path from the package root to the template directory
	TemplateDir string
}

GenOpts is the user interface defines the doc generate options

func (*GenOpts) ValidateComplete added in v0.5.4

func (opts *GenOpts) ValidateComplete() (*GenContext, error)

type Generator

type Generator interface {
	GenFromSource(w io.Writer, filename string, src interface{}) error
	GenFromTypes(w io.Writer, types ...*pb.KclType)
	GenSchema(w io.Writer, typ *pb.KclType)
	GetTypeName(typ *pb.KclType) string
}

type GoStruct

type GoStruct struct {
	Name          string
	Fields        []*GoStructField
	FieldNum      int
	StructComment string
}

func ParseGoSourceCode

func ParseGoSourceCode(filename string, src interface{}) ([]*GoStruct, error)

ParseGoSourceCode parse go source code from .go file path or source code

type GoStructField

type GoStructField struct {
	FieldName    string
	FieldType    string
	FieldTag     string
	FieldTagKind string
	FieldComment string
}

type KclExample added in v0.6.0

type KclExample struct {
	Summary     string `json:"summary,omitempty"`
	Description string `json:"description,omitempty"`
	Value       string `json:"value,omitempty"`
}

KclExample defines the example code snippet of the schema

type KclExtensions added in v0.5.4

type KclExtensions struct {
	XKclModelType   *XKclModelType    `json:"x-kcl-type,omitempty"`
	XKclDecorators  XKclDecorators    `json:"x-kcl-decorators,omitempty"`
	XKclUnionTypes  []*KclOpenAPIType `json:"x-kcl-union-types,omitempty"`
	XKclDictKeyType *KclOpenAPIType   `json:"x-kcl-dict-key-type,omitempty"` // dict key type
}

KclExtensions defines all the KCL specific extensions patched to OpenAPI

type KclModelImportInfo added in v0.5.4

type KclModelImportInfo struct {
	Package string `json:"package,omitempty"` // import package path
	Alias   string `json:"alias,omitempty"`   // import alias
}

KclModelImportInfo defines how to import the current type

type KclOpenAPIType added in v0.5.4

type KclOpenAPIType struct {
	Type                 SwaggerTypeName            `json:"type,omitempty"`                 // object, string, array, integer, number, bool
	Format               TypeFormat                 `json:"format,omitempty"`               // type format
	Default              string                     `json:"default,omitempty"`              // default value
	Enum                 []string                   `json:"enum,omitempty"`                 // enum values
	ReadOnly             bool                       `json:"readOnly,omitempty"`             // readonly
	Description          string                     `json:"description,omitempty"`          // description
	Properties           map[string]*KclOpenAPIType `json:"properties,omitempty"`           // schema properties
	Required             []string                   `json:"required,omitempty"`             // list of required schema property names
	Items                *KclOpenAPIType            `json:"items,omitempty"`                // list item type
	AdditionalProperties *KclOpenAPIType            `json:"additionalProperties,omitempty"` // dict value type
	Examples             map[string]KclExample      `json:"examples,omitempty"`             // examples
	ExternalDocs         string                     `json:"externalDocs,omitempty"`         // externalDocs
	Ref                  string                     `json:"ref,omitempty"`                  // reference to schema path
	*KclExtensions                                  // x-kcl- extensions
}

KclOpenAPIType defines the KCL representation of SchemaObject field in Swagger v2.0. And the mapping between kcl type and the Kcl OpenAPI type is:

## basic types

┌───────────────────────┬──────────────────────────────────────┐
│      KCL Types        │      KCL OpenAPI Types (format)      │
├───────────────────────┼──────────────────────────────────────┤
│       str             │         string                       │
├───────────────────────┼──────────────────────────────────────┤
│       int             │        integer(int64)                │
├───────────────────────┼──────────────────────────────────────┤
│      float            │         number(float)                │
├───────────────────────┼──────────────────────────────────────┤
│       bool            │             bool                     │
├───────────────────────┼──────────────────────────────────────┤
│   number_multiplier   │    string(units.NumberMultiplier)    │
└───────────────────────┴──────────────────────────────────────┘

## Composite Types

┌───────────────────────┬───────────────────────────────────────────────────────────────────────────────┐
│      KCL Types        │      KCL OpenAPI Types (format)                                               │
├───────────────────────┼───────────────────────────────────────────────────────────────────────────────┤
│       list            │   type:array, items: itemType                                                 │
├───────────────────────┼───────────────────────────────────────────────────────────────────────────────┤
│       dict            │   type: object, additionalProperties: valueType, x-kcl-dict-key-type: keyType │
├───────────────────────┼───────────────────────────────────────────────────────────────────────────────┤
│      union            │   type: object, x-kcl-union-types: unionTypes                                 │
├───────────────────────┼───────────────────────────────────────────────────────────────────────────────┤
│      schema           │   type: object, properties: propertyTypes, required, x-kcl-type               │
├───────────────────────┼───────────────────────────────────────────────────────────────────────────────┤
│    nested schema      │   type: object, ref: jsonRefPath                                              │
└───────────────────────┴───────────────────────────────────────────────────────────────────────────────┘

func GetKclOpenAPIType added in v0.5.4

func GetKclOpenAPIType(pkgPath string, from *kcl.KclType, nested bool) *KclOpenAPIType

GetKclOpenAPIType converts the kcl.KclType(the representation of Type in KCL API) to KclOpenAPIType(the representation of Type in KCL Open API)

func (*KclOpenAPIType) GetAnyEnum added in v0.7.2

func (tpe *KclOpenAPIType) GetAnyEnum() []interface{}

func (*KclOpenAPIType) GetExtensionsMapping added in v0.7.2

func (tpe *KclOpenAPIType) GetExtensionsMapping() map[string]interface{}

func (*KclOpenAPIType) GetKclTypeName added in v0.5.4

func (tpe *KclOpenAPIType) GetKclTypeName(omitAny bool, addLink bool, escapeHtml bool) string

GetKclTypeName get the string representation of a KclOpenAPIType

func (*KclOpenAPIType) GetSchemaPkgDir added in v0.5.4

func (tpe *KclOpenAPIType) GetSchemaPkgDir(base string) string

type KclPackage added in v0.5.6

type KclPackage struct {
	Name        string `json:"name,omitempty"`        // kcl package name
	Version     string `json:"version,omitempty"`     // kcl package version
	Description string `json:"description,omitempty"` // summary of the kcl package

	SchemaList     []*KclOpenAPIType `json:"schemaList,omitempty"`     // the schema list sorted by name in the KCL package
	SubPackageList []*KclPackage     `json:"subPackageList,omitempty"` // the sub package list sorted by name in the KCL package
	// contains filtered or unexported fields
}

KclPackage contains package information of package metadata(such as name, version, description, ...) and exported models(such as schemas)

type Mode added in v0.5.2

type Mode int

Mode is the mode of kcl schema code generation.

const (
	ModeAuto Mode = iota
	ModeGoStruct
	ModeJsonSchema
	ModeTerraformSchema
	ModeJson
	ModeYaml
)

type Options

type Options struct {
	GoPackage string
	PbPackage string
}

type SpecInfo added in v0.5.4

type SpecInfo struct {
	Title       string `json:"title"`
	Version     string `json:"version"`
	Description string `json:"description,omitempty"`
}

SpecInfo defines KCL package info

type SwaggerTypeName added in v0.5.4

type SwaggerTypeName string

SwaggerTypeName defines possible values of "type" field in Swagger v2.0 spec

const (
	Object  SwaggerTypeName = "object"
	String  SwaggerTypeName = "string"
	Array   SwaggerTypeName = "array"
	Integer SwaggerTypeName = "integer"
	Number  SwaggerTypeName = "number"
	Bool    SwaggerTypeName = "bool"
)

type SwaggerV2Spec added in v0.5.4

type SwaggerV2Spec struct {
	Definitions map[string]*KclOpenAPIType `json:"definitions"`
	Paths       map[string]interface{}     `json:"paths"`
	Swagger     string                     `json:"swagger"`
	Info        SpecInfo                   `json:"info"`
}

SwaggerV2Spec defines KCL OpenAPI Spec based on Swagger v2.0

func ExportSwaggerV2Spec added in v0.6.0

func ExportSwaggerV2Spec(pkgPath string) (*SwaggerV2Spec, error)

ExportSwaggerV2Spec extracts the swagger v2 representation of a kcl package

type TypeFormat added in v0.5.4

type TypeFormat string

TypeFormat defines possible values of "format" field in Swagger v2.0 spec

const (
	Int64            TypeFormat = "int64"
	Float            TypeFormat = "float"
	NumberMultiplier TypeFormat = "units.NumberMultiplier"
)

type XKclDecorator added in v0.7.2

type XKclDecorator struct {
	Name      string            `json:"name,omitempty"`
	Arguments []string          `json:"arguments,omitempty"`
	Keywords  map[string]string `json:"keywords,omitempty"`
}

XKclDecorator definition

type XKclDecorators added in v0.5.4

type XKclDecorators []*XKclDecorator

XKclDecorators defines the `x-kcl-decorators` extension

type XKclModelType added in v0.5.4

type XKclModelType struct {
	Type   string              `json:"type,omitempty"`   // schema short name
	Import *KclModelImportInfo `json:"import,omitempty"` // import information
}

XKclModelType defines the `x-kcl-type` extension

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL