gen

package
v0.0.0-...-af45976 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package gen provides type safe generic types. They are type safe in that array and objects can only be constructed of other types in the package. The basic types are:

Bool
Int
Float
String
Time

The collection types are Array and Object. All the types implement the Node interface which is relatively simple interface defined primarily to restrict what can be in the collection types. The Node interface should not be used to define new generic types.

Also included in the package are a builder and parser that behave like the parser and builder in the oj package except for gen types.

Index

Examples

Constants

View Source
const BigLimit = math.MaxInt64 / 10

BigLimit is the limit before a number is converted into a Big instance. (9223372036854775807 / 10 = 922337203685477580)

Variables

View Source
var EmptyArray = Array{}

EmptyArray is a array of nodes of zero length.

View Source
var False = Bool(false)

False is a false boolean value.

View Source
var Sort = false

Sort if true sorts Object keys on output.

View Source
var TimeFormat = ""

TimeFormat defines how time is encoded. Options are to use a time. layout string format such as time.RFC3339Nano, "second" for a decimal representation, "nano" for a an integer.

View Source
var TimeWrap = ""

TimeWrap if not empty encoded time as an object with a single member. For example if set to "@" then and TimeFormat is RFC3339Nano then the encoded time will look like '{"@":"2020-04-12T16:34:04.123456789Z"}'

View Source
var True = Bool(true)

True is a true boolean value.

Functions

This section is empty.

Types

type Array

type Array []Node

Array represents an array of nodes.

func (Array) Alter

func (n Array) Alter() any

Alter the array into a simple []any.

func (Array) Dup

func (n Array) Dup() Node

Dup creates a deep duplicate of the Node.

func (Array) Empty

func (n Array) Empty() bool

Empty returns true if the Array is empty.

func (Array) Simplify

func (n Array) Simplify() any

Simplify creates a simplified version of the Node as a []any.

func (Array) String

func (n Array) String() string

type Big

type Big string

Big represents a number too large to be an int64 or a float64.

func (Big) Alter

func (n Big) Alter() any

Alter returns the backing string.

func (Big) Dup

func (n Big) Dup() Node

Dup returns itself since it is immutable.

func (Big) Empty

func (n Big) Empty() bool

Empty returns true if the backing string is empty.

func (Big) Simplify

func (n Big) Simplify() any

Simplify the Node into a string.

func (Big) String

func (n Big) String() string

String representation of the number.

type Bool

type Bool bool

Bool repreents a boolean value.

func (Bool) Alter

func (n Bool) Alter() any

Alter returns the backing boolean value of the Node.

func (Bool) Dup

func (n Bool) Dup() Node

Dup returns itself.

func (Bool) Empty

func (n Bool) Empty() bool

Empty returns false.

func (Bool) Simplify

func (n Bool) Simplify() any

Simplify returns the backing boolean value.

func (Bool) String

func (n Bool) String() (s string)

String returns a string representation of the Node.

type Builder

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

Builder is assists in build a more complex Node.

Example
package main

import (
	"fmt"

	"github.com/khaf/ojg/gen"
	"github.com/khaf/ojg/sen"
)

func main() {
	var b gen.Builder

	b.MustObject()
	b.MustArray("a")
	b.MustValue(gen.True)
	b.MustObject()
	b.MustValue(gen.Int(123), "x")
	b.Pop()
	b.MustValue(nil)
	b.PopAll()
	v := b.Result()

	fmt.Println(sen.String(v))

}
Output:

{a:[true {x:123}null]}

func (*Builder) Array

func (b *Builder) Array(key ...string) error

Array adds an array to the builder. A key is required if adding to a parent object.

func (*Builder) MustArray

func (b *Builder) MustArray(key ...string)

MustArray adds an array to the builder. A key is required if adding to a parent object.

func (*Builder) MustObject

func (b *Builder) MustObject(key ...string)

MustObject adds an object to the builder. A key is required if adding to a parent object.

func (*Builder) MustValue

func (b *Builder) MustValue(value Node, key ...string)

MustValue adds a Node to the builder. A key is required if adding to a parent object.

func (*Builder) Object

func (b *Builder) Object(key ...string) error

Object adds an object to the builder. A key is required if adding to a parent object.

func (*Builder) Pop

func (b *Builder) Pop()

Pop close a parent Object or Array Node.

func (*Builder) PopAll

func (b *Builder) PopAll()

PopAll close all parent Object or Array Nodes.

func (*Builder) Reset

func (b *Builder) Reset()

Reset clears the the Builder of previous built nodes.

func (*Builder) Result

func (b *Builder) Result() (result Node)

Result returns the current built Node.

func (*Builder) Value

func (b *Builder) Value(value Node, key ...string) error

Value adds a Node to the builder. A key is required if adding to a parent object.

type Float

type Float float64

Float is a float64 Node.

func (Float) Alter

func (n Float) Alter() any

Alter returns the backing float64 value of the Node.

func (Float) Dup

func (n Float) Dup() Node

Dup returns the backing float64 value of the Node.

func (Float) Empty

func (n Float) Empty() bool

Empty returns false.

func (Float) Simplify

func (n Float) Simplify() any

Simplify returns the backing float64 value of the Node.

func (Float) String

func (n Float) String() string

String returns a string representation of the Node.

type Int

type Int int64

Int is a int64 Node.

func (Int) Alter

func (n Int) Alter() any

Alter returns the backing int64 value of the Node.

func (Int) Dup

func (n Int) Dup() Node

