document

package
v0.0.0-...-21754ba Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalid = errors.New("invalid quoted string")

Functions

func AppendQuotedString

func AppendQuotedString(b []byte, s string, quote byte) []byte

AppendQuotedString appends s, quoted for use as a KDL FormattedString, to b, and returns the expanded buffer.

AppendQuotedString is based on the JSON string quoting function from the MIT-Licensed ZeroLog, Copyright (c) 2017 Olivier Poitrey, but has been heavily modified to improve performance and use KDL string escapes instead of JSON.

func AppendRawString

func AppendRawString(b []byte, s string) []byte

AppendRawString appends s, quoted for use as a KDL RawString, to b and returns the expanded buffer.

func AppendUnquotedString

func AppendUnquotedString(b []byte, s string, quote byte) ([]byte, error)

AppendUnquotedString appends s, unquoted from KDL FormattedString notation, to b and returns the expanded buffer.

AppendUnquotedString was originally based on the JSON string quoting function from the MIT-Licensed ZeroLog, Copyright (c) 2017 Olivier Poitrey, but has been heavily modified to unquote KDL quoted strings.

func QuoteString

func QuoteString(s string) string

QuoteString returns s quoted for use as a KDL FormattedString

func UnquoteString

func UnquoteString(s string) (string, error)

UnquoteString returns s unquoted from KDL FormattedString notation

Types

type Document

type Document struct {
	Nodes []*Node
}

Document is the top-level container for a KDL document

func New

func New() *Document

New cerates a new Document

func (*Document) AddNode

func (d *Document) AddNode(child *Node)

AddNode adds a Node to this document

type Node

type Node struct {
	// Name is name of the node
	Name *Value
	// Type is the type annotation of the node, or an empty string if none
	Type TypeAnnotation
	// Arguments is the list of arguments for the node, or nil if none
	Arguments []*Value
	// Properties is the list of properties for the node, or nil if none
	Properties Properties
	// Children is the list of child nodes for the node, or nil if none
	Children []*Node
}

Node represents a single node in a KDL document

func NewNode

func NewNode() *Node

NewNode creates and returns a new Node

func (*Node) AddArgument

func (n *Node) AddArgument(value interface{}, typeAnnot TypeAnnotation) *Value

AddArgument adds an argument to this node, with the given type annotation (which may be ""), and returns the added Value

func (*Node) AddArgumentToken

func (n *Node) AddArgumentToken(t tokenizer.Token, typeAnnot tokenizer.Token) error

AddArgumentToken adds an argument to this node from a Token, with the given type annotation (which may be ""), and returns a non-nil error on failure

func (*Node) AddNode

func (n *Node) AddNode(child *Node)

AddNode adds a Node as a child of this node

func (*Node) AddProperty

func (n *Node) AddProperty(name string, value interface{}, typeAnnot TypeAnnotation) *Value

AddProperty adds a property to this node with the given name, value, and type annotation (which may be ""), and returns the added Value

func (*Node) AddPropertyToken

func (n *Node) AddPropertyToken(name tokenizer.Token, value tokenizer.Token, typeAnnot tokenizer.Token) (*Value, error)

AddPropertyToken adds a property to this node from the given name and value Token and type annotation (which may be "") and returns the added Value and a non-nil error on failure

func (*Node) AddPropertyValue

func (n *Node) AddPropertyValue(name string, value *Value, typeAnnot TypeAnnotation) *Value

func (*Node) ExpectArguments

func (n *Node) ExpectArguments(count int)

func (*Node) ExpectChildren

func (n *Node) ExpectChildren(count int)

func (*Node) SetName

func (n *Node) SetName(name string)

SetName sets the name of the node

func (*Node) SetNameToken

func (n *Node) SetNameToken(t tokenizer.Token) error

SetNameToken sets the name of the node from a Token, and returns a non-nil error on failure

func (*Node) String

func (n *Node) String() string

String returns the complete KDL representation of this node, including its type annotation and name

func (*Node) TextString

func (n *Node) TextString() string

TextString returns a text representation of this node, without its type annotation or name. If the node contains exactly one argument, zero properties, and zero children, it writes the unquoted string representation of the only argument.

func (*Node) ValueString

func (n *Node) ValueString() string

ValueString returns the KDL representation of this node, without its type annotation or name.

func (*Node) WriteTextValueTo

func (n *Node) WriteTextValueTo(w io.Writer) (int64, error)

WriteTextValueTo writes a text representation of the arguments, properties, and children of node. If node contains exactly one argument, zero properties, and zero children, it writes the unquoted string representation of the only argument.

func (*Node) WriteTo

func (n *Node) WriteTo(w io.Writer) (int64, error)

WriteTo writes the complete KDL representation of this node, including its type annotation or name.

func (*Node) WriteToOptions

func (n *Node) WriteToOptions(w io.Writer, opts NodeWriteOptions) (int64, error)

WriteToOptions writes the KDL representation of this node with the specified options.

func (*Node) WriteValueTo

func (n *Node) WriteValueTo(w io.Writer) (int64, error)

WriteValueTo writes the KDL representation of this node, without its type annotation or name.

