genjay

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

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

Go to latest
Published: Apr 14, 2026 License: AGPL-3.0 Imports: 23 Imported by: 0

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,

package main

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

	Wheel 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
}

Features to add

  • Compress enum integers with small value ranges.

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 (
	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 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.

	// IsMarshalMethodPtr changes generated MarshalJ method to a pointer receiver. Used for suppressing Go linter messages:
	// `Struct ... has methods on both value and pointer receivers. Such usage is not recommended by the Go Documentation.`
	// True: `func (f *Foo) MarshalJ()`,
	// False: `func (f Foo) MarshalJ()`.
	IsMarshalMethodPtr bool

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