anytype

package module
v0.0.0-...-0aefbd8 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: LGPL-2.1 Imports: 12 Imported by: 1

README

AnyType

AnyType is a Go library providing dynamic data structures with JSON support. It contains a number of advanced features with API inspired by Java collections.

It supports following data types compatible with JSON standard:

  • string
  • integer (number)
  • float (number)
  • boolean
  • object
  • list (array)
  • nil (null)

Types can be referenced by the Type enum (e.g. anytype.TypeObject, anytype.TypeInt, ...).

Objects

Object is an unordered set of key-value pairs. The default implementation is based on built-in Go maps. It is possible to make custom implementations by implementing the Object interface.

Constructors
  • NewObject(values ...any) Object - initial object values are specified as key value pairs. The function panics if an odd number of arguments is given,
emptyObject := anytype.NewObject()
object := anytype.NewObject(
    "first", 1,
    "second", 2, 
)
  • NewObjectFrom(dict any) Object - object can be also created from a given Go map. Any map with string as a key and a compatible type as a value can be used,
object := anytype.NewObjectFrom(map[string]int{
	"first":  1,
	"second": 2,
})
  • ParseObject(json string) (Object, error) - loads an object from a JSON string,
object, err := anytype.ParseObject(`{"first":1,"second":2}`)
if err != nil {
    // ...
}
  • ParseFile(path string) (Object, error) - loads an object from an UTF-8 encoded JSON file.
object, err := anytype.ParseFile("file.json")
if err != nil {
    // ...
}
Manipulation With Fields
  • Set(values ...any) Object - multiple new values can be set as key-value pairs, analogically to the constructor,
object.Set(
    "first", 1,
    "second", 2, 
)
  • Unset(keys ...string) Object - removes the given keys from the object,
object.Unset("first", "second")
  • Clear() Object - removes all keys from the object.
object.Clear()
Getting Fields
  • Universal getter (requires type assertion),
nested := object.Get("nested").(anytype.Object)
list := object.Get("list").(anytype.List)
str := object.Get("str").(string)
boolean := object.Get("boolean").(bool)
integer := object.Get("integer").(int)
float := object.Get("float").(float64)
  • type-specific getters.
nested := object.GetObject("nested")
list := object.GetList("list")
str := object.GetString("str")
boolean := object.GetBool("boolean")
integer := object.GetInt("integer")
float := object.GetFloat("float")
Type Check
  • TypeOf(key string) Type.
if object.TypeOf("integer") == anytype.TypeInt {
    // ...
}
Export
  • String() string - exports the object to a JSON string,
fmt.Println(object.String())
  • FormatString(indent int) string - exports the object to a well-arranged JSON string with the given indentation,
fmt.Println(object.FormatString(4))
  • Dict() map[string]any - exports the object to a Go map,
var dict map[string]any
dict = object.Dict()
  • Keys() List - exports all keys of the object to an AnyType list,
var keys anytype.List
keys = object.Keys()
  • Values() List - exports all values of the object to an AnyType list.
var values anytype.List
values = object.Values()
Features Over Whole Object
  • Clone() Object - performs a deep copy of the object,
copy := object.Clone()
  • Count() int - returns a number of fileds of the object,
for i := 0; i < object.Count(); i++ {
    // ...
}
  • Empty() bool - checks whether the object is empty,
if object.Empty() {
    // ...
}
  • Equals(another Object) bool - checks whether all fields of the object are equal to the fields of another object,
if object.Equals(another) {
    // ...
}
  • Merge(another Object) Object - merges two objects together,
merged := object.Merge(another)
  • Pluck(keys ...string) Object - creates a new object containing only the selected keys from existing object,
plucked := object.Pluck("first", "second")
  • Contains(elem any) bool - checks whether the object contains a value,
if object.Contains(1) {
    // ...
}
  • KeyOf(elem any) string - returns any key containing the given value,
first := object.KeyOf(1)
  • KeyExists(key string) bool - checks whether a key exists in the object.
if object.KeyExists("first") {
    // ...
}
ForEaches
  • ForEach(function func(string, any)) Object - executes a given function over an every field of the object,
object.ForEach(func(key string, value any) {
    // ...
})
  • ForEachValue(function func(any)) Object - ForEach without the key variable within the anonymous function,
object.ForEachValue(func(value any) {
    // ...
})
  • type-specific ForEaches - anonymous function is only executed over values of the corresponding type.
object.ForEachObject(func(object anytype.Object) {
    // ...
})
object.ForEachList(func(list anytype.List) {
    // ...
})
object.ForEachString(func(str string) {
    // ...
})
object.ForEachBool(func(object bool) {
    // ...
})
object.ForEachInt(func(integer int) {
    // ...
})
object.ForEachFloat(func(float float64) {
    // ...
})
Mappings
  • Map(function func(string, any) any) Object - returns a new object with fields modified by a given function,
mapped := object.Map(func(key string, value any) any {
    // ...
	return newValue
})
  • MapValues(function func(any) any) Object - Map without the key variable within the anonymous function,
mapped := object.MapValues(func(value any) any {
    // ...
	return newValue
})
  • type-specific Maps - selects only fields of the corresponding type.
objects := object.MapObjects(func(object anytype.Object) any {
    // ...
	return newValue
})
lists := object.MapLists(func(list anytype.List) any {
    // ...
	return newValue
})
strs := object.MapStrings(func(str string) any {
    // ...
	return newValue
})
integers := object.MapInts(func(integer int) any {
    // ...
	return newValue
})
floats := object.MapFloats(func(float float64) any {
    // ...
	return newValue
})
Asynchronous
  • ForEachAsync(function func(string, any)) Object - performs the ForEach paralelly,
