Help us shape the future of Go! Take the 2026 Go Developer Survey

model

package
v0.9.8 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 4 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// BaseTypes enumerates all IDL primitive types mapped to Go types.
	BaseTypes      = []*Type{TypeBool, TypeByte, TypeInt8, TypeInt16, TypeInt32, TypeInt64, TypeUint8, TypeUint16, TypeUint32, TypeUint64, TypeFloat64, TypeString, TypeBinary}
	ContainerTypes = []*Type{TypeBaseList, TypeBaseMap, TypeBaseSet}
	// BaseModel is a sentinel Model used as Scope for builtin types.
	// Types with Scope == &BaseModel are treated as built-in and don't need cross-package qualification.
	BaseModel = Model{}
)
View Source
var (
	TypeBool = &Type{
		Name:  "bool",
		Scope: &BaseModel,
		Kind:  KindBool,
	}
	// TypeByte maps Thrift's "byte" to Go's "int8" (Thrift byte is signed).
	TypeByte = &Type{
		Name:  "int8",
		Scope: &BaseModel,
		Kind:  KindInt8,
	}
	// TypePbByte maps Protobuf's "bytes" element to Go's "byte" (unsigned).
	TypePbByte = &Type{
		Name:  "byte",
		Scope: &BaseModel,
		Kind:  KindInt8,
	}
	TypeUint8 = &Type{
		Name:  "uint8",
		Scope: &BaseModel,
		Kind:  KindInt8,
	}
	TypeUint16 = &Type{
		Name:  "uint16",
		Scope: &BaseModel,
		Kind:  KindInt16,
	}
	TypeUint32 = &Type{
		Name:  "uint32",
		Scope: &BaseModel,
		Kind:  KindInt32,
	}
	TypeUint64 = &Type{
		Name:  "uint64",
		Scope: &BaseModel,
		Kind:  KindInt64,
	}
	TypeUint = &Type{
		Name:  "uint",
		Scope: &BaseModel,
		Kind:  KindInt,
	}
	TypeInt8 = &Type{
		Name:  "int8",
		Scope: &BaseModel,
		Kind:  KindInt8,
	}
	TypeInt16 = &Type{
		Name:  "int16",
		Scope: &BaseModel,
		Kind:  KindInt16,
	}
	TypeInt32 = &Type{
		Name:  "int32",
		Scope: &BaseModel,
		Kind:  KindInt32,
	}
	TypeInt64 = &Type{
		Name:  "int64",
		Scope: &BaseModel,
		Kind:  KindInt64,
	}
	TypeInt = &Type{
		Name:  "int",
		Scope: &BaseModel,
		Kind:  KindInt,
	}
	TypeFloat32 = &Type{
		Name:  "float32",
		Scope: &BaseModel,
		Kind:  KindFloat64,
	}
	TypeFloat64 = &Type{
		Name:  "float64",
		Scope: &BaseModel,
		Kind:  KindFloat64,
	}
	TypeString = &Type{
		Name:  "string",
		Scope: &BaseModel,
		Kind:  KindString,
	}
	TypeBinary = &Type{
		Name:     "binary",
		Scope:    &BaseModel,
		Kind:     KindSlice,
		Category: CategoryBinary,
		Extra:    []*Type{TypePbByte},
	}

	TypeBaseMap = &Type{
		Name:     "map",
		Scope:    &BaseModel,
		Kind:     KindMap,
		Category: CategoryMap,
	}
	TypeBaseSet = &Type{
		Name:     "set",
		Scope:    &BaseModel,
		Kind:     KindSlice,
		Category: CategorySet,
	}
	TypeBaseList = &Type{
		Name:     "list",
		Scope:    &BaseModel,
		Kind:     KindSlice,
		Category: CategoryList,
	}
)

Functions

func IsBaseType

func IsBaseType(typ *Type) bool

Types

type BoolExpression

type BoolExpression struct {
	Src bool
}

func (BoolExpression) Expression

func (boolExpr BoolExpression) Expression() string

type Category

type Category int64

Category represents the IDL-level semantic type category (Thrift/Protobuf). Values align with Thrift type IDs where applicable.

const (
	CategoryConstant  Category = 1
	CategoryBinary    Category = 8
	CategoryMap       Category = 9
	CategoryList      Category = 10
	CategorySet       Category = 11
	CategoryEnum      Category = 12
	CategoryStruct    Category = 13
	CategoryUnion     Category = 14
	CategoryException Category = 15
	CategoryTypedef   Category = 16
	CategoryService   Category = 17
)

type Choice

type Choice struct {
	MessageName string
	ChoiceName  string
	Type        *Type
}

type Constant

type Constant struct {
	Scope *Model
	Name  string
	Type  *Type
	Value Literal
}

type DoubleExpression

type DoubleExpression struct {
	Src float64
}

func (DoubleExpression) Expression

func (doubleExpr DoubleExpression) Expression() string

type Enum

type Enum struct {
	Scope  *Model
	Name   string
	GoType string
	Values []Constant
}

type Field

type Field struct {
	Scope            *Struct
	Name             string
	Type             *Type
	IsSetDefault     bool         // whether a default value is explicitly set in IDL
	DefaultValue     Literal      // the default value expression
	Required         RequiredNess // Thrift requiredness
	Tags             Tags         // struct field tags (json, query, form, etc.)
	LeadingComments  string
	TrailingComments string
	IsPointer        bool // whether to generate as pointer type (for optional fields)
}

func (Field) GenGoTags

func (f Field) GenGoTags() string

type Function

type Function struct {
	Scope *Model
	Name  string
	Args  []Variable
	Rets  []Variable
	Code  string
}

type IntExpression

type IntExpression struct {
	Src int
}

func (IntExpression) Expression

