parser

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2022 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpNode

func DumpNode(node *StructNode)

DumpNode dumps info in the whole tree

func ExtractPaths

func ExtractPaths(paths map[string]*StructNode) []string

ExtractPaths extracts all keys in the paths map and adds them to an array.

func NodesToParameterMap

func NodesToParameterMap(node *StructNode,
	paths map[string]*StructNode, filter *support.FieldFilters, tags []string)

NodesToParameterMap grabs all tag FullNames on nodes that do have at least one tag in the StructNode.Tag property. The tags full name is the associated with the node itself. This is to gain a more accessable structure to search for nodes. Note if multiple tag FullName are present for same StructNode, multiple entries in the paths map will be created, one per tag.FullName.

func RenderPrefix

func RenderPrefix(prefix string, env string, svc string) string

RenderPrefix renders a prefix based on the in param strings

Types

type Parser

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

Parser parses struct(s) and produces a tree of nodes along with fields, values and possibly tags.

func New

func New(service string, environment string, prefix string) *Parser

New creates a new instrance of the Parser

When a _prefix_ is passed, it acts as a default prefix if no prefix is specified in the tag. Prefix operates under two modes _Local_ and _Global_.

.Local vs Global Mode [cols="1,1,4"] |=== |Mode |Example |Description

|Local |my-local-prefix/nested |This will render environment/service/my-local-prefix/nested/property. E.g. dev/tes-service/my-local-prefix/nested/password

|Global |/my-global-prefix/nested |This will render environment/my-global-prefix/nested/property. E.g. dev/my-global-prefix/nested/password

|===

NOTE: When global prefix, the _service_ element is eliminated (in order to have singeltons).

func (*Parser) Parse

func (p *Parser) Parse(v reflect.Value) (*StructNode, error)

Parse will parse the in param value. It may either be a type such as var s MyStruct or a instance such as s := MyStruct{...} and then do reflect.ValueOf(&s) and send that to Parse.

func (*Parser) RegisterTagParser

func (p *Parser) RegisterTagParser(tag string, parser TagParser) *Parser

RegisterTagParser registers a tag parser that parses a specified tag.

type StructNode

type StructNode struct {
	// FqName is the fully qualified field name. If
	// within a substruct it will have a dotted path.
	// For example Address.Street. This means that
	// Street is this node and it is within a sub-struct
	// on a field called Address.
	FqName string
	// Type is the the struct type
	Type reflect.Type
	// Field is the field value (not set if root node)
	Field reflect.StructField
	// Tag is the parsed tags (if sub-struct attr will be nil)
	// each tag is mapped to the registered tag name e.g. pms, asm etc.
	Tag map[string]StructTag
	// Childs is any child nodes (if sub-struct)
	Childs []StructNode
	// Owner is the owning node (nil if root node) that either
	// owns the scalar field or a sub-struct
	Owner *StructNode
	// Value is used if sub-/root struct
	Value reflect.Value
}

StructNode is a node representing a struct or a field in a struct.

func (*StructNode) EnsureInstance

func (s *StructNode) EnsureInstance(children bool)

EnsureInstance ensures that value part is set If the embedded reflect.Value is a pointer type and it is nil. It will create a new instance and assing the it to the pointer. All this is done through reflection.

func (*StructNode) HasChildren

func (s *StructNode) HasChildren() bool

HasChildren returns true if this node has children

func (*StructNode) HasTag

func (s *StructNode) HasTag(tag string) bool

HasTag checks if in param tag name exists int the Tag map

func (*StructNode) ToString

func (s *StructNode) ToString(children bool) string

ToString renders the node

type StructTag

type StructTag interface {
	// Named is the named parametres indexed by name
	// If not key = value, instead just a value
	// the key is "" and the value is the value.
	GetNamed() map[string]string
	// The other key = values where not special
	// meaning
	GetTags() map[string]string
	// FullName is a fully qualified name separated
	// with slashes. This is a combination of a
	// prefix and the Named["name"] of this StructTag.
	GetFullName() string
}

StructTag is a interface struct that represents a tag with parameters

type StructTagImpl

type StructTagImpl struct {
	// Gets the named parametres indexed by name
	// If not key = value, instead just a value
	// the key is "" and the value is the value.
	Named map[string]string
	// The other key = values where not special
	// meaning
	Tags map[string]string
	// FullName is a fully qualified name separated
	// with slashes. This is a combination of a
	// prefix and the Named["name"] of this StructTag.
	FullName string
}

StructTagImpl is a generic struct that represents a tag with parameters

func (*StructTagImpl) GetFullName

func (t *StructTagImpl) GetFullName() string

GetFullName is the fully qualified name, i.e. prefix + name

func (*StructTagImpl) GetNamed

func (t *StructTagImpl) GetNamed() map[string]string

GetNamed gets the named parametres indexed by name. If not key = value, instead just a value the key is "" and the value is the value.

func (*StructTagImpl) GetTags

func (t *StructTagImpl) GetTags() map[string]string

GetTags contains all tags that do not have a special meaning.

type TagParser

type TagParser interface {
	// ParseTagString is implemented by the user of the parser. The tagstring is the complete
	// tag string for the registered tag name. The parameter prefix is the navigation
	// when having sub struct separated with slashes. In this way the parser may
	// add it as prefix on some occasions. The env is the in param environment data and
	// that can be anything. The svc is the service currently using this parser.
	//
	// The ParseTagString returns either a StructTag (struct) or an error.
	ParseTagString(tagstring string, prefix string, env string, svc string) (StructTag, error)
}

TagParser where external caller implements to parse a tag. It is also possible to use a default tag parser within this package. The tag parser has two reserved tags "name" and "prefix" that it must adhere to check the default implementation for the handling of those.

func NewTagParser

func NewTagParser(named []string) TagParser

NewTagParser creates a new default tag parser

Jump to

Keyboard shortcuts

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