object.ForEachAsync(func(key string, value any) {
    // ...
})
  • MapAsync(function func(string, any) any) Object - performs the Map paralelly.
mapped := object.MapAsync(func(key string, value any) any {
    // ...
	return newValue
})
Tree Form
  • GetTF(tf string) any - returns a value specified by the given tree form string,
value := object.GetTF(".first#2")
  • SetTF(tf string, value any) Object - sets a value on the path specified by the given tree form string.
object.SetTF(".first#2", 2)

Lists

List is an ordered sequence of elements. The default implementation is based on built-in Go slices. It is possible to make custom implementations by implementing the List interface.

Constructors
  • NewList(values ...any) List - initial list elements could be given as variadic arguments,
emptyList := anytype.NewList()
list := anytype.NewList(1, 2, 3)
  • NewListOf(value any, count int) List - creates a list of n repeated values,
list := anytype.NewListOf(nil, 10)
  • NewListFrom(slice any) List - creates a list from a given Go slice. Any slice of a compatible type (including any) can be used,
list := anytype.NewListOf(nil, 10)
  • ParseList(json string) (List, error) - loads a list from a JSON string,
list, err := anytype.ParseList(`[1, 2, 3]`)
if err != nil {
    // ...
}
Manipulation With Elements
  • Add(val ...any) List - adds any amount of new elements to the list,
list.Add(1, 2, 3)
  • Insert(index int, value any) List - inserts new element to a specific position in the list,
list.Insert(1, 1.5)
  • Replace(index int, value any) List - replaces an existing element,
list.Replace(1, "2")
  • Delete(index ...int) List - deletes specified elements,
list.Delete(1, 2)
  • Pop() List - Deletes the last element in the list,
list.Pop()
  • Clear() List - removes all elements from the list.
list.Clear()
Getting Elements
  • Universal getter (requires type assertion),
object := list.Get(0).(anytype.Object)
nested := list.Get(1).(anytype.List)
str := list.Get(2).(string)
boolean := list.Get(3).(bool)
integer := list.Get(4).(int)
float := list.Get(5).(float64)
  • type-specific getters.
object := list.GetObject(0)
nested := list.GetList(1)
str := list.GetString(2)
boolean := list.GetBool(3)
integer := list.GetInt(4)
float := list.GetFloat(5)
Type Check
  • TypeOf(index int) Type.
if list.TypeOf(0) == anytype.TypeInt {
    // ...
}
Export
  • String() string - exports the list to a JSON string,
fmt.Println(list.String())
  • FormatString(indent int) string - exports the list to a well-arranged JSON string with the given indentation,
fmt.Println(list.FormatString(4))
  • Slice() []any - exports the list to a Go slice,
var slice []any
slice = list.Slice()
  • export to type-specific slices - panics if the list is not homogeneous,
var objects []anytype.Object
objects = list.ObjectSlice()
var lists []anytype.List
lists = list.ListSlice()
var strs []string
strs = list.StringSlice()
var bools []bool
bools = list.BoolSlice()
var ints []int
ints = list.IntSlice()
var floats []float64
floats = list.FloatSlice()
Features Over Whole List
  • Clone() List - performs a deep copy of the list,
copy := list.Clone()
  • Count() int - returns a number of elements of the list,
for i := 0; i < list.Count(); i++ {
    // ...
}
  • Empty() bool - checks whether the list is empty,
if list.Empty() {
    // ...
}
  • Equals(another List) bool - checks whether all elements of the list are equal to the elements of another list,
if list.Equals(another) {
    // ...
}
  • Concat(another List) List - concates two lists together,
concated := list.Concat(another)
  • SubList(start int, end int) List - cuts a part of the list,
subList := list.SubList(1, 3)
  • Contains(elem any) bool - checks whether the list contains a value,
if list.Contains("value") {
    // ...
}
  • IndexOf(elem any) int - returns a position of the first occurrence of the given value,
elem := list.IndexOf("value")
  • Sort() List - sorts the elements in the list. List has to be homogeneous and the elements have to be either numeric or strings,
list.Sort()
  • Reverse() List - reverses the list,
list.Reverse()
Checks For Homogeneity
  • AllNumeric() bool - checks if all elements are numbers (either ints or floats),
if list.AllNumeric() {
    // ...
}
  • type-specific asserts.
if list.AllObjects() {
    // ...
} else if list.AllLists() {
    // ...
} else if list.AllStrings() {
    // ...
} else if list.AllBools() {
    // ...
} else if list.AllInts() {
    // ...
} else if list.AllFloats() {
    // ...
} else {
    // ...
}
ForEaches
  • ForEach(function func(int, any)) List - executes a given function over an every element of the list,
list.ForEach(func(index int, value any) {
    // ...
})
  • ForEachValue(function func(any)) List - ForEach without the index variable within the anonymous function,
list.ForEachValue(func(value any) {
    // ...
})
  • type-specific ForEaches - anonymous function is only executed over values of the corresponding type.
list.ForEachObject(func(object anytype.Object) {
    // ...
})
list.ForEachList(func(list anytype.List) {
    // ...
})
list.ForEachString(func(str string) {
    // ...
})
list.ForEachBool(func(object bool) {
    // ...
})
list.ForEachInt(func(integer int) {
    // ...
})
list.ForEachFloat(func(float float64) {
    // ...
})
Mappings
  • Map(function func(int, any) any) List - returns a new list with elements modified by a given function,
