genjay

package module
v0.0.0-...-4671515 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: AGPL-3.0 Imports: 26 Imported by: 0

README

genjay

Go Reference Go Report Card license AGPL3

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

Tag options

Each struct field can have a tag containing multiple options.

  • Ignore field j:"-"
  • Required field j:"req"
    Flag any string or slice field as required, indicating that it will never be empty.
    A micro optimization that removes if length != 0 {…} statement before converting into []byte.
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.

Tag profiles

It is possible to use multiple tags for including different struct fields for separate scenarios.
For example:

type Car struct {
	Id    uint64 `vin:"-" db:"-"`
	VIN   []byte
	Make  string `vin:"-"`
	Model string `vin:"-"`
}

var car Car

Using the following commands will generate different methods.

Command Tag Id VIN Make Model Marshal Unmarshal Output file
jay j ✅ ✅ ✅ ✅ car.MarshalJ() car.UnmarshalJ() jay.go
jay -mf Save -uf Get -tag db -o db db ❌ ✅ ✅ ✅ car.Save() car.Get() db.go
jay -mf write -uf read -tag vin -o vin vin ❌ ✅ ❌ ❌ car.write() car.read() vin.go

Embedded structs

genjay can include unexported structs when they are embedded into an exported struct and have an exported field name.
For example:

package main

type Car struct {
	Wheels          // Added - exported type
	W       Wheels  // Added - exported field name
	Gearbox gearbox // Added - exported field name

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

	gearbox         // Ignored - not an exported type
	gbx     gearbox // Ignored - not an exported field name
	_       Wheels  // Ignored - not an exported field name

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

type Engine struct {
	CC uint16
}

Documentation

Overview

Package genjay 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
	MethodMarshalJ        = "MarshalJ"
	MethodUnmarshalJ      = "UnmarshalJ"
	IntSize               = 32 << (^uint(0) >> 63) // 32-bit or 64-bit architecture.

)
View Source
const (
	BitAuto = MaxSize(iota * 32)
	Bit32
	Bit64
)
View Source
const (
	StructTagKey = "j" // StructTagKey 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:-`

)
View Source
const (
	ARRAY int8 = iota + 1
	SLICE
	STRUCT
	SelectorExpr
	TIME
	DURATION
	TypeSpec
)

List of supported types.

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

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

	StructTagKey      string // Default is `j`. Change for multiple different usages...
	MarshalFuncName   string // Default `MarshalJ`.
	UnmarshalFuncName string // Default `UnmarshalJ`.

	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 vary in length (8-bit to 64-bit) or be a fixed length (32-bit, or 64-bit) depending on the system's architecture.
	// True = Least bandwidth used.
	// False = Fastest CPU serialization/deserialization throughput.
	VariableUintSize bool

	Verbose     *log.Logger
	SearchTests bool // When true, searches Go test files for exported structs too.
	DisableSort bool // When true, uses the same order structs are defined in files. When false, sorts the generated functions by their struct name.

	// UseValueReceiver changes generated MarshalJ methods to use value receivers instead of pointer receivers.
	// True:  `func (c Car) MarshalJ()`.
	// False: `func (c *Car) MarshalJ()`,
	UseValueReceiver bool

	ReturnErrType bool // Determines if the error type returned is error (true) or bool (false).
	SkipTests     bool
	SkipMarshal   bool // When true doesn't generate any marshalling methods.
	SkipUnmarshal bool // When true doesn't generate any unmarshalling methods.
	// contains filtered or unexported fields
}

func LoadOptions

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

func (*Option) ProcessFiles

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

ProcessFiles ...

func (*Option) ProcessWrite

func (o *Option) ProcessWrite(source any, 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 for Go.
Command jay is the command line tool to generate Jay serialization code for Go.
comboTests
3 command

Jump to

Keyboard shortcuts

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