gen

package
v2.27.3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package gen allows generating Go structs from avro schemas.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Struct

func Struct(s string, w io.Writer, cfg Config) error

Struct generates Go structs based on the schema and writes them to w.

func StructFromSchema

func StructFromSchema(schema avro.Schema, w io.Writer, cfg Config) error

StructFromSchema generates Go structs based on the schema and writes them to w.

Types

type Config

type Config struct {
	PackageName  string
	Tags         map[string]TagStyle
	FullName     bool
	Encoders     bool
	FullSchema   bool
	StrictTypes  bool
	Initialisms  []string
	LogicalTypes []LogicalType
}

Config configures the code generation.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator generates Go structs from schemas.

func NewGenerator

func NewGenerator(pkg string, tags map[string]TagStyle, opts ...OptsFunc) *Generator

NewGenerator returns a generator.

func (*Generator) Parse

func (g *Generator) Parse(schema avro.Schema)

Parse parses an avro schema into Go types.

func (*Generator) Reset

func (g *Generator) Reset()

Reset reset the generator.

func (*Generator) Write

func (g *Generator) Write(w io.Writer) error

Write writes Go code from the parsed schemas.

type LogicalType

type LogicalType struct {
	// Name of the LogicalType
	Name string
	// Typ returned, has to be a valid Go type
	Typ string
	// Import added as import (if not empty)
	Import string
	// ThirdPartyImport added as import (if not empty)
	ThirdPartyImport string
}

LogicalType used when the name of the "LogicalType" field in the Avro schema matches the Name attribute.

type OptsFunc

type OptsFunc func(*Generator)

OptsFunc is a function that configures a generator.

func WithEncoders

func WithEncoders(b bool) OptsFunc

WithEncoders configures the generator to generate schema and encoders on all objects.

func WithFullName

func WithFullName(b bool) OptsFunc

WithFullName configures the generator to use the full name of a record when creating the struct name.

func WithFullSchema

func WithFullSchema(b bool) OptsFunc

WithFullSchema configures the generator to store the full schema within the generation context.

func WithInitialisms

func WithInitialisms(ss []string) OptsFunc

WithInitialisms configures the generator to use additional custom initialisms when styling struct and field names.

func WithLogicalType

func WithLogicalType(logicalType LogicalType) OptsFunc

WithLogicalType registers a LogicalType which takes precedence over the default logical types defined by this package.

func WithPackageDoc

func WithPackageDoc(text string) OptsFunc

WithPackageDoc configures the generator to output the given text as a package doc comment.

func WithStrictTypes

func WithStrictTypes(b bool) OptsFunc

WithStrictTypes configures the generator to use strict type sizes.

func WithTemplate

func WithTemplate(template string) OptsFunc

WithTemplate configures the generator to use a custom template provided by the user.

type TagStyle

type TagStyle string

TagStyle defines the styling for a tag.

const (
	// Original is a style like whAtEVer_IS_InthEInpuT.
	Original TagStyle = "original"
	// Snake is a style like im_written_in_snake_case.
	Snake TagStyle = "snake"
	// Camel is a style like imWrittenInCamelCase.
	Camel TagStyle = "camel"
	// Kebab is a style like im-written-in-kebab-case.
	Kebab TagStyle = "kebab"
	// UpperCamel is a style like ImWrittenInUpperCamel.
	UpperCamel TagStyle = "upper-camel"
)

Jump to

Keyboard shortcuts

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