enumgen

package
v1.4.3-poc-rc4 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	PkgPath                 string
	IntStringerName         = "IntStringerEnum"
	ValueOffsetName         = "ValueOffset"
	ScanIntEnumStringerName = "ScanIntEnumStringer"
)

Functions

This section is empty.

Types

type Enum

type Enum struct {
	Path    string
	Name    string
	Options Options
}

func GetEnumByName

func GetEnumByName(g *Generator, name string) *Enum

func NewEnum

func NewEnum(name string, options Options) *Enum

func (Enum) ConstName

func (e Enum) ConstName(value string) Snippet
Example
fmt.Println(string(sample.ConstName("XXX").Bytes()))
fmt.Println(string(sample.ConstName("ABC").Bytes()))
fmt.Println(string(scheme.ConstName("HTTP").Bytes()))
fmt.Println(string(scheme.ConstName("HTTPS").Bytes()))
Output:

SAMPLE__XXX
SAMPLE__ABC
SCHEME__HTTP
SCHEME__HTTPS

func (Enum) ConstUnknown

func (e Enum) ConstUnknown() Snippet

func (Enum) ConstValues

func (e Enum) ConstValues(f *File) Snippet
Example
fmt.Println(string(sample.ConstValues(f).Bytes()))
Output:

func (v Sample) ConstValues() []enum.IntStringerEnum {
return []enum.IntStringerEnum{SAMPLE__XXX, SAMPLE__YYY, SAMPLE__ZZZ}
}

func (Enum) Errors

func (e Enum) Errors(f *File) Snippet
Example
fmt.Println(string(sample.Errors(f).Bytes()))
fmt.Println(string(scheme.Errors(f).Bytes()))
Output:

var InvalidSample = errors.New("invalid Sample type")
var InvalidScheme = errors.New("invalid Scheme type")

func (Enum) Integer

func (e Enum) Integer(f *File) Snippet
Example
fmt.Println(string(sample.Integer(f).Bytes()))
Output:

func (v Sample) Int() int {
return int(v)
}

func (Enum) LabelParser

func (e Enum) LabelParser(f *File) Snippet
Example
fmt.Println(string(sample.LabelParser(f).Bytes()))
Output:

func ParseSampleFromLabel(s string) (Sample, error) {
switch s {
default:
return SAMPLE_UNKNOWN, InvalidSample
case "":
return SAMPLE_UNKNOWN, nil
case "样例XXX":
return SAMPLE__XXX, nil
case "样例YYY":
return SAMPLE__YYY, nil
case "样例ZZZ":
return SAMPLE__ZZZ, nil
}
}

func (Enum) Labeler

func (e Enum) Labeler(f *File) Snippet
Example
fmt.Println(string(sample.Labeler(f).Bytes()))
Output:

func (v Sample) Label() string {
switch v {
default:
return "UNKNOWN"
case SAMPLE_UNKNOWN:
return ""
case SAMPLE__XXX:
return "样例XXX"
case SAMPLE__YYY:
return "样例YYY"
case SAMPLE__ZZZ:
return "样例ZZZ"
}
}

func (Enum) Receiver

func (e Enum) Receiver(name string) *SnippetField

func (Enum) Scanner

func (e Enum) Scanner(f *File) Snippet
Example
fmt.Println(string(sample.Scanner(f).Bytes()))
Output:

func (v *Sample) Scan(src interface{}) error {
offset := 0
o, ok := interface{}(v).(enum.ValueOffset)
if ok {
offset = o.Offset()
}
i, err := enum.ScanIntEnumStringer(src, offset)
if err != nil {
return err
}
*(v) = Sample(i)
return nil
}

func (Enum) StarReceiver

func (e Enum) StarReceiver(name string) *SnippetField

func (Enum) StringParser

func (e Enum) StringParser(f *File) Snippet
Example
fmt.Println(string(sample.StringParser(f).Bytes()))
Output:

func ParseSampleFromString(s string) (Sample, error) {
switch s {
default:
return SAMPLE_UNKNOWN, InvalidSample
case "":
return SAMPLE_UNKNOWN, nil
case "XXX":
return SAMPLE__XXX, nil
case "YYY":
return SAMPLE__YYY, nil
case "ZZZ":
return SAMPLE__ZZZ, nil
}
}

func (Enum) Stringer

func (e Enum) Stringer(f *File) Snippet
Example
fmt.Println(string(sample.Stringer(f).Bytes()))
Output:

func (v Sample) String() string {
switch v {
default:
return "UNKNOWN"
case SAMPLE_UNKNOWN:
return ""
case SAMPLE__XXX:
return "XXX"
case SAMPLE__YYY:
return "YYY"
case SAMPLE__ZZZ:
return "ZZZ"
}
}

func (Enum) TextMarshaler

func (e Enum) TextMarshaler(f *File) Snippet
Example
fmt.Println(string(sample.TextMarshaler(f).Bytes()))
Output:

func (v Sample) MarshalText() ([]byte, error) {
s := v.String()
if s == "UNKNOWN" {
return nil, InvalidSample
}
return []byte(s), nil
}

func (Enum) TextUnmarshaler

func (e Enum) TextUnmarshaler(f *File) Snippet
Example
fmt.Println(string(sample.TextUnmarshaler(f).Bytes()))
Output:

func (v *Sample) UnmarshalText(data []byte) error {
s := string(bytes.ToUpper(data))
val, err := ParseSampleFromString(s)
if err != nil {
return err
}
*(v) = val
return nil
}

func (Enum) TypeName

func (e Enum) TypeName(f *File) Snippet

func (Enum) Valuer

func (e Enum) Valuer(f *File) Snippet
Example
fmt.Println(string(sample.Valuer(f).Bytes()))
Output:

func (v Sample) Value() (driver.Value, error) {
offset := 0
o, ok := interface{}(v).(enum.ValueOffset)
if ok {
offset = o.Offset()
}
return int64(v) + int64(offset), nil
}

func (Enum) VarInvalidError

func (e Enum) VarInvalidError() Snippet

func (Enum) WriteToFile

func (e Enum) WriteToFile(f *File)

type Generator

type Generator struct {
	*Scanner
	// contains filtered or unexported fields
}

func New

func New(pkg *pkgx.Pkg) *Generator

func (Generator) Output

func (g Generator) Output(cwd string)

func (*Generator) Scan

func (g *Generator) Scan(names ...string)

type Option

type Option struct {
	Label string   `json:"label"`
	Str   *string  `json:"str,omitempty"`
	Int   *int64   `json:"int,omitempty"`
	Float *float64 `json:"float,omitempty"`
}

func NewFloatOption

func NewFloatOption(f float64, label string) *Option

func NewIntOption

func NewIntOption(i int64, label string) *Option

func NewOption

func NewOption(i int64, str, label string) *Option

NewOption int-string option

func NewStringOption

func NewStringOption(str, label string) *Option

func (Option) Value

func (o Option) Value() interface{}

type Options

type Options []Option

func (Options) Len

func (o Options) Len() int

func (Options) Less

func (o Options) Less(i, j int) bool

func (Options) Swap

func (o Options) Swap(i, j int)

func (Options) Values

func (o Options) Values() []interface{}

type Scanner

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

func NewScanner

func NewScanner(pkg *pkgx.Pkg) *Scanner

func (*Scanner) Append

func (s *Scanner) Append(tn *types.TypeName, opt *Option)

func (*Scanner) Options

func (s *Scanner) Options(tn *types.TypeName) (Options, bool)

func (*Scanner) Package

func (s *Scanner) Package() *pkgx.Pkg

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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