Documentation ¶
Overview ¶
Index ¶
- func CsvFileToJson(filename string) string
- func DefaultExportNameFunc(name string, abbreviations map[string]bool) string
- func Generate(opts GenerateOptions) (string, error)
- func SplitComponents(name string) []string
- type ExportNameFunc
- type GenerateOptions
- 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) ObserveJSONString(s string)
- 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 WithOmitEmpty(omitEmptyOption OmitEmptyOption) GeneratorOption
- func WithPackageComment(packageComment string) GeneratorOption
- func WithPackageName(packageName string) GeneratorOption
- func WithRenames(renames map[string]string) GeneratorOption
- func WithSkipUnparseableProperties(skipUnparseableProperties 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 OmitEmptyOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CsvFileToJson ¶
func DefaultExportNameFunc ¶
DefaultExportNameFunc returns the exported name for name.
func Generate ¶
func Generate(opts GenerateOptions) (string, error)
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 GenerateOptions ¶
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) ObserveJSONString ¶
ObserveJSONString obverses JSON values from a json string
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 abbreviatons.
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 WithOmitEmpty ¶
func WithOmitEmpty(omitEmptyOption OmitEmptyOption) GeneratorOption
WithOmitEmpty sets whether each field is tagged with omitempty.
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 WithSkipUnparseableProperties ¶
func WithSkipUnparseableProperties(skipUnparseableProperties bool) GeneratorOption
WithSkipUnparseableProperties sets whether unparseable properties should be skipped.
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 OmitEmptyOption ¶
type OmitEmptyOption int
An OmitEmptyOption is an option for handling omitempty.
const ( OmitEmptyNever OmitEmptyOption = iota OmitEmptyAlways OmitEmptyAuto )
omitempty options.