node

package module
v0.0.0-...-814f756 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2023 License: MIT Imports: 6 Imported by: 0

README

node

A go structure for handling dynamic JSON.

Feature List

  • Primary Goal: Unmarshal JSON into walkable structure(s) for easier access to data. (i.e. no predefining structure(s) or unmarshaling into a map of string interface{} or any)
  • Secondary Goal: Support Marshaling into valid JSON (the order may differ) but remain technically the same JSON payload.

How it works

  1. We unmarshal the JSON into a map[string]any (or map[string]interface{} as any is an alias for interface{})
  2. Then we walk thru each element calling reflect to determine the dynamic type under run-time: *
    • Maps/Maplikes (map[string]any/map[string]interface{}) will be walked thru determining it's dynamic type *
    • Arrays/Slices/Arraylikes ([]any/[]interface{}) will be walked thru determining it's dynamic type *
    • Strings/Integers/Floats/Values (any/interface{}) will be assigned to either a named structure or a un-named structure (un-named are used when the previous depth or "parent" is determined to be Arraylike (Arrays/Slices), while named structures are used when the previous depth or "parent" is determined to be Maplike (Maps))
  3. Once the structures have been populated, all the JSON has technically been walked over, and thus the Node tree or JSON structure is now available.

* This process is recursive, it will iterate over it's elements determining their type and can possibly cause it to iterate over that.

This concept uses pointers to structures heavily for a few reasons:

  • Pointers are lighter than structures (as they are just pointing to them, so they exist in memory only once, with 1 or more pointers to them)
  • Pointers are like C++'s "Reference" (we can modify the "parent" or "children" without fear of copies and other ugly memory issues)
  • So long as there is 1 reference to the pointer, it won't be freed/forgotten (This is quite similar to all the "parent" and "children" being C++ "Smart Pointers" in that they keep a "reference count", when it's 0, then it's released)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

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

func NewNode

func NewNode() *Node

func NewNodeFromFile

func NewNodeFromFile(name string) (*Node, error)

func NewNodeFromJSON

func NewNodeFromJSON(data []byte) (*Node, error)

func NewNodeFromStream

func NewNodeFromStream(r io.Reader) (*Node, error)

func NewNodeWithData

func NewNodeWithData(data any) *Node

func NewNodeWithDataAndName

func NewNodeWithDataAndName(data any, name string) *Node

func NewNodeWithName

func NewNodeWithName(name string) *Node

func NewNodeWithNameAndData

func NewNodeWithNameAndData(name string, data any) *Node

func (*Node) AddKid

func (n *Node) AddKid(kid *Node) bool

func (*Node) Data

func (n *Node) Data(data ...any) any

func (*Node) Depth

func (n *Node) Depth() int

func (*Node) Destroy

func (n *Node) Destroy()

func (*Node) Detach

func (n *Node) Detach() bool

func (*Node) Index

func (n *Node) Index() int

func (*Node) IsMapLike

func (n *Node) IsMapLike() bool

Checks if the children of this node are better represented as a map or an array

This is determined by:

* Totaling number of children without names

* Comparing the percent of children without names to total children (if less than 50% use a map, if more than 50% use an array)

func (*Node) Kid

func (n *Node) Kid(index int) *Node

func (*Node) KidByName

func (n *Node) KidByName(name string, recurse ...bool) *Node

func (*Node) Kids

func (n *Node) Kids() []*Node

func (*Node) Len

func (n *Node) Len() int

func (*Node) MarshalJSON

func (n *Node) MarshalJSON() ([]byte, error)

Marshals the results from ToMap

func (*Node) Name

func (n *Node) Name(name ...string) string

func (*Node) NewKid

func (n *Node) NewKid() *Node

func (*Node) NewKidWithData

func (n *Node) NewKidWithData(data any) *Node

func (*Node) NewKidWithDataAndName

func (n *Node) NewKidWithDataAndName(data any, name string) *Node

func (*Node) NewKidWithName

func (n *Node) NewKidWithName(name string) *Node

func (*Node) NewKidWithNameAndData

func (n *Node) NewKidWithNameAndData(name string, data any) *Node

func (*Node) Parent

func (n *Node) Parent(parent ...*Node) *Node

func (*Node) RemoveAllKids

func (n *Node) RemoveAllKids()

func (*Node) RemoveKid

func (n *Node) RemoveKid(index int) bool

func (*Node) Root

func (n *Node) Root() *Node

func (*Node) ToMap

func (n *Node) ToMap() any

func (*Node) UnmarshalJSON

func (n *Node) UnmarshalJSON(pay []byte) error

Unmarshal function

Jump to

Keyboard shortcuts

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