kv

package module
v0.0.0-...-48b2ce1 Latest Latest
Warning

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

Go to latest
Published: May 29, 2021 License: MIT Imports: 9 Imported by: 0

README

kv-go

Valve KeyValues (kv1) parser

Documentation

Overview

Package kv implements encoding and decoding of Valve's KeyValue format.

https://developer.valvesoftware.com/wiki/KeyValues

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinaryDecoder

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

BinaryDecoder reads and decodes binary-encoded KeyValue nodes from an input stream.

func NewBinaryDecoder

func NewBinaryDecoder(r io.Reader) *BinaryDecoder

NewBinaryDecoder returns a new binary decoder that reads from r.

func (*BinaryDecoder) Decode

func (d *BinaryDecoder) Decode(kv KeyValue) error

Decode reads the next binary-encoded KeyValue node from its input and stores it in the value pointed to by kv.

type BinaryEncoder

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

BinaryEncoder writes binary-encoded KeyValue nodes to an output stream.

func NewBinaryEncoder

func NewBinaryEncoder(w io.Writer) *BinaryEncoder

NewBinaryEncoder returns a new binary encoder that writes to w.

func (*BinaryEncoder) Encode

func (e *BinaryEncoder) Encode(kv KeyValue) error

Encode writes the KeyValue binary encoding of kv to the stream.

type KeyValue

type KeyValue interface {
	// Type returns the node's Type.
	Type() Type
	// SetType sets the node's Type and returns the receiver.
	SetType(Type) KeyValue
	// Key returns the node's Key.
	Key() string
	// SetKey sets the node's Key and returns the receiver.
	SetKey(key string) KeyValue
	// Value returns the node's Value.
	Value() string
	// AsString returns Value as string if Type is TypeString, otherwise returns an error.
	AsString() (string, error)
	// AsInt32 returns Value as int32 if Type is TypeInt32, otherwise returns an error.
	AsInt32() (int32, error)
	// AsInt64 returns Value as int64 if Type is TypeInt64, otherwise returns an error.
	AsInt64() (int64, error)
	// AsUint64 returns Value as uint64 if Type is TypeUint64, otherwise returns an error.
	AsUint64() (uint64, error)
	// AsFloat32 returns Value as float32 if Type is TypeFloat32, otherwise returns an error.
	AsFloat32() (float32, error)
	// AsColor returns Value as int32 if Type is TypeColor, otherwise returns an error.
	AsColor() (int32, error)
	// AsPointer returns Value as int32 if Type is TypePointer, otherwise returns an error.
	AsPointer() (int32, error)
	// SetValue sets the node's Value and returns the receiver.
	SetValue(value string) KeyValue
	// SetString sets Value to given string value if Type is TypeString, otherwise returns an error.
	SetString(string) error
	// SetInt32 sets Value to given int32 value if Type is TypeInt32, otherwise returns an error.
	SetInt32(int32) error
	// SetInt64 sets Value to given int64 value if Type is TypeInt64, otherwise returns an error.
	SetInt64(int64) error
	// SetUint64 sets Value to given uint64 value if Type is TypeUint64, otherwise returns an error.
	SetUint64(uint64) error
	// SetFloat32 sets Value to given float32 value if Type is TypeFloat32, otherwise returns an error.
	SetFloat32(float32) error
	// SetColor sets Value to given int32 value if Type is TypeColor, otherwise returns an error.
	SetColor(int32) error
	// SetPointer sets Value to given int32 value if Type is TypePointer, otherwise returns an error.
	SetPointer(int32) error
	// Parent returns the parent node.
	Parent() KeyValue
	// SetParent sets the node's parent node and returns the receiver.
	SetParent(KeyValue) KeyValue
	// Children returns all child nodes
	Children() []KeyValue
	// SetChildren sets the node's children and returns the receiver.
	SetChildren(...KeyValue) KeyValue
	// Child finds a child node with the given key.
	Child(key string) KeyValue
	// NewChild creates an empty child node and returns the child node.
	NewChild() KeyValue
	// AddChild adds a child node and returns the receiver.
	AddChild(KeyValue) KeyValue
	// AddObject adds an Object child node and returns the receiver.
	AddObject(key string) KeyValue
	// AddString adds a String child node and returns the receiver.
	AddString(key, value string) KeyValue
	// AddInt32 adds an Int32 child node and returns the receiver.
	AddInt32(key, value string) KeyValue
	// AddInt64 adds an Int64 child node and returns the receiver.
	AddInt64(key, value string) KeyValue
	// AddUint64 adds an Uint64 child node and returns the receiver.
	AddUint64(key, value string) KeyValue
	// AddFloat32 adds a Float32 child node and returns the receiver.
	AddFloat32(key, value string) KeyValue
	// AddColor adds a Color child node and returns the receiver.
	AddColor(key, value string) KeyValue
	// AddPointer adds a Pointer child node and returns the receiver.
	AddPointer(key, value string) KeyValue

	encoding.BinaryMarshaler
	encoding.BinaryUnmarshaler
	encoding.TextMarshaler
	encoding.TextUnmarshaler
}

