Skip to content

Commit

Permalink
Removed root node when redundant
Browse files Browse the repository at this point in the history
  • Loading branch information
Galaco committed Sep 17, 2019
1 parent 96da4d5 commit adb1f77
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
[![Build Status](https://travis-ci.com/Galaco/KeyValues.svg?branch=master)](https://travis-ci.com/Galaco/KeyValues)

# 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
```golang
package main
Expand Down
8 changes: 5 additions & 3 deletions keyvalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,14 @@ func TestKeyValue_AsString(t *testing.T) {
}

func TestKeyValue_AsInt(t *testing.T) {
value := int32(3456123)
expected := int32(3456123)
value := "3456123"
kv := KeyValue{
key: "foo",
valueType: ValueInt,
value: append(make([]interface{}, 0), value),
}
if val, err := kv.AsInt(); err != nil || val != value {
if val, err := kv.AsInt(); err != nil || val != expected {
if err != nil {
t.Error(err)
} else {
Expand All @@ -148,11 +149,12 @@ func TestKeyValue_AsInt(t *testing.T) {
}

func TestKeyValue_AsFloat(t *testing.T) {
expected := "23424.1233123"
value := float32(23424.1233123)
kv := KeyValue{
key: "foo",
valueType: ValueFloat,
value: append(make([]interface{}, 0), value),
value: append(make([]interface{}, 0), expected),
}
if val, err := kv.AsFloat(); err != nil || val != value {
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func (reader *Reader) Read() (keyvalue KeyValue, err error) {

readScope(bufReader, &rootNode)

if rootNode.HasChildren() && len(rootNode.value) == 1 {
root := rootNode.value[0].(*KeyValue)
return *root,nil
}

return rootNode, err
}

Expand Down

0 comments on commit adb1f77

Please sign in to comment.