genum

package
v0.0.0-...-68323b4 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package genum provides methods used to generate enum golang type based on CVS data.

Index

Constants

View Source
const (
	// Command is the name of the command line to generate golang enums based on CSV data.
	Command = "genum"
	// DefaultType is the default type name of an enum.
	DefaultType = "Enum"
	// DefaultKind is the default base type for an enum.
	DefaultKind = "int"
)
View Source
const (
	// ErrMissing is returned when a data is missing.
	ErrMissing = errGenum("missing data")
)

Variables

This section is empty.

Functions

func DefaultFormat

func DefaultFormat(valueFormat string) string

DefaultFormat returns the format used to return the enum string.

func Generate

func Generate(opts ...Configurator) (err error)

Generate generates the enum file based on these options.

func NameFormat

func NameFormat() string

NameFormat returns the format used to return the enum name.

func TypeFormat

func TypeFormat() string

TypeFormat returns the format used to return the enum type.

Types

type Configurator

type Configurator func(g *Generator) error

Configurator must be implemented by any methods acted as an enum layout generator.

func Layout

func Layout(s Settings, args []string) []Configurator

Layout returns the generation configuration based on the given settings.

func ParseBitmask

func ParseBitmask(data io.Reader, enumType string, joinPrefix, trimPrefix bool) Configurator

ParseBitmask reads the given source as a CSV and tries to create a bitmasks list.

func ParseEnums

func ParseEnums(data io.Reader, enumType string, enumKind Kind, joinPrefix, trimPrefix, useIota bool) Configurator

ParseEnums reads the given source as a CSV and tries to create a list of constants based on it.

func PrintBitmask

func PrintBitmask(enumType string) Configurator

PrintBitmask prints related methods to bitmask operations.

func PrintEnums

func PrintEnums(enumType string, useIota, commented bool) Configurator

PrintEnums prints the list of constants.

func PrintHeader

func PrintHeader(pkg string, args []string, packages map[string]struct{}) Configurator

PrintHeader prints the go file header (package, import, etc.).

func PrintJSONMarshaler

func PrintJSONMarshaler(enumType string, enumKind Kind) Configurator

PrintJSONMarshaler adds methods to marshal and unmarshal the enum value as JSON data.

func PrintLookup

func PrintLookup(enumType string, enumKind Kind) Configurator

PrintLookup adds a private method dedicated to get if exists the name of a constant with ok at true. Otherwise, a standard failover name with ok at false are returned.

func PrintStringer

func PrintStringer(format string, enumType string, enumKind Kind) Configurator

PrintStringer chooses the "best" methods regarding the data to manage the String method.

func PrintTextMarshaler

func PrintTextMarshaler(format string, enumType string) Configurator

PrintTextMarshaler adds methods to marshal and unmarshal the enum String value as a text.

func PrintValidator

func PrintValidator(enumType string) Configurator

PrintValidator builds a method to check the validity of a constant.

func PrintXMLMarshaler

func PrintXMLMarshaler(enumType string, enumKind Kind) Configurator

PrintXMLMarshaler adds methods to marshal and unmarshal the enum value as XML data.

func WriteFile

func WriteFile(filename string) Configurator

WriteFile tries to write the go file.

type Enum

type Enum struct {
	Iota    string
	Kind    Kind
	Text    string
	RawText string
	Type    string
	Value   string
}

Enum represents an Enum.

func (Enum) Format

func (e Enum) Format(pos int, useIota, commented bool) string

Format formats the constant regarding to its context (iota, position, etc.)

Enum Kind = iota // e = 0 Enum Enum Kind = iota + 100 _ Kind = iota _ Enum Kind = "rv" Non-exhaustive list.

func (Enum) ParseValue

func (e Enum) ParseValue() (interface{}, error)

ParseValue tries to parse the Value as expected by its Kind.

type Generator

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

Generator represents an enum generator.

type Kind

type Kind uint8

Kind represents a golang type.

const (
	Int Kind = iota
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64
	Float32
	Float64
	String
)

List of supported kinds.

func KindNamed

func KindNamed(s string) Kind

KindNamed converts s to a Kind.

func KindUnsigned

func KindUnsigned(max int) Kind

KindUnsigned returns an integer unsigned large enough to hold this max value.

func (Kind) BitSize

func (k Kind) BitSize() int

BitSize returns the bit size of the type.

func (Kind) Cast

func (k Kind) Cast(data string) string

Cast wraps the data with underlying data type.

func (Kind) IsInteger

func (k Kind) IsInteger() bool

IsInteger returns true if the type is an integer.

func (Kind) IsNumber

func (k Kind) IsNumber() bool

IsNumber returns true if the type is a number (float or integer).

func (Kind) IsSigned

func (k Kind) IsSigned() bool

IsSigned returns true if the type is a signed number.

func (Kind) Name

func (k Kind) Name() string

Name returns the Kind name.

func (Kind) String

func (i Kind) String() string

func (Kind) ValueFormat

func (k Kind) ValueFormat() string

ValueFormat returns the formats used to output a Kind value.

type Pos

type Pos int

Pos represents a data position.

const (
	NamePos Pos = iota + 1
	ValuePos
	TypePos
)

Positions of each data in the String formater.

func (Pos) String

func (p Pos) String() string

String implements the fmt.Stringer interface.

type Settings

type Settings interface {
	DstFilename() string
	SrcFile() io.Reader
	PackageName() string
	TypeName() string
	TypeKind() Kind
	Bitmask() bool
	Commented() bool
	JoinPrefix() bool
	TrimPrefix() bool
	Validator() bool
	Iota() bool
	JSONMarshaler() bool
	TextMarshaler() bool
	XMLMarshaler() bool
	Stringer() bool
	StringFormater() string
}

Settings must be implemented by any service wanted to share generation configuration.

Jump to

Keyboard shortcuts

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