Dup returns the backing int64 value of the Node.

func (Int) Empty

func (n Int) Empty() bool

Empty returns false.

func (Int) Simplify

func (n Int) Simplify() any

Simplify returns the backing int64 value of the Node.

func (Int) String

func (n Int) String() string

String returns a string representation of the Node.

type Key

type Key string

Key use for parsing.

func (Key) Alter

func (k Key) Alter() any

Alter converts the node into it's native type. Note this will modify Objects and Arrays in place making them no longer usable as the original type. Use with care!

func (Key) Dup

func (k Key) Dup() Node

Dup returns a deep duplicate of the node.

func (Key) Empty

func (k Key) Empty() bool

Empty returns true if the node is empty.

func (Key) Simplify

func (k Key) Simplify() any

Simplify makes a copy of the node but as simple types.

func (Key) String

func (k Key) String() string

String returns the key as a string.

type Node

type Node interface {
	fmt.Stringer

	// Alter converts the node into it's native type. Note this will modify
	// Objects and Arrays in place making them no longer usable as the
	// original type. Use with care!
	Alter() any

	// Simplify makes a copy of the node but as simple types.
	Simplify() any

	// Dup returns a deep duplicate of the node.
	Dup() Node

	// Empty returns true if the node is empty.
	Empty() bool
}

Node is the interface for typed generic data.

type Number

type Number struct {
	I          uint64
	Frac       uint64
	Div        uint64
	Exp        uint64
	Neg        bool
	NegExp     bool
	BigBuf     []byte
	ForceFloat bool
}

Number is used internally by parsers.

func (*Number) AddDigit

func (n *Number) AddDigit(b byte)

AddDigit to a number.

func (*Number) AddExp

func (n *Number) AddExp(b byte)

AddExp adds an exponent digit.

func (*Number) AddFrac

func (n *Number) AddFrac(b byte)

AddFrac adds a fractional digit.

func (*Number) AsNode

func (n *Number) AsNode() (num Node)

AsNode returns the number as best fit.

func (*Number) AsNum

func (n *Number) AsNum() (num any)

AsNum returns the number as best fit.

func (*Number) FillBig

func (n *Number) FillBig()

FillBig fills the internal buffer with a big number.

func (*Number) Reset

func (n *Number) Reset()

Reset the number.

type Object

type Object map[string]Node

Object is a map of Nodes with string keys.

func (Object) Alter

func (n Object) Alter() any

Alter the Object into a simple map[string]any.

func (Object) Dup

func (n Object) Dup() Node

Dup creates a deep duplicate of the Node.

func (Object) Empty

func (n Object) Empty() bool

Empty returns true if the Object is empty.

func (Object) Simplify

func (n Object) Simplify() any

Simplify creates a simplified version of the Node as a map[string]any.

func (Object) String

func (n Object) String() string

String returns a string representation of the Node.

type ParseError

type ParseError struct {
	Message string
	Line    int
	Column  int
}

ParseError represents a parse error.

func (*ParseError) Error

func (err *ParseError) Error() string

Error returns a string representation of the error.

type Parser

type Parser struct {

	// OnlyOne returns an error if more than one JSON is in the string or stream.
	OnlyOne bool

	// Reuse maps. Previously returned maps will no longer be valid or rather
	// could be modified during parsing.
	Reuse bool
	// contains filtered or unexported fields
}

Parser is a reusable JSON parser. It can be reused for multiple parsings which allows buffer reuse for a performance advantage.

func (*Parser) Parse

func (p *Parser) Parse(buf []byte, args ...any) (Node, error)

Parse a JSON string in to simple types. An error is returned if not valid JSON.

Example
package main

import (
	"fmt"

	"github.com/khaf/ojg/gen"
	"github.com/khaf/ojg/oj"
)

func main() {
	// The parser can be reused for better performance by reusing buffers.
	var p gen.Parser
	v, err := p.Parse([]byte(`{"a": 1, "b":[2,3,4]}`))
	if err == nil {
		// Sorted output allows for consistent results.
		fmt.Println(oj.JSON(v, &oj.Options{Sort: true}))
		fmt.Printf("type: %T\n", v)
	} else {
		fmt.Println(err.Error())
	}
}
Output:

{"a":1,"b":[2,3,4]}
type: gen.Object

func (*Parser) ParseReader

func (p *Parser) ParseReader(r io.Reader, args ...any) (data Node, err error)

ParseReader a JSON io.Reader. An error is returned if not valid JSON.

type String

type String string

String is a string Node.

func (String) Alter

func (n String) Alter() any

Alter returns the backing float64 value of the Node.

func (String) Dup

func (n String) Dup() Node

Dup returns the backing float64 value of the Node.

func (String) Empty

func (n String) Empty() bool

Empty returns false if the string has no characters and true otherwise.

func (String) Simplify

func (n String) Simplify() any

Simplify returns the backing float64 value of the Node.

func (String) String

func (n String) String() string

String returns a string representation of the Node.

type Time

type Time time.Time

Time is a time.Time Node.

func (Time) Alter

func (n Time) Alter() any

Alter returns the backing time.Time value of the Node.

func (Time) Dup

func (n Time) Dup() Node

Dup returns the backing time.Time value of the Node.

func (Time) Empty

func (n Time) Empty() bool

Empty returns false.

func (Time) Simplify

func (n Time) Simplify() any

Simplify returns the backing time.Time value of the Node.

func (Time) String

func (n Time) String() string

String returns a string representation of the Node.

Jump to

Keyboard shortcuts

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