keyvalues

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2019 License: Unlicense Imports: 5 Imported by: 9

README

GoDoc Go report card Build Status

keyvalues

A zero-dependency library for Parsing Valve KeyValue format data.

Go library for parsing Valve keyvalue format files. This library constructs a simple kv node tree that you can query any structure(s) and any property(s) of.

It has been tested against various gameinfo.txt engine files, but should work with other KeyValue files as well (such as .vmf or .vmt).

It is important to note that KeyValue's appear to support (in certain rare uses of the format) multiple root nodes in a single definition. This package will create a root node with Key $root in this situation, with all root nodes as children. If there is only a single root node, the root node will be as defined in the KeyValues.

Usage
package main

import (
    "log"
    "os"
    "github.com/galaco/keyvalues"
)

func main() {
	file,_ := os.Open("gameinfo.txt")

	reader := keyvalues.NewReader(file)
	kv,_ := reader.Read()

    // counterstrike: source's gameinfo.txt would return "Counter-Strike Source"
    gameInfoNode,_ := kv.Find("GameInfo")
    gameNode,_ := gameInfoNode.Find("game")
    log.Println(gameNode.AsString())

    // counterstrike: source's gameinfo.txt would return 1
    noModelsNode,_ := gameInfoNode.Find("nomodels")
    log.Println(noModelsNode.AsInt())

    // counterstrike: source's gameinfo.txt would return 240
    fileSystemNode,_ := gameInfoNode.Find("FileSystem")
    appIdNode,_ := fileSystemNode.Find("SteamAppId")
    log.Println(appIdNode.AsInt())
}
Todo
  • Implement multi-line values. At present, a \n character in a quoted value will break the parser. This is how CS:GO Hammer behaves. However, other versions of Hammer support this, as well as all engine versions. Worth noting what spec is available doesn't cover this behaviour.
  • Implement pointer value type (unsure if there is any point to this besides matching spec)
  • Proper test coverage

Documentation

Index

Constants

View Source
const ValueArray = ValueType("array")
View Source
const ValueFloat = ValueType("float")
View Source
const ValueInt = ValueType("integer")
View Source
const ValuePtr = ValueType("ptr")
View Source
const ValueString = ValueType("string")

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyValue

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

KeyValue object, that may hold multiple Values

func NewKeyValuePair added in v1.4.0

func NewKeyValuePair(key string, value interface{}, valueType ValueType) *KeyValue

NewKeyValuePair allows for manual creation of a single KeyValue pair.

func (*KeyValue) AddChild

func (node *KeyValue) AddChild(value *KeyValue) error

Add adds a new KeyValue pair to an existing Key Existing key's value must be an Array type

func (*KeyValue) AsFloat

func (node *KeyValue) AsFloat() (float32, error)

AsFloat returns value as an int32, assuming it is of float type

func (*KeyValue) AsInt

func (node *KeyValue) AsInt() (int32, error)

AsInt returns value as an int32, assuming it is of integer type

func (*KeyValue) AsString

func (node *KeyValue) AsString() (string, error)

AsString returns value as a string, assuming it is of string type

func (*KeyValue) Children

func (node *KeyValue) Children() (children []*KeyValue, err error)

GetChildren gets all node child values This is used for keys that contain 1 or more children as its value rather than a basic type

func (*KeyValue) Find

func (node *KeyValue) Find(key string) (*KeyValue, error)

Find returns a keyvalue pair where the key matches input It will return the first found KeyValue in cases where the key is defined multiple times

func (*KeyValue) FindAll

func (node *KeyValue) FindAll(key string) (children []*KeyValue, err error)

FindAll returns all children of a given type for a node. This is different from properties, as a property is a string:<primitive> This will return an array of all KeyValues that match a given key, even though there should be only one.

func (*KeyValue) HasChildren

func (node *KeyValue) HasChildren() bool

HasChildren returns if this Key has KeyValues as its own value.

func (*KeyValue) Key

func (node *KeyValue) Key() string

Key returns KeyValues's key

func (*KeyValue) Parent added in v1.2.0

func (node *KeyValue) Parent() *KeyValue

Parent returns this node's parent. Parent can be nil

func (*KeyValue) Patch added in v1.3.0

func (node *KeyValue) Patch(parent *KeyValue) (merged KeyValue, err error)

Patch merges this KeyValue tree into another, adding KeyValues that don't exist in the parent.

func (*KeyValue) RemoveChild added in v1.2.3

func (node *KeyValue) RemoveChild(key string) error

RemoveChild removes a KeyValue from a parent value

func (*KeyValue) Replace added in v1.3.0

func (node *KeyValue) Replace(parent *KeyValue) (merged KeyValue, err error)

Replace merges this KeyValue tree into another. The resultant tree will contain all nodes in the same tree from both this and the target. In the case where a key exists in both trees, this key's value will replace the parent's value

func (*KeyValue) Type

func (node *KeyValue) Type() ValueType

Type returns type of this key's value

type Reader

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

Reader is used for parsing a KeyValue format stream There are various KeyValue based formats (vmt, vmf, gameinfo.txt etc.) This should be able to parse all of them.

func NewReader

func NewReader(file io.Reader) Reader

NewReader Return a new Vmf Reader

func (*Reader) Read

func (reader *Reader) Read() (keyvalue KeyValue, err error)

Read buffer file into our defined structures Returns a fully mapped Vmf structure Every root KeyValue is contained in a predefined root node, due to spec lacking clarity about the number of valid root nodes. This assumes there can be more than 1

type ValueType

type ValueType string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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