Documentation ¶
Index ¶
- func DefaultExportNameFunc(name string, abbreviations map[string]bool) string
- func SplitComponents(name string) []string
- type ExportNameFunc
- type Generator
- func (g *Generator) Generate() ([]byte, error)
- func (g *Generator) ObserveJSONFile(filename string) error
- func (g *Generator) ObserveJSONReader(r io.Reader) error
- func (g *Generator) ObserveValue(value any)
- func (g *Generator) ObserveYAMLFile(filename string) error
- func (g *Generator) ObserveYAMLReader(r io.Reader) error
- type GeneratorOption
- func WithAbbreviations(abbreviations ...string) GeneratorOption
- func WithAddStructTagName(structTagName string) GeneratorOption
- func WithExportNameFunc(exportNameFunc ExportNameFunc) GeneratorOption
- func WithExtraAbbreviations(abbreviations ...string) GeneratorOption
- func WithGoFormat(goFormat bool) GeneratorOption
- func WithImports(imports ...string) GeneratorOption
- func WithIntType(intType string) GeneratorOption
- func WithOmitEmptyTags(omitEmptyTags OmitEmptyTagsType) GeneratorOption
- func WithPackageComment(packageComment string) GeneratorOption
- func WithPackageName(packageName string) GeneratorOption
- func WithRenames(renames map[string]string) GeneratorOption
- func WithSkipUnparsableProperties(skipUnparsableProperties bool) GeneratorOption
- func WithStringTags(stringTags bool) GeneratorOption
- func WithStructTagName(structTagName string) GeneratorOption
- func WithStructTagNames(structTagNames []string) GeneratorOption
- func WithTypeComment(typeComment string) GeneratorOption
- func WithTypeName(typeName string) GeneratorOption
- func WithUseJSONNumber(useJSONNumber bool) GeneratorOption
- type OmitEmptyTagsType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultExportNameFunc ¶
DefaultExportNameFunc returns the exported name for name.
func SplitComponents ¶
SplitComponents splits name into components. name may be kebab case, snake case, or camel case.
Types ¶
type ExportNameFunc ¶
An ExportNameFunc returns the exported name for a property.
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
A Generator generates Go types from observed values.
func NewGenerator ¶
func NewGenerator(options ...GeneratorOption) *Generator
NewGenerator returns a new Generator with options.
func (*Generator) ObserveJSONFile ¶
ObserveJSONFile observes JSON values from filename.
Example ¶
generator := NewGenerator() if err := generator.ObserveJSONFile("testdata/example.json"); err != nil { panic(err) } data, err := generator.Generate() if err != nil { panic(err) } os.Stdout.Write(data)
Output: package main type T struct { Age int `json:"age"` FavoriteFoods []string `json:"favoriteFoods,omitempty"` UserHeightM float64 `json:"user_height_m"` }
func (*Generator) ObserveJSONReader ¶
ObserveJSONReader observes JSON values from r.
func (*Generator) ObserveValue ¶
ObserveValue observes value.
func (*Generator) ObserveYAMLFile ¶
ObserveYAMLFile observes YAML values from filename.
Example ¶
generator := NewGenerator( WithPackageName("mypackage"), WithTypeName("MyType"), ) if err := generator.ObserveYAMLFile("testdata/example.yaml"); err != nil { panic(err) } data, err := generator.Generate() if err != nil { panic(err) } os.Stdout.Write(data)
Output: package mypackage type MyType struct { Nested struct { Bar bool `json:"bar"` Foo *string `json:"foo"` } `json:"nested"` }
type GeneratorOption ¶
type GeneratorOption func(*Generator)
A GeneratorOption sets an option on a Generator.
func WithAbbreviations ¶
func WithAbbreviations(abbreviations ...string) GeneratorOption
WithAbbreviations sets the abbreviations.
func WithAddStructTagName ¶
func WithAddStructTagName(structTagName string) GeneratorOption
WithAddStructTagName adds a struct tag name.
func WithExportNameFunc ¶
func WithExportNameFunc(exportNameFunc ExportNameFunc) GeneratorOption
WithExportNameFunc sets the export name function.
func WithExtraAbbreviations ¶
func WithExtraAbbreviations(abbreviations ...string) GeneratorOption
WithExtraAbbreviations adds abbreviations.
func WithGoFormat ¶
func WithGoFormat(goFormat bool) GeneratorOption
WithGoFormat sets whether the output is should be formatted with go fmt.
func WithImports ¶
func WithImports(imports ...string) GeneratorOption
WithImports adds custom package imports.
func WithIntType ¶
func WithIntType(intType string) GeneratorOption
WithIntType sets the integer type.
func WithOmitEmptyTags ¶
func WithOmitEmptyTags(omitEmptyTags OmitEmptyTagsType) GeneratorOption
WithOmitEmptyTags sets whether ",omitempty" tags should be used.
func WithPackageComment ¶
func WithPackageComment(packageComment string) GeneratorOption
WithPackageComment sets the package comment.
func WithPackageName ¶
func WithPackageName(packageName string) GeneratorOption
WithPackageName sets the package name.
func WithRenames ¶
func WithRenames(renames map[string]string) GeneratorOption
WithRenames sets the renames.
func WithSkipUnparsableProperties ¶
func WithSkipUnparsableProperties(skipUnparsableProperties bool) GeneratorOption
WithSkipUnparsableProperties sets whether unparsable properties should be skipped.
func WithStringTags ¶
func WithStringTags(stringTags bool) GeneratorOption
WithStringTags sets whether ",string" tags should be used.
func WithStructTagName ¶
func WithStructTagName(structTagName string) GeneratorOption
WithStructTagName sets the struct tag name.
func WithStructTagNames ¶
func WithStructTagNames(structTagNames []string) GeneratorOption
WithStructTagNames sets the struct tag names.
func WithTypeComment ¶
func WithTypeComment(typeComment string) GeneratorOption
WithTypeComment sets the type comment.
func WithTypeName ¶
func WithTypeName(typeName string) GeneratorOption
WithTypeName sets the type name.
func WithUseJSONNumber ¶
func WithUseJSONNumber(useJSONNumber bool) GeneratorOption
WithUseJSONNumber sets whether to use json.Number when both int and float64s are observed for the same property.
type OmitEmptyTagsType ¶
type OmitEmptyTagsType int
An OmitEmptyTagsType sets how to handle ,omitempty tags.
const ( OmitEmptyTagsNever OmitEmptyTagsType = iota OmitEmptyTagsAlways OmitEmptyTagsAuto )
OmitEmptyTags values.