func (intExpr IntExpression) Expression() string

type Kind

type Kind uint

Kind mirrors reflect.Kind to represent Go type kinds for code generation.

const (
	KindInvalid Kind = iota
	KindBool
	KindInt
	KindInt8
	KindInt16
	KindInt32
	KindInt64
	KindUint
	KindUint8
	KindUint16
	KindUint32
	KindUint64
	KindUintptr
	KindFloat32
	KindFloat64
	KindComplex64
	KindComplex128
	KindArray
	KindChan
	KindFunc
	KindInterface
	KindMap
	KindPtr
	KindSlice
	KindString
	KindStruct
	KindUnsafePointer
)

type ListExpression

type ListExpression struct {
	ElementType *Type
	Elements    []Literal
}

func (ListExpression) Expression

func (listExpr ListExpression) Expression() string

type Literal

type Literal interface {
	Expression() string
}

type MapExpression

type MapExpression struct {
	KeyType   *Type
	ValueType *Type
	Elements  map[string]Literal
}

func (MapExpression) Expression

func (mapExpr MapExpression) Expression() string

type Method

type Method struct {
	Scope        *Model
	ReceiverName string
	ReceiverType *Type
	ByPtr        bool
	Function
}

type Model

type Model struct {
	FilePath string            // output file path (set during generation)
	Package  string            // Go import path for this model
	Imports  map[string]*Model // imported models, keyed by import path

	PackageName string // short package name (last segment of Package)
	Typedefs    []TypeDef
	Constants   []Constant
	Variables   []Variable
	Functions   []Function
	Enums       []Enum
	Structs     []Struct
	Methods     []Method
	Oneofs      []Oneof // protobuf oneof fields
}

Model represents a single IDL file translated into Go code generation data. Each Model corresponds to one output .go file.

func (Model) IsEmpty

func (m Model) IsEmpty() bool

type Models

type Models []*Model

func (*Models) MergeArray

func (a *Models) MergeArray(b []*Model)

func (*Models) MergeMap

func (a *Models) MergeMap(b map[string]*Model)

MergeMap appends models from b that are not already in a (by pointer identity).

type NumberExpression

type NumberExpression struct {
	Src string
}

NumberExpression stores the number as a raw string to preserve the original IDL representation (e.g. hex literals, large numbers).

func (NumberExpression) Expression

func (numExpr NumberExpression) Expression() string

type Oneof

type Oneof struct {
	MessageName   string
	OneofName     string
	InterfaceName string
	Choices       []Choice
}

type RequiredNess

type RequiredNess int

RequiredNess maps to Thrift field requiredness semantics.

const (
	RequiredNess_Default  RequiredNess = 0 // Thrift "default" requiredness
	RequiredNess_Required RequiredNess = 1
	RequiredNess_Optional RequiredNess = 2
)

type StringExpression

type StringExpression struct {
	Src string
}

func (StringExpression) Expression

func (stringExpr StringExpression) Expression() string

type Struct

type Struct struct {
	Scope           *Model
	Name            string
	Fields          []Field
	Category        Category
	LeadingComments string
}

type Tag

type Tag struct {
	Key       string
	Value     string
	IsDefault bool // true if auto-generated (not explicitly set in IDL annotation)
}

type Tags

type Tags []Tag

func (Tags) Len

func (ts Tags) Len() int

func (Tags) Less

func (ts Tags) Less(i, j int) bool

func (*Tags) Remove

func (ts *Tags) Remove(name string)

func (Tags) String

func (ts Tags) String() string

func (Tags) Swap

func (ts Tags) Swap(i, j int)

type Type

type Type struct {
	Name     string   // Go type name (e.g. "int64", "MyStruct")
	Scope    *Model   // owning model; nil means unresolved, &BaseModel means builtin
	Kind     Kind     // Go type kind
	Indirect bool     // whether this type is referenced indirectly (pointer)
	Category Category // IDL semantic category
	Extra    []*Type  // container element types: [elem] for list/set, [key, value] for map
	HasNew   bool     // whether this type needs a constructor (e.g. structs, enums)
}

Type describes a Go type for code generation, bridging IDL types to Go types.

func NewCategoryType

func NewCategoryType(typ *Type, cg Category) *Type

NewCategoryType creates a shallow copy of typ with a different Category. Used when the same Go type has different IDL semantics (e.g. int64 as enum vs constant).

func NewEnumType

func NewEnumType(name string, cg Category) *Type

func NewFuncType

func NewFuncType(name string, cg Category) *Type

func NewOneofType

func NewOneofType(name string) *Type

func NewStructType

func NewStructType(name string, cg Category) *Type

func (*Type) IsBaseType

func (rt *Type) IsBaseType() bool

func (*Type) IsBinary

func (rt *Type) IsBinary() bool

func (*Type) IsSettable

func (rt *Type) IsSettable() bool

IsSettable returns true if the type is a reference type that can be nil-checked.

func (*Type) ResolveDefaultValue

func (rt *Type) ResolveDefaultValue() string

func (*Type) ResolveName

func (rt *Type) ResolveName(scope *Model) (string, error)

ResolveName returns the fully qualified Go type name relative to scope. For structs, it prepends "*" (pointer). For types from different packages, it prepends the package name qualifier.

func (*Type) ResolveNameForTypedef

func (rt *Type) ResolveNameForTypedef(scope *Model) (string, error)

ResolveNameForTypedef returns the fully qualified Go type name for a typedef alias. Unlike ResolveName, it does not add pointer prefix for struct types since typedefs define new named types rather than references.

type TypeDef

type TypeDef struct {
	Scope *Model
	Alias string
	Type  *Type
}

type Variable

type Variable struct {
	Scope *Model
	Name  string
	Type  *Type
	Value Literal
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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