generate

package module
v0.0.0-...-7c1c066 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: AGPL-3.0 Imports: 23 Imported by: 1

README

generate

Go Reference Go Report Card

Traverses .go files to find exported Go structs to generate marshalling .MarshalJ() and unmarshalling .UnmarshalJ() methods for the Jay serialization format.

Field tag options

j:- Ignore this field.

TODO

max

  • Maximum value for integers and floats.
  • Maximum length for strings and slices.

min

  • Minimum value for integers and floats.
  • Minimum length for strings and slices.

req

  • Flag any string or slice field as required, indicating that it will never be empty.
  • A micro optimization for marshalling, removing length == 0 if statement before calling the function to convert into []byte.

Embedded structs

Any private struct can be embedded into an exported struct when an exported field name is included. For example,

type Car struct {
	gearbox         // Ignored - not an exported field name
	Gearbox gearbox // Added - exported field name
	gbx gearbox     // Ignored - not an exported field name

	Axel    // Ignored - no fields
	Turbo   // Ignored - no exported fields

	Wheels   // Added - exported type
	W Wheels // Added - exported field name
	w Wheels // Ignored - not an exported field name
	_ Wheels // Ignored - not an exported field name

	Wheels  `j:-`     // Ignored - flag present
	Wh Wheels `j:-`   // Ignored - flag present
	Gbx gearbox `j:-` // Ignored - flag present
}

type gearbox struct {
	GearsQty uint8
}

type Axel struct {}

type Turbo struct {
	size uint16
}

type Wheels struct {
	Offset int
	- int
}

Features to add

  • Compress enum integers with small value ranges.

Documentation

Overview

Package generate scans .go files for exported structs and generates methods .MarshalJ() and .UnmarshalJ() to serialize structs into the Jay serialization format https://github.com/speedyhoon/jay.

Index

Constants

View Source
const (
	TagIgnore structTag = iota - 2
	TagEmbed
	TagNone

	DecIgnore = "J--"
	DecEmbed  = "J-"
)
View Source
const (
	ExportedErr           = pkgName + ".ErrUnexpectedEOB"
	DefaultOutputFileName = pkgName + ext.Go
	IntSize               = 32 << (^uint(0) >> 63) // 32-bit or 64-bit architecture.

)
View Source
const (
	BitAuto = MaxSize(iota * 32)
	Bit32
	Bit64
)
View Source
const (
	StructTagName = "j" // StructTagName is the field tag to specify optional flags. For example, `j:max=0`
	IgnoreFlag    = "-" // IgnoreFlag is the value to ignore any exported field from serializing: `j:-`

)

Variables

View Source
var (
	ErrNoSource     = errors.New("no filename or source provided")
	ErrNoneExported = errors.New("no exported struct fields found")
)

Functions

func IsCommentOrWhitespace

func IsCommentOrWhitespace(r rune) bool

func ParseFile

func ParseFile(filename string, src interface{}) (f *dst.File, err error)

Types

type MaxSize

type MaxSize uint8

type Option

type Option struct {
	// ErrVarName is the name of an exported error variable to return from UnmarshalJ functions,
	// overriding the default "jay.ErrUnexpectedEOB". Helpful if generated code imports the jay package only for the error variable.
	ErrVarName string

	MaxIntSize  MaxSize
	MaxUintSize MaxSize

	Is32bit bool
	IsDebug bool

	// MaxDefaultStrSize limits all strings to be within this length if a field tag is NOT present.
	// Minimum:	1 (1 byte),
	// Default: maxUint8 (255 bytes),
	// Maximum: maxUint24 (16 Megabytes).
	// To override the default for a field use: `j:"max:4030"` for 4,030 bytes.
	// The smallest value is the most optimal for performance.
	MaxDefaultStrSize uint

	// OnlyTypes will only generate marshalling & unmarshalling functions for the listed types.
	// When empty, all types are permitted.
	// Expected struct type alias names like: "Vehicle", "animal.Species".
	OnlyTypes []string

	OutputFileName string

	// Whether type `int` should be a fixed length (4 bytes for 32-bit, or 8 bytes for 64-bit) or vary in length depending on the value provided.
	VariableIntSize bool

	// Whether type `uint` should be a fixed length (4 bytes 32-bit, or 8 bytes 64-bit) or vary in length depending on the value provided.
	// True = Highest CPU serialization/deserialization throughput,
	// false = Least bandwidth used.
	VariableUintSize bool

	Verbose     *log.Logger
	SearchTests bool

	// IsMarshalMethodPtr changes generated MarshalJ method to a pointer receiver. true: `func (f *Foo) MarshalJ()`, false: `func (f Foo) MarshalJ()`.
	IsMarshalMethodPtr bool

	SkipTests     bool
	SkipMarshal   bool
	SkipUnmarshal bool
	// contains filtered or unexported fields
}

func LoadOptions

func LoadOptions(opts ...Option) (o Option)

func (*Option) ProcessFiles

func (o *Option) ProcessFiles(source interface{}, filenames ...string) (output []Output, errs error)

ProcessFiles ...

func (*Option) ProcessWrite

func (o *Option) ProcessWrite(source interface{}, outputFile string, filenames ...string) (err error)

ProcessWrite processes a file and writes to OutputFileName.

type Output

type Output struct {
	Src []byte
	Dir string
}

Directories

Path Synopsis
cmd
jay command
Command jay is the command line tool to generate Jay serialization code.
Command jay is the command line tool to generate Jay serialization code.
comboTests
3 command

Jump to

Keyboard shortcuts

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