mapped := list.Map(func(index int, value any) any {
    // ...
	return newValue
})
  • MapValues(function func(any) any) List - Map without the index variable within the anonymous function,
mapped := list.MapValues(func(value any) any {
    // ...
	return newValue
})
  • type-specific Maps - selects only elements of the corresponding type.
objects := list.MapObjects(func(object anytype.Object) any {
    // ...
	return newValue
})
lists := list.MapLists(func(list anytype.List) any {
    // ...
	return newValue
})
strs := list.MapStrings(func(str string) any {
    // ...
	return newValue
})
integers := list.MapInts(func(integer int) any {
    // ...
	return newValue
})
floats := list.MapFloats(func(float float64) any {
    // ...
	return newValue
})
Reductions
  • Reduce(initial any, function func(any, any) any) any - reduces all elements of the list into a single value,
result := list.Reduce(0, func(sum, value any) any {
	return sum.(int) + value.(int)
})
  • type-specific Reductions - selects only elements of the corresponding type. Return value has to be of the same type.
result := list.ReduceStrings("", func(concated, value string) string {
	return concated + value
})
result := list.ReduceInts(0, func(sum, value int) int {
	return sum + value
})
result := list.ReduceFloats(0, func(sum, value float64) float64 {
	return sum + value
})
Filters
  • Filter(function func(any) bool) List - filters elements in the list based on a condition,
filtered := list.Filter(func(value any) bool {
    // ...
	return condition
})
  • type-specific Filters - filters only elements of the corresponding type.
objects := list.FilterObjects(func(value anytype.Object) bool {
    // ...
	return condition
})
lists := list.FilterLists(func(value anytype.List) bool {
    // ...
	return condition
})
strs := list.FilterStrings(func(value string) bool {
    // ...
	return condition
})
integers := list.FilterInts(func(value int) bool {
    // ...
	return condition
})
floats := list.FilterFloats(func(value float64) bool {
    // ...
	return condition
})
Numeric Operations
  • IntSum() int - computes a sum of all elements in the list if all of them are ints,
sum := list.IntSum()
  • Sum() float64 - works with both numeric types, returns float,
sum := list.Sum()
  • IntProd() int - computes a product of all elements in the list if all of them are ints,
product := list.IntProd()
  • Prod() float64 - works with both numeric types, returns float,
product := list.Prod()
  • Avg() float64 - computes and arithmetic mean of all elements in the list,
average := list.Avg()
  • IntMin() int - returns a minimum value in the list (if all elements are ints),
minimum := list.IntMin()
  • Min() float64 - works with both numeric types, returns float,
minimum := list.Min()
  • IntMax() int - returns a maximum value in the list (if all elements are ints),
maximum := list.IntMax()
  • Max() float64 - works with both numeric types, returns float,
maximum := list.Max()
Asynchronous
  • ForEachAsync(function func(int, any)) List - performs the ForEach paralelly,
list.ForEachAsync(func(index int, value any) {
    // ...
})
  • MapAsync(function func(int, any) any) List - performs the Map paralelly.
mapped := list.MapAsync(func(index int, value any) any {
    // ...
	return newValue
})
Tree Form
  • GetTF(tf string) any - returns a value specified by the given tree form string,
value := list.GetTF("#2.first")
  • SetTF(tf string, value any) List - sets a value on the path specified by the given tree form string.
list.SetTF("#2.first", 2)

Derived Structures

AnyType supports inheritance and method overriding by defining custom structures with object or list embedded in them. As Go uses the embedded pointer as receiver instead of the structure itself, the pointer to the derived structure ("ego pointer") has to be stored using the method Init(ptr Object)/Init(ptr List). When overriding a method, the ego pointer can be obtained with Ego() Object/Ego() List.

// Embeds an object
type Animal struct {
	anytype.Object
	name string
}

func NewAnimal(name string, age int) *Animal {
	ego := &Animal{
		Object: anytype.NewObject(
			"age", age,
		),
		name: name,
	}
	ego.Init(ego) // Ego pointer initialization
	return ego
}

// Method overriding
func (ego *Animal) Clear() anytype.Object {
	fmt.Fprintln(os.Stderr, "fields of an animal cannot be cleared")
	return ego.Ego() // Using stored pointer to return Animal insted of embedded object
}

func (ego *Animal) Breathe() {
	fmt.Println("breathing")
}

// Inherits from animal, adds another field
type Dog struct {
	*Animal
	breed string
}

func NewDog(name string, age int, breed string) *Dog {
	ego := &Dog{
		Animal: NewAnimal(name, age),
		breed:  breed,
	}
	ego.Init(ego) // Ego pointer initialization
	return ego
}

// Method overriding
func (ego *Dog) Unset(keys ...string) anytype.Object {
	fmt.Fprintln(os.Stderr, "fields of a dog cannot be unset")
	return ego.Ego() // Using stored pointer to return Dog insted of object
}

func (ego *Dog) Bark() {
	fmt.Println("woof")
}

