types

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	InvalidKindErr       = "kind not supported"
	RootKindErr          = "root type must be a struct"
	CyclicalReferenceErr = "cyclical reference"
	NilInterfaceErr      = "interface element is nil"
	EmptyStructErr       = "empty struct not supported"
	EmptyMapErr          = "empty map not supported"
	NoExportedFieldsErr  = "struct has no exported fields"
	MapKeyTypeErr        = "map key type must be string"
	SliceMultiTypeErr    = "slice contains multiple kinds"
	DuplicateMapKeyErr   = "duplicate map key"
)

Errors for type reflection.

View Source
const (
	ROOT_NAME    = "Root"
	TYPEREF_NAME = "TypeRef"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AncestorTypeRef

type AncestorTypeRef map[string]int

AncestorTypeRef keeps track of type references that are ancestors of the current element. - Stores a count of references found. - If count > 1, a cyclical reference exists.

func NewAncestorTypeRef

func NewAncestorTypeRef() AncestorTypeRef

NewAncestorTypeRef initializes a new ancestor list.

func (AncestorTypeRef) Add

func (a AncestorTypeRef) Add(key string) int

Add adds a reference count to the ancestor list.

func (AncestorTypeRef) Contains

func (a AncestorTypeRef) Contains(key string) bool

Contains returns true if the key exists in ancestor list.

func (AncestorTypeRef) Copy

Copy makes a copy of the ancestor list.

type NativeOption

type NativeOption map[string]string

NativeOption stores options as key-value pairs but returns a list of strings. - Value-only entries are unique by value. - Values with keys are unique by key.

func NewNativeOption

func NewNativeOption() NativeOption

func (NativeOption) AddBool

func (n NativeOption) AddBool(key string, val bool)

AddBool adds a boolean as an option string. - key is required

  • if key is empty, nothing is added

- val is boolean value

func (NativeOption) AddKeyVal

func (n NativeOption) AddKeyVal(key, val string)

AddKeyVal adds an option string key=val

func (NativeOption) AddThreeFlag

func (n NativeOption) AddThreeFlag(key string, val threeflag.ThreeFlag)

AddThreeFlag adds a ThreeFlag value as a string. - key is required

  • if key is empty, nothing is added

- val is ThreeFlag value

func (NativeOption) AddVal

func (n NativeOption) AddVal(val string)

AddVal adds an option value string.

func (NativeOption) AsList

func (n NativeOption) AsList() []string

AsList returns options as a slice of strings.

func (NativeOption) Copy

func (n NativeOption) Copy() NativeOption

Copy makes a copy of the NativeOption.

func (NativeOption) Delete

func (n NativeOption) Delete(key string)

Delete removes an entry from the option map. - key will match either key-value pairs or value-only settings.

func (NativeOption) Equals

func (n NativeOption) Equals(other NativeOption) bool

Equals returns true if both NativeOption struct have the same values.

func (NativeOption) UpdateFrom

func (n NativeOption) UpdateFrom(other NativeOption)

UpdateFrom updates with values from another NativeOption.

type NativeType

type NativeType struct {
	// Name of language of dialect represented by NativeType.
	Dialect string

	// Name of element if different from generic Name.
	Name string `json:",omitempty"`

	// Native type of element if different from the generic Type.
	Type string `json:",omitempty"`

	// TypeRef holds the native name of a type if different from the generic TypeRef.
	TypeRef string `json:",omitempty"`

	// Include indicates whether an element should be included in output for a dialect.
	// Include has three value values:
	// - "" (empty string) means value is not set
	// - "yes" = include element in output
	// - "no" = exclude element from output
	Include threeflag.ThreeFlag

	// Options contains a list of strings representing dialect-specific options.
	// - Format is one of:
	//   - "value"
	//   - "key=value"
	Options NativeOption

	// Capture error if element cannot reflect.
	Error string `json:",omitempty"`
}

NativeType holds key-value attributes specific to one dialect. - A dialect is the name of a language (e.g. golang) or implementation (e.g. json-schema)

func NewNativeType

func NewNativeType(dialect string) *NativeType

NewNativeType initializes a new NativeType with default settings.

func (*NativeType) AsMap

func (n *NativeType) AsMap() map[string]string

AsMap returns a map[string]string representation of the NativeType struct.

func (*NativeType) Copy

func (n *NativeType) Copy() *NativeType

Copy makes a copy of a NativeType.

func (*NativeType) UpdateFromTag

func (n *NativeType) UpdateFromTag(t *StructFieldTag)

UpdateFromTag sets NativeType fields from a StructFieldTag.

type Schema

type Schema struct {
	// Root is the node ID of the root of types in the order found.
	Root *TypeNode

	// TypeRef is the node ID that holds a map of named types by name.
	TypeRef *TypeNode
}

Schema is the result of parsing types.

func NewSchema added in v0.0.6

func NewSchema(nativeDialect string) *Schema

NewSchema initializes a new schema with root nodes.

func (*Schema) CopyWithoutNative added in v0.0.10

func (schema *Schema) CopyWithoutNative() *Schema

CopyWithoutNative removes all native dialects for the minimal schema.

type StructFieldTag

type StructFieldTag struct {
	Ignore  bool
	Alias   string
	Options NativeOption
}

StructFieldTag stores attributes of a struct field tag.

Tags are parsed as follows: - tag="-" --> ignored field, Ignore=true - tag="someString" --> alias only, Alias = "someString" - tag="someString,options" --> alias with options, Alias="someString", Options=remainder after the first comma - If tag = "-", Ignore is true -->

func NewStructFieldTag

func NewStructFieldTag(tag string) *StructFieldTag

NewStructFieldTag parses the contents of tag string to initialize a StructFieldTag. - Reference for common tags: https://zchee.github.io/golang-wiki/Well-known-struct-tags/

Tags always follow the pattern: <alias>,<comma-delimited options> - if tag string is "-", field is ignored - either <alias> or <options> can be omitted - if <options> is empty, the comma may be omitted

func (*StructFieldTag) Equals

func (s *StructFieldTag) Equals(other *StructFieldTag) bool

Equals returns true if two StructFieldTag structs have the same values.

type Tags

type Tags map[string]*StructFieldTag

Tags stores struct tags by tag name.

func ParseTags

func ParseTags(tag reflect.StructTag) Tags

ParseTags Parsing code is derived from: go/src/reflect/type.go --> Lookup()

type TypeList

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

TypeList holds a slice of TypeElements. - Behavior is similar to a stack with Push/Pop methods to add/remove elements from the end

func NewTypeList

func NewTypeList() *TypeList

func (*TypeList) Copy

func (typeList *TypeList) Copy(parentElem *TypeNode) *TypeList

Copy makes a copy of the current TypeList. - Parent is set if parentElem is not nil.

func (*TypeList) Elements

func (typeList *TypeList) Elements() []*TypeNode

Elements returns the internal slice of TypeElements.

func (*TypeList) Len

func (typeList *TypeList) Len() int

Len returns the number of elements in the TypeList.

func (*TypeList) Pop

func (typeList *TypeList) Pop() *TypeNode

Pop removes the last element from the list an returns it. - Returns nil is list is empty.

func (*TypeList) Push

func (typeList *TypeList) Push(elem *TypeNode)

Push adds an element to the list.

type TypeNode added in v0.0.6

type TypeNode struct {
	// Optional Name and Description of element.
	// - Name applies to struct/map types with string keys.
	Name        string `json:",omitempty"`
	Description string `json:",omitempty"`

	// Nullable indicates that a field should accept null in addition to values.
	Nullable bool `json:",omitempty"`

	// Generic type of element.
	Type string

	// TypeRef holds the name of a type (e.g. struct)
	TypeRef string `json:",omitempty"`

	// NativeDialect is the name of the dialect that was the source for the schema.
	NativeDialect string `json:",omitempty"`

	// Native type features by dialect name.
	Native map[string]*NativeType `json:",omitempty"`

	// Capture error if element cannot reflect.
	Error string `json:",omitempty"`

	// MetaKey is a tag attached to a top-level node during schema derivation.
	// This can be used to attach additional metadata during rendering.
	MetaKey string `json:",omitempty"`

	// Pointers to Parent and Child ID strings.
	Parent   *TypeNode   `json:"-"`
	Children []*TypeNode `json:",omitempty"`
}

TypeNode holds type information about an element. - TypeNode should be cross-platform and only use basic types.

func NewRootNode added in v0.0.10

func NewRootNode(name, dialect string) *TypeNode

NewRootNode creates a new type element that is a root of a tree.

func NewTypeNode added in v0.0.10

func NewTypeNode(name, dialect string) *TypeNode

NewTypeNode returns a new TypeNode in the current NodePool.

func (*TypeNode) AddChild added in v0.0.6

func (t *TypeNode) AddChild(childElem *TypeNode)

AddChild adds a child element to the current element. - Sets Parent on the child element.

func (*TypeNode) Ancestors added in v0.0.6

func (t *TypeNode) Ancestors() []*TypeNode

Ancestors returns a slice of all ancestors of the given TypeNode.

func (*TypeNode) ChildByName added in v0.0.6

func (t *TypeNode) ChildByName(name string, m map[string]*TypeNode) *TypeNode

ChildByName gets the child with the given element name. - Returns nil if child does not exist.

func (*TypeNode) ChildKeys added in v0.0.6

func (t *TypeNode) ChildKeys(m map[string]*TypeNode) []string

ChildKeys returns a sorted list of child names.

func (*TypeNode) ChildMap added in v0.0.6

func (t *TypeNode) ChildMap() map[string]*TypeNode

ChildMap returns a map of Children name --> *TypeNode - Output map can be passed to ChildKeys, ContainsChild, ChildByName for reuse.

func (*TypeNode) ContainsChild added in v0.0.6

func (t *TypeNode) ContainsChild(name string, m map[string]*TypeNode) bool

ContainsChild returns true if a child with the given name exist.

func (*TypeNode) Copy added in v0.0.6

func (t *TypeNode) Copy() *TypeNode

Copy makes a copy of a TypeNode and its Children. - The copied element has no Parent.

func (*TypeNode) CopyWithoutNative added in v0.0.10

func (t *TypeNode) CopyWithoutNative() *TypeNode

CopyWithoutNative makes a copy of a TypeNode and its Children without Native types. - The copied element has no Parent.

func (*TypeNode) GetName added in v0.0.6

func (t *TypeNode) GetName(dialect string) string

GetName returns the alias for the given lang or Name.

func (*TypeNode) GetNativeType added in v0.0.6

func (t *TypeNode) GetNativeType(dialect string) *NativeType

GetNativeType returns a new NativeType with Name,Type,TypeRef,Include set.

func (*TypeNode) IsBasicType added in v0.0.6

func (t *TypeNode) IsBasicType() bool

IsBasicType returns true if the element is a basic type.

func (*TypeNode) IsExported added in v0.0.6

func (t *TypeNode) IsExported() bool

IsExported returns true if the element Name starts with an uppercase letter.

func (*TypeNode) MapKey added in v0.0.7

func (t *TypeNode) MapKey() string

MapKey returns a key for a TypeNode as the first, non-empty value of: - Name - MetaKey - ID

func (*TypeNode) NativeDefault added in v0.0.6

func (t *TypeNode) NativeDefault() *NativeType

NativeDefault returns the native element for the NativeDialect.

func (*TypeNode) NewChild added in v0.0.6

func (t *TypeNode) NewChild(name string) *TypeNode

NewChild creates a new type element that is a child of the current one.

func (*TypeNode) RemoveAllChildren added in v0.0.6

func (t *TypeNode) RemoveAllChildren()

RemoveAllChildren removes all children from the current element.

func (*TypeNode) RemoveChild added in v0.0.6

func (t *TypeNode) RemoveChild(childElem *TypeNode)

RemoveChild removes the given child from the Children list. - Uses ID for matching. - Sets Parent on child to nil.

func (*TypeNode) RemoveParent added in v0.0.6

func (t *TypeNode) RemoveParent()

RemoveParent removes the Parent.

func (*TypeNode) SetName added in v0.0.6

func (t *TypeNode) SetName(dialect, alias string)

SetName sets the GetName for the native dialect.

func (*TypeNode) SetParent added in v0.0.6

func (t *TypeNode) SetParent(p *TypeNode)

SetParent set the Parent and ParentID.

Jump to

Keyboard shortcuts

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