KeyValue represents a node in a KeyValue tree.

func NewKeyValue

func NewKeyValue(t Type, key, value string, parent KeyValue) KeyValue

NewKeyValue creates a KeyValue node.

func NewKeyValueColor

func NewKeyValueColor(key, value string, parent KeyValue) KeyValue

NewKeyValueColor creates a KeyValue node with TypeColor type.

func NewKeyValueEmpty

func NewKeyValueEmpty() KeyValue

NewKeyValueEmpty creates an empty KeyValue node.

func NewKeyValueFloat32

func NewKeyValueFloat32(key, value string, parent KeyValue) KeyValue

NewKeyValueFloat32 creates a KeyValue node with TypeFloat32 type.

func NewKeyValueInt32

func NewKeyValueInt32(key, value string, parent KeyValue) KeyValue

NewKeyValueInt32 creates a KeyValue node with TypeInt32 type.

func NewKeyValueInt64

func NewKeyValueInt64(key, value string, parent KeyValue) KeyValue

NewKeyValueInt64 creates a KeyValue node with TypeInt64 type.

func NewKeyValueObject

func NewKeyValueObject(key string, parent KeyValue) KeyValue

NewKeyValueObject creates a KeyValue node with TypeObject type.

func NewKeyValuePointer

func NewKeyValuePointer(key, value string, parent KeyValue) KeyValue

NewKeyValuePointer creates a KeyValue node with TypePointer type.

func NewKeyValueRoot

func NewKeyValueRoot(key string) KeyValue

NewKeyValueRoot creates a root KeyValue node.

func NewKeyValueString

func NewKeyValueString(key, value string, parent KeyValue) KeyValue

NewKeyValueString creates a KeyValue node with TypeString type.

func NewKeyValueUint64

func NewKeyValueUint64(key, value string, parent KeyValue) KeyValue

NewKeyValueUint64 creates a KeyValue node with TypeUint64 type.

type TextDecoder

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

TextDecoder reads and decodes text-encoded KeyValue nodes from an input stream.

func NewTextDecoder

func NewTextDecoder(r io.Reader) *TextDecoder

NewTextDecoder returns a new text decoder that reads from r.

func (*TextDecoder) Decode

func (d *TextDecoder) Decode(kv KeyValue) error

Decode reads the next text-encoded KeyValue node from its input and stores it in the value pointed to by kv.

The parser makes no assumptions regarding field types, so all fields are of type TypeString.

type TextEncoder

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

TextEncoder writes text-encoded KeyValue nodes to an output stream.

func NewTextEncoder

func NewTextEncoder(w io.Writer) *TextEncoder

NewTextEncoder returns a new text encoder that writes to w.

func (*TextEncoder) Encode

func (e *TextEncoder) Encode(kv KeyValue) error

Encode writes the KeyValue text encoding of kv to the stream.

type Type

type Type int8

Type represents a KeyValue's node type.

const (
	TypeInvalid Type = iota - 1 // -1
	TypeObject                  // 0x00
	TypeString                  // 0x01
	TypeInt32                   // 0x02
	TypeFloat32                 // 0x03
	TypePointer                 // 0x04
	TypeWString                 // 0x05
	TypeColor                   // 0x06
	TypeUint64                  // 0x07
	TypeEnd                     // 0x08

	TypeInt64 // 0x0a
)

KeyValue types.

func TypeFromByte

func TypeFromByte(b byte) Type

TypeFromByte converts a byte from binary format to a Type.

Returns TypeInvalid if the given byte is not a valid Type.

func (Type) Byte

func (t Type) Byte() byte

Byte returns the corresponding byte in binary format.

func (Type) String

func (i Type) String() string

Directories

Path Synopsis
Package parser implements parsing of KeyValue values in text format.
Package parser implements parsing of KeyValue values in text format.

Jump to

Keyboard shortcuts

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