func main() {

	dog := NewDog("Rex", 2, "German Shepherd")

	// Methods from both Dog and Animal can be used
	dog.Breathe()
	dog.Bark()

    // Methods of the object can be used, too
	dog.Set("color", "black")

	// Printing the object inside
	fmt.Println(dog.String())

	// Both methods have been overridden
	dog.Unset("age")
	dog.Clear()

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

type List interface {

	/*
		Initializes the ego pointer, which allows deriving.

		Parameters:
		  - ptr - ego pointer.
	*/
	Init(ptr List)

	/*
		Acquires the ego pointer previously set by Init.

		Returns:
		  - ego pointer.
	*/
	Ego() List

	/*
		Adds new elements at the end of the list.

		Parameters:
		  - values... - any amount of elements to add.

		Returns:
		  - updated list.
	*/
	Add(val ...any) List

	/*
		Inserts a new element at the specified position in the list.

		Parameters:
		  - index - position where the element should be inserted,
		  - value - element to insert.

		Returns:
		  - updated list.
	*/
	Insert(index int, value any) List

	/*
		Replaces an existing element with a new one.

		Parameters:
		  - index - position of the element which should be replaced,
		  - value - new element.

		Returns:
		  - updated list.
	*/
	Replace(index int, value any) List

	/*
		Deletes the elements at the specified positions in the list.

		Parameters:
		  - indexes... - any amount of positions of the elements to delete.

		Returns:
		  - updated list.
	*/
	Delete(index ...int) List

	/*
		Deletes the last element in the list.

		Returns:
		  - updated list.
	*/
	Pop() List

	/*
		Deletes all elements in the list.

		Returns:
		  - updated list.
	*/
	Clear() List

	/*
		Acquires the element at the specified position in the list.

		Parameters:
		  - index - position of the element to get.

		Returns:
		  - corresponding value (any type, has to be asserted).
	*/
	Get(index int) any

	/*
		Acquires the object at the specified position in the list.
		Causes a panic if the element has another type.

		Parameters:
		  - index - position of the element to get.

		Returns:
		  - corresponding value asserted as object.
	*/
	GetObject(index int) Object

	/*
		Acquires the list at the specified position in the list.
		Causes a panic if the element has another type.

		Parameters:
		  - index - position of the element to get.

		Returns:
		  - corresponding value asserted as list.
	*/
	GetList(index int) List

	/*
		Acquires the string at the specified position in the list.
		Causes a panic if the element has another type.

		Parameters:
		  - index - position of the element to get.

		Returns:
		  - corresponding value asserted as string.
	*/
	GetString(index int) string

	/*
		Acquires the boolean at the specified position in the list.
		Causes a panic if the element has another type.

		Parameters:
		  - index - position of the element to get.

		Returns:
		  - corresponding value asserted as bool.
	*/
	GetBool(index int) bool

	/*
		Acquires the integer at the specified position in the list.
		Causes a panic if the element has another type.

		Parameters:
		  - index - position of the element to get.

		Returns:
		  - corresponding value asserted as int.
	*/
	GetInt(index int) int

	/*
		Acquires the float at the specified position in the list.
		Causes a panic if the element has another type.

		Parameters:
		  - index - position of the element to get.

		Returns:
		  - corresponding value asserted as float64.
	*/
	GetFloat(index int) float64

	/*
		Gives a type of the element at the specified position in the list.

		Parameters:
		  - index - position of the element.

		Returns:
		  - integer constant representing the type (see type enum).
	*/
	TypeOf(index int) Type

	/*
		Gives a JSON representation of the list, including nested lists and objects.

		Returns:
		  - JSON string.
	*/
	String() string

	/*
		Gives a JSON representation of the list in standardized format with the given indentation.

		Parameters:
		  - indent - indentation spaces (0-10).

		Returns:
		  - JSON string.
	*/
	FormatString(indent int) string

	/*
		Converts the list into a Go slice of empty interfaces.

		Returns:
		  - slice.
	*/
	Slice() []any

	/*
		Converts the list of objects into a Go slice.
		The list has to be homogeneous and all elements have to be objects.

		Returns:
		  - slice.
	*/
	ObjectSlice() []Object

	/*
		Converts the list of lists into a Go slice.
		The list has to be homogeneous and all elements have to be lists.

		Returns:
		  - slice.
	*/
	ListSlice() []List

	/*
		Converts the list of strings into a Go slice.
		The list has to be homogeneous and all elements have to be strings.

		Returns:
		  - slice.
	*/
	StringSlice() []string

	/*
		Converts the list of bools into a Go slice.
		The list has to be homogeneous and all elements have to be bools.

		Returns:
		  - slice.
	*/
	BoolSlice() []bool

	/*
		Converts the list of ints into a Go slice.
		The list has to be homogeneous and all elements have to be ints.

		Returns:
		  - slice.
	*/
	IntSlice() []int

	/*
		Converts the list of floats into a Go slice.
		The list has to be homogeneous and all elements have to be floats.

		Returns:
		  - slice.
	*/
	FloatSlice() []float64

	/*
		Creates a deep copy of the list.

		Returns:
		  - copied list.
	*/
	Clone() List

	/*
		Gives a number of elements in the list.

		Returns:
		  - number of elements.
	*/
	Count() int

	/*
		Checks whether the list is empty.

		Returns:
		  - true if the list is empty, false otherwise.
	*/
	Empty() bool

	/*
		Checks if the content of the list is equal to the content of another list.
		Nested objects and lists are compared recursively (by value).

		Parameters:
		  - another - a list to compare with.

		Returns:
		  - true if the lists are equal, false otherwise.
	*/
	Equals(another List) bool

	/*
		Creates a new list containing all elements of the old list and another list.
		The old list remains unchanged.

		Parameters:
		  - another - a list to append.

		Returns:
		  - new list.
	*/
	Concat(another List) List

	/*
		Creates a new list containing the elements from the starting index (including) to the ending index (excluding).
		If the ending index is zero, it is set to the length of the list. If negative, it is counted from the end of the list.
		Starting index has to be non-negative and cannot be higher than the ending index.

		Parameters:
		  - start - starting index,
		  - end - ending index.

		Returns:
		  - created sub list.
	*/
	SubList(start int, end int) List

	/*
		Checks if the list contains a given element.
		Objects and lists are compared by reference.

		Parameters:
		  - elem - the element to check.

		Returns:
		  - true if the list contains the element, false otherwise.
	*/
	Contains(elem any) bool

	/*
		Gives a position of the first occurrence of a given element.

		Parameters:
		  - elem - the element to check.

		Returns:
		  - index of the element (-1 if the list does not contain the element).
	*/
	IndexOf(elem any) int

	/*
		Sorts elements in the list (ascending).
		The list has to be homogeneous, all elements have to be either strings, ints or floats.

		Returns:
		  - updated list.
	*/
	Sort() List

	/*
		Reverses the order of elements in the list.

		Returns:
		  - updated list.
	*/
	Reverse() List

	/*
		Checks if the list is homogeneous and all of its elements are objects.

		Returns:
		  - true if all elements are objects, false otherwise.
	*/
	AllObjects() bool

	/*
		Checks if the list is homogeneous and all of its elements are lists.

		Returns:
		  - true if all elements are lists, false otherwise.
	*/
	AllLists() bool

	/*
		Checks if the list is homogeneous and all of its elements are strings.

		Returns:
		  - true if all elements are strings, false otherwise.
	*/
	AllStrings() bool

	/*
		Checks if the list is homogeneous and all of its elements are bools.

		Returns:
		  - true if all elements are bools, false otherwise.
	*/
	AllBools() bool

	/*
		Checks if the list is homogeneous and all of its elements are ints.

		Returns:
		  - true if all elements are ints, false otherwise.
	*/
	AllInts() bool

	/*
		Checks if the list is homogeneous and all of its elements are floats.

		Returns:
		  - true if all elements are floats, false otherwise.
	*/
	AllFloats() bool

	/*
		Checks if all elements of the list are numeric (ints or floats).

		Returns:
		  - true if all elements are numeric, false otherwise.
	*/
	AllNumeric() bool

	/*
		Executes a given function over an every element of the list.
		The function has two parameters: index of the current element and its value.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged list.
	*/
	ForEach(function func(int, any)) List

	/*
		Executes a given function over an every element of the list.
		The function has one parameter, value of the current element.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged list.
	*/
	ForEachValue(function func(any)) List

	/*
		Executes a given function over all objects in the list.
		Elements with other types are ignored.
		The function has one parameter, the current object.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged list.
	*/
	ForEachObject(function func(Object)) List

	/*
		Executes a given function over all lists nested in the list.
		Elements with other types are ignored.
		The function has one parameter, the current list.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged list.
	*/
	ForEachList(function func(List)) List

	/*
		Executes a given function over all strings in the list.
		Elements with other types are ignored.
		The function has one parameter, the current string.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged list.
	*/
	ForEachString(function func(string)) List

	/*
		Executes a given function over all bools in the list.
		Elements with other types are ignored.
		The function has one parameter, the current bool.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged list.
	*/
	ForEachBool(function func(bool)) List

	/*
		Executes a given function over all ints in the list.
		Elements with other types are ignored.
		The function has one parameter, the current int.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged list.
	*/
	ForEachInt(function func(int)) List

	/*
		Executes a given function over all floats in the list.
		Elements with other types are ignored.
		The function has one parameter, the current float.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged list.
	*/
	ForEachFloat(function func(float64)) List

	/*
		Copies the list and modifies each element by a given mapping function.
		The resulting element can have a different type than the original one.
		The function has two parameters: current index and value of the current element. Returns empty interface.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new list.
	*/
	Map(function func(int, any) any) List

	/*
		Copies the list and modifies each element by a given mapping function.
		The resulting element can have a different type than the original one.
		The function has one parameter, value of the current element, and returns empty interface.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new list.
	*/
	MapValues(function func(any) any) List

	/*
		Selects all objects from the list and modifies each of them by a given mapping function.
		Elements with other types are ignored.
		The resulting element can have a different type than the original one.
		The function has one parameter, the current object, and returns empty interface.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new list.
	*/
	MapObjects(function func(Object) any) List

	/*
		Selects all nested lists from the list and modifies each of them by a given mapping function.
		Elements with other types are ignored.
		The resulting element can have a different type than the original one.
		The function has one parameter, the current list, and returns empty interface.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new list.
	*/
	MapLists(function func(List) any) List

	/*
		Selects all strings from the list and modifies each of them by a given mapping function.
		Elements with other types are ignored.
		The resulting element can have a different type than the original one.
		The function has one parameter, the current string, and returns empty interface.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new list.
	*/
	MapStrings(function func(string) any) List

	/*
		Selects all ints from the list and modifies each of them by a given mapping function.
		Elements with other types are ignored.
		The resulting element can have a different type than the original one.
		The function has one parameter, the current int, and returns empty interface.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new list.
	*/
	MapInts(function func(int) any) List

	/*
		Selects all floats from the list and modifies each of them by a given mapping function.
		Elements with other types are ignored.
		The resulting element can have a different type than the original one.
		The function has one parameter, the current float, and returns empty interface.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new list.
	*/
	MapFloats(function func(float64) any) List

	/*
		Reduces all elements of the list into a single value.
		The function has two parameters: value returned by the previous iteration and value of the current element. Returns empty interface.
		The list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - computed value.
	*/
	Reduce(initial any, function func(any, any) any) any

	/*
		Reduces all strings in the list into a single string.
		Elements with other types are ignored.
		The function has two parameters: string returned by the previous iteration and current string. Returns string.
		The list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - computed value.
	*/
	ReduceStrings(initial string, function func(string, string) string) string

	/*
		Reduces all ints in the list into a single int.
		Elements with other types are ignored.
		The function has two parameters: int returned by the previous iteration and current int. Returns int.
		The list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - computed value.
	*/
	ReduceInts(initial int, function func(int, int) int) int

	/*
		Reduces all floats in the list into a single float.
		Elements with other types are ignored.
		The function has two parameters: float returned by the previous iteration and current float. Returns float.
		The list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - computed value.
	*/
	ReduceFloats(initial float64, function func(float64, float64) float64) float64

	/*
		Creates a new list containing elements of the old one, satisfying a condition.
		The function has one parameter, value of the current element, and returns bool.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - filtered list.
	*/
	Filter(function func(any) bool) List

	/*
		Creates a new list containing objects of the old one, satisfying a condition.
		Elements with other types are ignored.
		The function has one parameter, current object, and returns bool.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - filtered list.
	*/
	FilterObjects(function func(Object) bool) List

	/*
		Creates a new list containing nested lists of the old one, satisfying a condition.
		Elements with other types are ignored.
		The function has one parameter, current list, and returns bool.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - filtered list.
	*/
	FilterLists(function func(List) bool) List

	/*
		Creates a new list containing strings of the old one, satisfying a condition.
		Elements with other types are ignored.
		The function has one parameter, current string, and returns bool.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - filtered list.
	*/
	FilterStrings(function func(string) bool) List

	/*
		Creates a new list containing ints of the old one, satisfying a condition.
		Elements with other types are ignored.
		The function has one parameter, current int, and returns bool.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - filtered list.
	*/
	FilterInts(function func(int) bool) List

	/*
		Creates a new list containing floats of the old one, satisfying a condition.
		Elements with other types are ignored.
		The function has one parameter, current float, and returns bool.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - filtered list.
	*/
	FilterFloats(function func(float64) bool) List

	/*
		Computes a sum of all elements in the list.
		The list has to be homogeneous and all its elements have to be ints.

		Returns:
		  - computed sum (int).
	*/
	IntSum() int

	/*
		Computes a sum of all elements in the list.
		The list has to be homogeneous and all its elements have to be numeric.

		Returns:
		  - computed sum (float).
	*/
	Sum() float64

	/*
		Computes a product of all elements in the list.
		The list has to be homogeneous and all its elements have to be ints.

		Returns:
		  - computed product (int).
	*/
	IntProd() int

	/*
		Computes a product of all elements in the list.
		The list has to be homogeneous and all its elements have to be numeric.

		Returns:
		  - computed pruduct (float).
	*/
	Prod() float64

	/*
		Computes an arithmetic mean of all elements in the list.
		The list has to be homogeneous and all its elements have to be numeric.

		Returns:
		  - computed average value (float).
	*/
	Avg() float64

	/*
		Finds a minimum of the list.
		The list has to be homogeneous and all its elements have to be ints.

		Returns:
		  - found minimum (int).
	*/
	IntMin() int

	/*
		Finds a minimum of the list.
		The list has to be homogeneous and all its elements have to be numeric.

		Returns:
		  - found minimum (float).
	*/
	Min() float64

	/*
		Finds a maximum of the list.
		The list has to be homogeneous and all its elements have to be ints.

		Returns:
		  - found maximum (int).
	*/
	IntMax() int

	/*
		Finds a maximum of the list.
		The list has to be homogeneous and all its elements have to be numeric.

		Returns:
		  - found maximum (float).
	*/
	Max() float64

	/*
		Parallelly executes a given function over an every element of the list.
		The function has two parameters: index of the current element and its value.
		The order of the iterations is random.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged list.
	*/
	ForEachAsync(function func(int, any)) List

	/*
		Copies the list and paralelly modifies each element by a given mapping function.
		The resulting element can have a different type than the original one.
		The function has two parameters: index of the current element and its value.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new list.
	*/
	MapAsync(function func(int, any) any) List

	/*
		Acquires the element specified by the given tree form.

		Parameters:
		  - tf - tree form string.

		Returns:
		  - corresponding value (any type, has to be asserted).
	*/
	GetTF(tf string) any

	/*
		Sets the element specified by the given tree form.

		Parameters:
		  - tf - tree form string,
		  - value - value to set.

		Returns:
		  - updated list.
	*/
	SetTF(tf string, value any) List
	// contains filtered or unexported methods
}

Interface for a list.

Extends:

  • field.

func NewList

func NewList(values ...any) List

List constructor. Creates a new list.

Parameters:

  • values... - any amount of initial elements.

Returns:

  • pointer to the created list.

func NewListFrom

func NewListFrom(slice any) List

List constructor. Converts a slice of supported types to a list.

Parameters:

  • slice - original slice.

Returns:

  • created list.

func NewListOf

func NewListOf(value any, count int) List

List constructor. Creates a new list of n repeated values.

Parameters:

  • value - value to repeat,
  • count - number of repetitions.

Returns:

  • pointer to the created list.

func ParseList

func ParseList(json string) (List, error)

Creates a new list from JSON. Patameters:

  • json - JSON string to parse.

Returns:

  • created list,
  • error if any occurred.

type Object

type Object interface {

	/*
		Initializes the ego pointer, which allows deriving.

		Parameters:
		  - ptr - ego pointer.
	*/
	Init(ptr Object)

	/*
		Acquires the ego pointer previously set by Init.

		Returns:
		  - ego pointer.
	*/
	Ego() Object

	/*
		Sets a values of the fields.
		If the key already exists, the value is overwritten, if not, new field is created.
		If one key is given multiple times, the value is set to the last one.

		Parameters:
		  - values... - any amount of key-value pairs to set.

		Returns:
		  - updated object.
	*/
	Set(values ...any) Object

	/*
		Deletes the fields with given keys.

		Parameters:
		  - keys... - any amount of keys to delete.

		Returns:
		  - updated object.
	*/
	Unset(keys ...string) Object

	/*
		Deletes all field of the object.

		Returns:
		  - updated object.
	*/
	Clear() Object

	/*
		Acquires the value under the specified key of the object.

		Parameters:
		  - key - key of the field to get.

		Returns:
		  - corresponding value (any type, has to be asserted).
	*/
	Get(key string) any

	/*
		Acquires the nested object under the specified key of the object.
		Causes a panic if the field has another type.

		Parameters:
		  - key - key of the field to get.

		Returns:
		  - corresponding value asserted as object.
	*/
	GetObject(key string) Object

	/*
		Acquires the list under the specified key of the object.
		Causes a panic if the field has another type.

		Parameters:
		  - key - key of the field to get.

		Returns:
		  - corresponding value asserted as list.
	*/
	GetList(key string) List

	/*
		Acquires the string under the specified key of the object.
		Causes a panic if the field has another type.

		Parameters:
		  - key - key of the field to get.

		Returns:
		  - corresponding value asserted as string.
	*/
	GetString(key string) string

	/*
		Acquires the bool under the specified key of the object.
		Causes a panic if the field has another type.

		Parameters:
		  - key - key of the field to get.

		Returns:
		  - corresponding value asserted as bool.
	*/
	GetBool(key string) bool

	/*
		Acquires the int under the specified key of the object.
		Causes a panic if the field has another type.

		Parameters:
		  - key - key of the field to get.

		Returns:
		  - corresponding value asserted as int.
	*/
	GetInt(key string) int

	/*
		Acquires the float under the specified key of the object.
		Causes a panic if the field has another type.

		Parameters:
		  - key - key of the field to get.

		Returns:
		  - corresponding value asserted as float.
	*/
	GetFloat(key string) float64

	/*
		Gives a type of the field under the specified key of the object.

		Parameters:
		  - key - key of the field.

		Returns:
		  - integer constant representing the type (see type enum).
	*/
	TypeOf(key string) Type

	/*
		Gives a JSON representation of the object, including nested objects and lists.

		Returns:
		  - JSON string.
	*/
	String() string

	/*
		Gives a JSON representation of the object in standardized format with the given indentation.

		Parameters:
		  - indent - indentation spaces (0-10).

		Returns:
		  - JSON string.
	*/
	FormatString(indent int) string

	/*
		Converts the object into a Go map of empty interfaces.

		Returns:
		  - map.
	*/
	Dict() map[string]any

	/*
		Convers the object to a list of its keys.

		Returns:
		  - list of keys of the object.
	*/
	Keys() List

	/*
		Convers the object to a list of its values.

		Returns:
		  - list of values of the object.
	*/
	Values() List

	/*
		Creates a deep copy of the object.

		Returns:
		  - copied object.
	*/
	Clone() Object

	/*
		Gives a number of fields of the object.

		Returns:
		  - number of fields.
	*/
	Count() int

	/*
		Checks whether the object is empty.

		Returns:
		  - true if the object is empty, false otherwise.
	*/
	Empty() bool

	/*
		Checks if the content of the object is equal to the content of another object.
		Nested objects and lists are compared recursively (by value).

		Parameters:
		  - another - an object to compare with.

		Returns:
		  - true if the objects are equal, false otherwise.
	*/
	Equals(another Object) bool

	/*
		Creates a new object containing all elements of the old object and another object.
		The old object remains unchanged.
		If both objects contain a key, the value from another object is used.

		Parameters:
		  - another - an object to merge.

		Returns:
		  - new object.
	*/
	Merge(another Object) Object

	/*
		Creates a new object containing the given fields of the existing object.

		Parameters:
		  - keys... - any amount of keys to be in the new object.

		Returns:
		  - created plucked object.
	*/
	Pluck(keys ...string) Object

	/*
		Checks if the object contains a field with a given value.
		Objects and lists are compared by reference.

		Parameters:
		  - value - the value to check.

		Returns:
		  - true if the object contains the value, false otherwise.
	*/
	Contains(value any) bool

	/*
		Gives a key containing a given value.
		If multiple keys contain the value, any of them is returned.

		Parameters:
		  - value - the value to check.

		Returns:
		  - key for the value (empty string if the object does not contain the value).
	*/
	KeyOf(value any) string

	/*
		Checks if a given key exists within the object.

		Parameters:
		  - key - the key to check.

		Returns:
		  - true if the key exists, false otherwise.
	*/
	KeyExists(key string) bool

	/*
		Executes a given function over an every field of the object.
		The function has two parameters: key of the current field and its value.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged object.
	*/
	ForEach(function func(string, any)) Object

	/*
		Executes a given function over an every field of the object.
		The function has one parameter, value of the current field.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged object.
	*/
	ForEachValue(function func(any)) Object

	/*
		Executes a given function over all objects nested in the object.
		Fields with other types are ignored.
		The function has one parameter, the current object.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged object.
	*/
	ForEachObject(function func(Object)) Object

	/*
		Executes a given function over all lists in the object.
		Fields with other types are ignored.
		The function has one parameter, the current list.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged object.
	*/
	ForEachList(function func(List)) Object

	/*
		Executes a given function over all strings in the object.
		Fields with other types are ignored.
		The function has one parameter, the current string.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged object.
	*/
	ForEachString(function func(string)) Object

	/*
		Executes a given function over all bools in the object.
		Fields with other types are ignored.
		The function has one parameter, the current bool.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged object.
	*/
	ForEachBool(function func(bool)) Object

	/*
		Executes a given function over all ints in the object.
		Fields with other types are ignored.
		The function has one parameter, the current int.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged object.
	*/
	ForEachInt(function func(int)) Object

	/*
		Executes a given function over all floats in the object.
		Fields with other types are ignored.
		The function has one parameter, the current float.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged object.
	*/
	ForEachFloat(function func(float64)) Object

	/*
		Copies the object and modifies each field by a given mapping function.
		The resulting field can have a different type than the original one.
		The function has two parameters: current key and value of the current element. Returns empty interface.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new list.
	*/
	Map(function func(string, any) any) Object

	/*
		Copies the object and modifies each field by a given mapping function.
		The resulting field can have a different type than the original one.
		The function has one parameter, value of the current field, and returns empty interface.
		The old list remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new object.
	*/
	MapValues(function func(any) any) Object

	/*
		Selects all nested objects of the object and modifies each of them by a given mapping function.
		Fields with other types are ignored.
		The resulting field can have a different type than the original one.
		The function has one parameter, the current object, and returns empty interface.
		The old object remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new object.
	*/
	MapObjects(function func(Object) any) Object

	/*
		Selects all lists of the object and modifies each of them by a given mapping function.
		Fields with other types are ignored.
		The resulting field can have a different type than the original one.
		The function has one parameter, the current list, and returns empty interface.
		The old object remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new object.
	*/
	MapLists(function func(List) any) Object

	/*
		Selects all nested strings of the object and modifies each of them by a given mapping function.
		Fields with other types are ignored.
		The resulting field can have a different type than the original one.
		The function has one parameter, the current string, and returns empty interface.
		The old object remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new object.
	*/
	MapStrings(function func(string) any) Object

	/*
		Selects all nested ints of the object and modifies each of them by a given mapping function.
		Fields with other types are ignored.
		The resulting field can have a different type than the original one.
		The function has one parameter, the current int, and returns empty interface.
		The old object remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new object.
	*/
	MapInts(function func(int) any) Object

	/*
		Selects all nested floats of the object and modifies each of them by a given mapping function.
		Fields with other types are ignored.
		The resulting field can have a different type than the original one.
		The function has one parameter, the current float, and returns empty interface.
		The old object remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new object.
	*/
	MapFloats(function func(float64) any) Object

	/*
		Parallelly executes a given function over an every field of the object.
		The function has two parameters: key of the current field and its value.
		The order of the iterations is random.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - unchanged object.
	*/
	ForEachAsync(function func(string, any)) Object

	/*
		Copies the object and paralelly modifies each field by a given mapping function.
		The resulting field can have a different type than the original one.
		The function has two parameters: key of the current field and its value.
		The old object remains unchanged.

		Parameters:
		  - function - anonymous function to be executed.

		Returns:
		  - new object.
	*/
	MapAsync(function func(string, any) any) Object

	/*
		Acquires the element specified by the given tree form.

		Parameters:
		  - tf - tree form string.

		Returns:
		  - corresponding value (any type, has to be asserted).
	*/
	GetTF(tf string) any

	/*
		Sets the element specified by the given tree form.

		Parameters:
		  - tf - tree form string,
		  - value - value to set.

		Returns:
		  - updated object.
	*/
	SetTF(tf string, value any) Object
	// contains filtered or unexported methods
}

Interface for an object.

Extends:

  • field.

func NewObject

func NewObject(values ...any) Object

Object constructor. Creates a new object.

Parameters:

  • values... - any amount of key-value pairs to set after the object creation.

Returns:

  • pointer to the created object.

func NewObjectFrom

func NewObjectFrom(dict any) Object

Object constructor. Converts a map of supported types to an object.

Parameters:

  • dict - original map.

Returns:

  • created object.

func ParseFile

func ParseFile(path string) (Object, error)

Creates a new object from JSON file. Patameters:

  • path - path to the file to parse.

Returns:

  • created object,
  • error if any occurred.

func ParseObject

func ParseObject(json string) (Object, error)

Creates a new object from JSON. Patameters:

  • json - JSON string to parse.

Returns:

  • created object,
  • error if any occurred.

type Type

type Type uint8

Type for AnyType data types.

const (
	TypeNil Type = iota
	TypeObject
	TypeList
	TypeString
	TypeBool
	TypeInt
	TypeFloat
)

Enum of AnyType data types.

Jump to

Keyboard shortcuts

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