json_map

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2021 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Contains the JsonMapInt interface which should be used in signatures as well as constants and types relating to AbsolutePaths and JSON path parsing to AbsolutePaths.

Index

Examples

Constants

This section is empty.

Variables

View Source
var AbsolutePathKeyTypeNames = map[AbsolutePathKeyType]string{
	StringKey:       "StringKey",
	IndexKey:        "IndexKey",
	Wildcard:        "Wildcard",
	Filter:          "Filter",
	First:           "First",
	Slice:           "Slice",
	StartEnd:        "StartEnd",
	RecursiveLookup: "RecursiveLookup",
}

Map of absolute key type values to their corresponding names.

Used in String method of AbsolutePathKey.

Functions

This section is empty.

Types

type AbsolutePathKey

type AbsolutePathKey struct {
	// An AbsolutePathKeyType which represents what type of descent is occurring.
	KeyType AbsolutePathKeyType
	// An associated value.
	//
	// Non-nil for StringKey, IndexKey, Filter, Slice and RecursiveLookup. Types can be inferred from the KeyType.
	Value interface{}
}

An absolute path key with a KeyType and a Value.

func (AbsolutePathKey) String

func (apk AbsolutePathKey) String() string

type AbsolutePathKeyType

type AbsolutePathKeyType int

Represents a type of a key within an AbsolutePath.

const (
	// Represents descent down a key in a map.
	StringKey AbsolutePathKeyType = iota
	// Represents descent down an index in an array.
	IndexKey AbsolutePathKeyType = iota
	// Represents descent down all key-value pairs in a map or elements in an array.
	Wildcard AbsolutePathKeyType = iota
	// Represents a filter expression on a map or an array.
	Filter AbsolutePathKeyType = iota
	// Represents a lexicographically first descent.
	First AbsolutePathKeyType = iota
	// Represents list slice notation (range of elements within a list).
	Slice AbsolutePathKeyType = iota
	// Represents a start or an end within a slice.
	//
	// Note: The following should only be used within a Slice AbsolutePathKey's Value.
	StartEnd AbsolutePathKeyType = iota
	// Represents a recursive lookup for a given property.
	RecursiveLookup AbsolutePathKeyType = iota
)

All the key types that can be added to an absolute path.

type AbsolutePaths

type AbsolutePaths [][]AbsolutePathKey

Type representing a list of absolute paths.

Used as an intermediary for calculating JSON paths.

Example

Constructing an AbsolutePaths value using AddToAll.

absolutePaths := make(AbsolutePaths, 0)

// We add a RecursiveLookup key, this will initialise the root of the AbsolutePaths.
absolutePaths.AddToAll(nil, false,
	AbsolutePathKey{
		KeyType: RecursiveLookup,
		Value:   "property",
	},
)

// Then we add the 0, 1 and 2 indices.
// Note: We have to add them using separate calls. This is because we want to create a new path for each index added.
absolutePaths.AddToAll(nil, false,
	AbsolutePathKey{
		KeyType: IndexKey,
		Value:   0,
	},
	AbsolutePathKey{
		KeyType: IndexKey,
		Value:   1,
	},
	AbsolutePathKey{
		KeyType: IndexKey,
		Value:   2,
	},
)

// Then we add the string key.
absolutePaths.AddToAll(nil, false,
	AbsolutePathKey{
		KeyType: StringKey,
		Value:   "name",
	},
)

fmt.Println(absolutePaths)
Output:

[[|RecursiveLookup: property| |IndexKey: 0| |StringKey: name|] [|RecursiveLookup: property| |IndexKey: 1| |StringKey: name|] [|RecursiveLookup: property| |IndexKey: 2| |StringKey: name|]]

func ParseJsonPath

func ParseJsonPath(jsonPath string) (absolutePaths AbsolutePaths, err error)

Given a JSON path will return the list of absolute paths to each value pointed to by that JSON path.

This does NOT check if the JSON path is valid.

Example

Parsing a JSON path string into an AbsolutePaths type.

path, _ := ParseJsonPath("$..property[0, 1, 2].name")
fmt.Println(path)
Output:

[[|RecursiveLookup: property| |IndexKey: 0| |StringKey: name|] [|RecursiveLookup: property| |IndexKey: 1| |StringKey: name|] [|RecursiveLookup: property| |IndexKey: 2| |StringKey: name|]]

func (*AbsolutePaths) AddToAll

func (p *AbsolutePaths) AddToAll(jsonMap JsonMapInt, check bool, pathValues ...AbsolutePathKey) (errs []error)

Adds the given value to the end of each absolute path in the AbsolutePaths list.

If check is true then the given jsonMap will be checked if all paths can be reached within the context of the map. jsonMap can be nil if check is false.

type JsonMapInt

type JsonMapInt interface {
	// Return a clone of the JsonMap. If clear is given then New will be called but "Array" field will be inherited.
	Clone(clear bool) JsonMapInt
	// Finds all the script and non-script fields within a JsonMap.
	FindScriptFields() (found bool)
	// Returns the current scopes JSON Path to itself.
	GetCurrentScopePath() string
	// Getter for insides.
	GetInsides() *map[string]interface{}
	// Given the list of absolute paths for a JsonMap, will return the list of values that said paths lead to.
	GetAbsolutePaths(absolutePaths *AbsolutePaths) (values []*JsonPathNode, errs []error)
	// Checks whether the JsonMap is an array at its root.
	IsArray() bool
	// Given a valid JSON path will return the list of pointers to json_map.JsonPathNode(s) that satisfies the JSON path.
	JsonPathSelector(jsonPath string) (out []*JsonPathNode, err error)
	// Given a valid JSON path: will set the values pointed to by the JSON path to be the value given.
	JsonPathSetter(jsonPath string, value interface{}) (err error)
	// Adds the given script of the given shebangName (must be a supported language) at the path pointed to by the given jsonPath.
	MarkupCode(jsonPath string, shebangName string, script string) (err error)
	// Marshal a JsonMap back into JSON.
	Marshal() (out []byte, err error)
	// A wrapper for MustSet(jsonPath, nil).
	MustDelete(jsonPath string)
	// Like JsonPathSelector, only it panics when an error occurs and returns an []interface{} instead of []json_map.JsonPathNode.
	MustGet(jsonPath string) (out []interface{})
	// Pops from an []interface{} indicated by the given JSON path at the given indices and panics if any errors occur.
	MustPop(jsonPath string, indices ...int) (popped []interface{})
	// Pushes to an []interface{} indicated by the given JSON path at the given indices and panics if any errors occur.
	MustPush(jsonPath string, value interface{}, indices ...int)
	// Like JsonPathSetter, only it panics when an error occurs.
	MustSet(jsonPath string, value interface{})
	// Given a JsonMap this will traverse it and execute all scripts. Will update the given JsonMap in place.
	Run()
	// Given the list of absolute paths for a JsonMap: will set the values pointed to by the given JSON path to be the given value.
	SetAbsolutePaths(absolutePaths *AbsolutePaths, value interface{}) (err error)
	// Strips any script key-value pairs found within the JsonMap and updates it in place.
	Strip()
	// Marshals the JsonMap into hjson and returns the stringified byte array.
	String() string
	// Unmarshal a hjson byte string and package it as a JsonMap.
	Unmarshal(jsonBytes []byte) (err error)
}

Acts as an interface for jom.JsonMap.

Primarily created to stop cyclic imports.

type JsonPathNode

type JsonPathNode struct {
	// The absolute path to the node.
	Absolute []AbsolutePathKey
	// The value of the node.
	Value interface{}
}

Stores a node within a JsonMapInt.

Jump to

Keyboard shortcuts

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