iprotogen

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// IProtoDirective - generate [iproto.MarshalerUnmarshaler] implementation for types marked by this special comment.
	IProtoDirective = "//adv:iproto:"

	// IProtoTag - struct tag for overriding the field's representation
	IProtoTag = "iproto"
)
View Source
const TmpDirPrefix = "iprotogen-"

Variables

This section is empty.

Functions

func GeneratedFileName

func GeneratedFileName(fname string) string

func GetModDir

func GetModDir(pkgDir string) (string, error)

GetModDir returns the outermost module directory containing specified package directory

func RebasePath

func RebasePath(base, newBase, file string) (string, error)

func StripComments

func StripComments(groups []*ast.CommentGroup) []*ast.CommentGroup

StripComments keeps:

  • directives
  • iprotogen banner

All other comments are removed.

func StripModule

func StripModule(modPath string, needTests bool) (string, error)

StripModule copies the module to a temporary location preserving only:

  • go.mod and go.sum files
  • all imports
  • all type and const declarations
  • all methods (replacing func bodies with a bare return or an empty block depending on values returned)
  • all funcs named main (regardless of their params, bodies are stripped too)
  • all comments on these entities

Anything other (e.g. vars, non-method funcs) is removed.

If a directory was created successfully it is returned always, even in the case of an error.

func StripPackage

func StripPackage(pkgDir string, needTests bool) (tmpModDir string, tmpPkgDir string, err error)

func WithDisableFormatting added in v0.0.11

func WithDisableFormatting(opt *fileEmitOptions)

func WithIgnoreGenerated added in v0.0.11

func WithIgnoreGenerated(opt *pkgParserOptions)

func WithParseTests added in v0.0.11

func WithParseTests(opt *pkgParserOptions)

Types

type Array

type Array struct {
	Len      *ast.BasicLit
	ElemType Type
}

func (Array) EmitMarshaler

func (arr Array) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (Array) EmitUnmarshaler

func (arr Array) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type Bool

type Bool struct {
	True  *ast.BasicLit
	False *ast.BasicLit
}

Bool represents a value with an underlying type bool. When decoding, False byte is decoded as false, and anything else as true (not only the True byte)

func (Bool) EmitMarshaler

func (b Bool) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (Bool) EmitUnmarshaler

func (b Bool) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type ByteArray

type ByteArray struct {
	Len      *ast.BasicLit
	TypeExpr ast.Expr
}

func (ByteArray) EmitMarshaler

func (ByteArray) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (ByteArray) EmitUnmarshaler

func (ba ByteArray) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type CommentsByIdent

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

func (*CommentsByIdent) FromFile

func (cbi *CommentsByIdent) FromFile(fset *token.FileSet, f *ast.File)

func (*CommentsByIdent) FromTypesInfo

func (cbi *CommentsByIdent) FromTypesInfo(fset *token.FileSet, ti *types.Info)

func (*CommentsByIdent) Get

func (cbi *CommentsByIdent) Get(name *ast.Ident) (*structtag.Tag, error)

type Custom

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

func (Custom) EmitMarshaler

func (c Custom) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (Custom) EmitUnmarshaler

func (c Custom) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type Decl

type Decl struct {
	TypeIdent *ast.Ident
	Type      Type
}

func (Decl) EmitMarshalerDecl

func (d Decl) EmitMarshalerDecl(decls []ast.Decl) []ast.Decl

func (Decl) EmitUnmarshalerDecl

func (d Decl) EmitUnmarshalerDecl(decls []ast.Decl) []ast.Decl

type DefType

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

DefType describes a defined type

type Dumb

type Dumb struct{}

func (Dumb) EmitMarshaler

func (Dumb) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (Dumb) EmitUnmarshaler

func (d Dumb) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type File

type File struct {
	Directives  string // before the package token
	PkgName     string
	ImportByPkg map[string]string   // pkg -> alias
	ImportAlias map[string]struct{} // the same file shouldn't be processed in parallel, so using goro-unsafe maps seems to be ok
	Decls       []Decl
}

