converter

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorSliceTypeNotAlign = errors.New("slice type not aligned")
	ErrorNilParser         = errors.New("parser can not be nil")
	ErrorUnsupportedMap    = errors.New("unsupported map type")
)
View Source
var AppendAllFields = func(tags []string) TagFunc {
	return func(tagKey string) string {
		buf := &bytes.Buffer{}
		buf.WriteString("`")
		for _, tag := range tags {

			buf.WriteString(tag)
			buf.WriteString(`:"`)
			buf.WriteString(tagKey)
			buf.WriteString(`" `)
		}
		buf.WriteString("`")
		return buf.String()
	}
}

Functions

This section is empty.

Types

type Converter

type Converter struct {
	// Parser - using to parse data into Node AST tree
	Parser

	// AppendTags - appending tags to strcut field by its name
	AppendTags TagFunc
	// contains filtered or unexported fields
}

func NewConverter added in v0.1.1

func NewConverter(parser Parser, appendTagFunc TagFunc) *Converter

func (*Converter) Convert added in v0.1.1

func (c *Converter) Convert(pkg, dst string) error

type MapParser added in v0.1.1

type MapParser struct {
	Data map[string]interface{}
}

MapParser - parse map[string]interface{} into Node Tree

func (*MapParser) ParseToNodeTree added in v0.1.1

func (m *MapParser) ParseToNodeTree() (*Node, error)

type Node

type Node struct {
	// inner node properties
	// the tag key of the node
	TagKey string
	// the struct field name
	FieldName string
	// the node type: string, bool, int64, float64, Slice, Map and so on
	Type reflect.Kind

	// sort with alphabeta
	Child []Node
}

type Parser added in v0.1.1

type Parser interface {
	ParseToNodeTree() (*Node, error)
}

func NewMapParser added in v0.1.1

func NewMapParser(data map[string]interface{}) Parser

type TagFunc added in v0.1.1

type TagFunc func(tagKey string) (tags string)

TagFunc - add tags for the specific field for example, add mapstructure tags for all field, the AddTag is like:

func mapstructureForAll (tagKey string) string {
		return fmt.Sprintf("`%s:\"%s\"`", tagKey, "mapstructure")
}

Jump to

Keyboard shortcuts

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