type NodeWriteOptions

type NodeWriteOptions struct {
	// LeadingTrailingSpace specifies whether leading space (indentation) and newlines are included in the output
	LeadingTrailingSpace bool
	// NameAndType specifies whether the node's name and type annotation are included in the output
	NameAndType bool
	// Depth specifies the indentation depth
	Depth int
	// Indent specifies the byte string to use for each indentation level
	Indent []byte
	// IgnoreFlags specifies that the formatting flags for the node's value(s) should be ignored
	IgnoreFlags bool
}

NodeWriteOptions controls how a node is written using WriteToOptions.

type Properties

type Properties map[string]*Value

Properties represents a list of properties for a Node

func (Properties) Add

func (p Properties) Add(name string, val *Value)

Add adds a property to the list

func (*Properties) Alloc

func (p *Properties) Alloc()

Alloc allocates the property list

func (Properties) Allocated

func (p Properties) Allocated() bool

Allocated indicates whether the property list has been allocated

func (Properties) Exist

func (p Properties) Exist() bool

Exist indicates whether any properties exist

func (Properties) Get

func (p Properties) Get(key string) (*Value, bool)

Get returns Properties[key]

func (Properties) Len

func (p Properties) Len() int

Len returns the number of properties

func (Properties) String

func (p Properties) String() string

String returns the KDL representation of the property list, formatting numbers per their flags

func (Properties) UnformattedString

func (p Properties) UnformattedString() string

UnformattedString returns the KDL representation of the property list, formatting numbers in decimal

func (Properties) Unordered

func (p Properties) Unordered() map[string]*Value

Unordered returns the unordered property map; this simply passes through p in this implementation but is provided as it is necessary in the deterministic version

type SuffixedDecimal

type SuffixedDecimal struct {
	Number []byte
	Suffix []byte
}

func ParseSuffixedDecimal

func ParseSuffixedDecimal(b []byte) (SuffixedDecimal, error)

func (SuffixedDecimal) AsDuration

func (s SuffixedDecimal) AsDuration() (time.Duration, error)

func (SuffixedDecimal) AsNumber

func (s SuffixedDecimal) AsNumber() (interface{}, error)

func (SuffixedDecimal) String

func (s SuffixedDecimal) String() string

type TypeAnnotation

type TypeAnnotation string

TypeAnnotation represents a type annotation in a KDL document

type Value

type Value struct {
	// Type is the value type, if an annotation was provided
	Type TypeAnnotation
	// Value is the actual value
	Value interface{}
	// Flag is any flag assigned for use in output
	Flag ValueFlag
}

Value represents a value in a KDL document

func ValueFromToken

func ValueFromToken(t tokenizer.Token) (*Value, error)

ValueFromToken creates and returns a Value representing the content of t, or a non-nil error on failure

func (*Value) FormattedString

func (v *Value) FormattedString() string

FormattedString is similar to String, but bare strings are converted to quoted strings.

This is suitable for returning arguments and property values while preserving their original formatting.

func (*Value) NodeNameString

func (v *Value) NodeNameString() string

NodeNameString returns the simplest possible KDL representation of this Value, including type annotation, formatting numbers in decimal notation and strings as bare strings if possible, otherwise quoted.

This is suitable for returning a valid node name.

func (*Value) ResolvedValue

func (v *Value) ResolvedValue() interface{}

ResolvedValue returns the unquoted, unescaped, un-type-hinted Go representation of this value via an interface{}: - numbers are returned as the appropriate numeric type (int64, float64, *big.Int, *big.Float, etc), - bools are returned as a bool - nulls are returned as nil - strings are returned as strings containing the unquoted representation of the string

func (*Value) String

func (v *Value) String() string

String returns the KDL representation of this Value, including type annotation, formatting numbers and strings per their Flags.

This returns the exact input KDL (if any) that was used to generate this Value.

func (*Value) UnformattedString

func (v *Value) UnformattedString() string

UnformattedString is similar to String, but bare strings are converted to quoted strings and numbers are formatted in decimal notation.

This is suitable for returning arguments and property values while ignoring their original formatting.

func (*Value) ValueString

func (v *Value) ValueString() string

ValueString returns the unquoted, unescaped, un-type-hinted representation of this Value; numbers are formatted per their Flags, strings are always unquoted.

This is suitable for passing as a []byte value to UnmarshalText.

type ValueFlag

type ValueFlag uint8

ValueFlag represents flags for a Value

const (
	// FlagNone indicates no flag is set
	FlagNone ValueFlag = iota
	// FlagRaw specifies that this Value should be output in RawString notation (r"foo\n")
	FlagRaw
	// FlagQuoted specifies that this Value should be output in FormattedString notation ("foo\\n")
	FlagQuoted
	// FlagBinary specifies that this Value should be output in binary notation (0b10101010)
	FlagBinary
	// FlagOctal specifies that this Value should be output in octal notation (0o751)
	FlagOctal
	// FlagHexadecimal specifies that this Value should be output in hexadecimal notation (0xdeadbeef)
	FlagHexadecimal
)

Jump to

Keyboard shortcuts

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