datatransforms

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2025 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

From https://github.com/twpayne/go-jsonstruct

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CsvFileToJson

func CsvFileToJson(filename string) string

func DefaultExportNameFunc

func DefaultExportNameFunc(name string, abbreviations map[string]bool) string

DefaultExportNameFunc returns the exported name for name.

func Generate

func Generate(opts GenerateOptions) (string, error)

func SplitComponents

func SplitComponents(name string) []string

SplitComponents splits name into components. name may be kebab case, snake case, or camel case.

Types

type ExportNameFunc

type ExportNameFunc func(string) string

An ExportNameFunc returns the exported name for a property.

type GenerateOptions

type GenerateOptions struct {
	JsonString       string
	JsonFile         string
	WebApi           string
	Stdin            bool
	GetFromClipboard bool
}

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) Generate

func (g *Generator) Generate() ([]byte, error)

Generate returns the Go source code for the observed values.

func (*Generator) ObserveJSONFile

func (g *Generator) ObserveJSONFile(filename string) error

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

func (g *Generator) ObserveJSONReader(r io.Reader) error

ObserveJSONReader observes JSON values from r.

func (*Generator) ObserveJSONString

func (g *Generator) ObserveJSONString(s string)

ObserveJSONString obverses JSON values from a json string

func (*Generator) ObserveValue

func (g *Generator) ObserveValue(value any)

ObserveValue observes value.

func (*Generator) ObserveYAMLFile

func (g *Generator) ObserveYAMLFile(filename string) error

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"`
}

func (*Generator) ObserveYAMLReader

func (g *Generator) ObserveYAMLReader(r io.Reader) error

ObserveYAMLReader observes YAML values from r.

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.

Jump to

Keyboard shortcuts

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