func (*File) AddType

func (f *File) AddType(ident *ast.Ident, typ Type)

func (File) Emit

func (f File) Emit(optFuncs ...FileEmitOptionsFunc) ([]byte, error)

func (*File) TypeToExpr

func (f *File) TypeToExpr(typ types.Type, pos token.Pos) (ast.Expr, error)

TypeToExpr is a design error and should be eliminated.

type FileEmitOptionsFunc

type FileEmitOptionsFunc func(*fileEmitOptions)

type FilesByPath

type FilesByPath map[string]*File

func (FilesByPath) Get

func (fbp FilesByPath) Get(filePath, pkgName string, directivesByFile map[string]string) *File

type Float

type Float struct {
	TypeExpr ast.Expr
	Size     int
}

func (Float) EmitMarshaler

func (f Float) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (Float) EmitUnmarshaler

func (f Float) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type Integer

type Integer struct {
	TypeExpr ast.Expr
	Size     int
	Min      int64
	Max      uint64
}

func (Integer) EmitMarshaler

func (i Integer) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (Integer) EmitUnmarshaler

func (i Integer) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type Map

type Map struct {
	LenType   Integer  // LenType.TypeExpr should always be int
	TypeExpr  ast.Expr // type of the map itself
	KeyType   Type
	ValueType Type
}

func (Map) EmitMarshaler

func (m Map) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (Map) EmitUnmarshaler

func (m Map) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type Parser

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

func NewParser

func NewParser(optFuncs ...ParserOptionsFunc) (*Parser, error)

func (*Parser) ParsePackage

func (p *Parser) ParsePackage(pkgName string) (FilesByPath, error)

type ParserOptionsFunc

type ParserOptionsFunc func(*pkgParserOptions)

func WithBuildFlag added in v0.0.11

func WithBuildFlag(flag string) ParserOptionsFunc

func WithModuleDir added in v0.0.11

func WithModuleDir(dir string) ParserOptionsFunc

func WithOnlyFile added in v0.0.11

func WithOnlyFile(fname string) ParserOptionsFunc

type Pointer

type Pointer struct {
	Type     Type
	TypeExpr ast.Expr // the type a pointer is pointing to
}

func (Pointer) EmitMarshaler

func (p Pointer) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (Pointer) EmitUnmarshaler

func (p Pointer) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type Slice

type Slice struct {
	LenType  Integer  // LenType.TypeExpr should always be int
	TypeExpr ast.Expr // type of the slice itself, not an element type
	ElemType Type
}

func (Slice) EmitMarshaler

func (sl Slice) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (Slice) EmitUnmarshaler

func (sl Slice) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type StringOrBytes

type StringOrBytes struct {
	LenType  Integer
	TypeExpr ast.Expr
	IsSlice  bool
}

func (StringOrBytes) EmitMarshaler

func (s StringOrBytes) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (StringOrBytes) EmitUnmarshaler

func (s StringOrBytes) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type Struct

type Struct struct {
	Fields []StructField
}

func (Struct) EmitMarshaler

func (s Struct) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (Struct) EmitUnmarshaler

func (s Struct) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type StructField

type StructField struct {
	Ident *ast.Ident
	Type  Type
}

type StructLiteral

type StructLiteral struct {
	Fields   []StructField
	TypeExpr ast.Expr
}

StructLiteral is used for map keys and values

func (StructLiteral) EmitMarshaler

func (s StructLiteral) EmitMarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

func (StructLiteral) EmitUnmarshaler

func (s StructLiteral) EmitUnmarshaler(x ast.Expr, block []ast.Stmt) []ast.Stmt

type Type

type Type interface {
	EmitMarshaler(ast.Expr, []ast.Stmt) []ast.Stmt
	EmitUnmarshaler(ast.Expr, []ast.Stmt) []ast.Stmt
}

Jump to

Keyboard shortcuts

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