Skip to content

Commit

Permalink
Rename TomlTree to Tree (pelletier#159)
Browse files Browse the repository at this point in the history
Avoid stutter.

Fixes pelletier#55
  • Loading branch information
pelletier authored May 11, 2017
1 parent 23f6449 commit 685a1f1
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 170 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This library supports TOML version
Go-toml provides the following features for using data parsed from TOML documents:

* Load TOML documents from files and string data
* Easily navigate TOML structure using TomlTree
* Easily navigate TOML structure using Tree
* Line & column position data for all parsed elements
* [Query support similar to JSON-Path](query/)
* Syntax errors contain line and column numbers
Expand Down Expand Up @@ -61,7 +61,7 @@ if err != nil {
password := config.Get("postgres.password").(string)

// or using an intermediate object
configTree := config.Get("postgres").(*toml.TomlTree)
configTree := config.Get("postgres").(*toml.Tree)
user = configTree.Get("user").(string)
password = configTree.Get("password").(string)
fmt.Println("User is ", user, ". Password is ", password)
Expand Down
6 changes: 3 additions & 3 deletions cmd/test_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ func translate(tomlData interface{}) interface{} {
typed[k] = translate(v)
}
return typed
case *toml.TomlTree:
case *toml.Tree:
return translate(*orig)
case toml.TomlTree:
case toml.Tree:
keys := orig.Keys()
typed := make(map[string]interface{}, len(keys))
for _, k := range keys {
typed[k] = translate(orig.GetPath([]string{k}))
}
return typed
case []*toml.TomlTree:
case []*toml.Tree:
typed := make([]map[string]interface{}, len(orig))
for i, v := range orig {
typed[i] = translate(v).(map[string]interface{})
Expand Down
2 changes: 1 addition & 1 deletion cmd/tomljson/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func reader(r io.Reader) (string, error) {
return mapToJSON(tree)
}

func mapToJSON(tree *toml.TomlTree) (string, error) {
func mapToJSON(tree *toml.Tree) (string, error) {
treeMap := tree.ToMap()
bytes, err := json.MarshalIndent(treeMap, "", " ")
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
// // load TOML data stored in a string
// tree, err := toml.Load(stringContainingTomlData)
//
// Either way, the result is a TomlTree object that can be used to navigate the
// Either way, the result is a Tree object that can be used to navigate the
// structure and data within the original document.
//
//
// Getting data from the TomlTree
// Getting data from the Tree
//
// After parsing TOML data with Load() or LoadFile(), use the Has() and Get()
// methods on the returned TomlTree, to find your way through the document data.
// methods on the returned Tree, to find your way through the document data.
//
// if tree.Has("foo") {
// fmt.Println("foo is:", tree.Get("foo"))
Expand Down Expand Up @@ -50,11 +50,11 @@
// tree.GetPath([]string{"foo","bar","baz"})
//
// Note that this is distinct from the heavyweight query syntax supported by
// TomlTree.Query() and the Query() struct (see below).
// Tree.Query() and the Query() struct (see below).
//
// Position Support
//
// Each element within the TomlTree is stored with position metadata, which is
// Each element within the Tree is stored with position metadata, which is
// invaluable for providing semantic feedback to a user. This helps in
// situations where the TOML file parses correctly, but contains data that is
// not correct for the application. In such cases, an error message can be
Expand Down
2 changes: 1 addition & 1 deletion doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Example_comprehensiveExample() {
password := config.Get("postgres.password").(string)

// or using an intermediate object
configTree := config.Get("postgres").(*TomlTree)
configTree := config.Get("postgres").(*Tree)
user = configTree.Get("user").(string)
password = configTree.Get("password").(string)
fmt.Println("User is ", user, ". Password is ", password)
Expand Down
36 changes: 18 additions & 18 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
)

/*
TomlTree structural types and corresponding marshal types
Tree structural types and corresponding marshal types
-------------------------------------------------------------------------------
*TomlTree (*)struct, (*)map[string]interface{}
[]*TomlTree (*)[](*)struct, (*)[](*)map[string]interface{}
*Tree (*)struct, (*)map[string]interface{}
[]*Tree (*)[](*)struct, (*)[](*)map[string]interface{}
[]interface{} (as interface{}) (*)[]primitive, (*)[]([]interface{})
interface{} (*)primitive
TomlTree primitive types and corresponding marshal types
Tree primitive types and corresponding marshal types
-----------------------------------------------------------
uint64 uint, uint8-uint64, pointers to same
int64 int, int8-uint64, pointers to same
Expand All @@ -36,7 +36,7 @@ type tomlOpts struct {
var timeType = reflect.TypeOf(time.Time{})
var marshalerType = reflect.TypeOf(new(Marshaler)).Elem()

// Check if the given marshall type maps to a TomlTree primitive
// Check if the given marshall type maps to a Tree primitive
func isPrimitive(mtype reflect.Type) bool {
switch mtype.Kind() {
case reflect.Ptr:
Expand All @@ -58,7 +58,7 @@ func isPrimitive(mtype reflect.Type) bool {
}
}

// Check if the given marshall type maps to a TomlTree slice
// Check if the given marshall type maps to a Tree slice
func isTreeSlice(mtype reflect.Type) bool {
switch mtype.Kind() {
case reflect.Slice:
Expand All @@ -68,7 +68,7 @@ func isTreeSlice(mtype reflect.Type) bool {
}
}

// Check if the given marshall type maps to a non-TomlTree slice
// Check if the given marshall type maps to a non-Tree slice
func isOtherSlice(mtype reflect.Type) bool {
switch mtype.Kind() {
case reflect.Ptr:
Expand All @@ -80,7 +80,7 @@ func isOtherSlice(mtype reflect.Type) bool {
}
}

// Check if the given marshall type maps to a TomlTree
// Check if the given marshall type maps to a Tree
func isTree(mtype reflect.Type) bool {
switch mtype.Kind() {
case reflect.Map:
Expand Down Expand Up @@ -134,11 +134,11 @@ func Marshal(v interface{}) ([]byte, error) {
}

// Convert given marshal struct or map value to toml tree
func valueToTree(mtype reflect.Type, mval reflect.Value) (*TomlTree, error) {
func valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, error) {
if mtype.Kind() == reflect.Ptr {
return valueToTree(mtype.Elem(), mval.Elem())
}
tval := newTomlTree()
tval := newTree()
switch mtype.Kind() {
case reflect.Struct:
for i := 0; i < mtype.NumField(); i++ {
Expand Down Expand Up @@ -166,8 +166,8 @@ func valueToTree(mtype reflect.Type, mval reflect.Value) (*TomlTree, error) {
}

// Convert given marshal slice to slice of Toml trees
func valueToTreeSlice(mtype reflect.Type, mval reflect.Value) ([]*TomlTree, error) {
tval := make([]*TomlTree, mval.Len(), mval.Len())
func valueToTreeSlice(mtype reflect.Type, mval reflect.Value) ([]*Tree, error) {
tval := make([]*Tree, mval.Len(), mval.Len())
for i := 0; i < mval.Len(); i++ {
val, err := valueToTree(mtype.Elem(), mval.Index(i))
if err != nil {
Expand Down Expand Up @@ -225,10 +225,10 @@ func valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
}
}

// Unmarshal attempts to unmarshal the TomlTree into a Go struct pointed by v.
// Unmarshal attempts to unmarshal the Tree into a Go struct pointed by v.
// Neither Unmarshaler interfaces nor UnmarshalTOML functions are supported for
// sub-structs, and only definite types can be unmarshaled.
func (t *TomlTree) Unmarshal(v interface{}) error {
func (t *Tree) Unmarshal(v interface{}) error {
mtype := reflect.TypeOf(v)
if mtype.Kind() != reflect.Ptr || mtype.Elem().Kind() != reflect.Struct {
return errors.New("Only a pointer to struct can be unmarshaled from TOML")
Expand Down Expand Up @@ -256,7 +256,7 @@ func Unmarshal(data []byte, v interface{}) error {
}

// Convert toml tree to marshal struct or map, using marshal type
func valueFromTree(mtype reflect.Type, tval *TomlTree) (reflect.Value, error) {
func valueFromTree(mtype reflect.Type, tval *Tree) (reflect.Value, error) {
if mtype.Kind() == reflect.Ptr {
return unwrapPointer(mtype, tval)
}
Expand Down Expand Up @@ -295,7 +295,7 @@ func valueFromTree(mtype reflect.Type, tval *TomlTree) (reflect.Value, error) {
}

// Convert toml value to marshal struct/map slice, using marshal type
func valueFromTreeSlice(mtype reflect.Type, tval []*TomlTree) (reflect.Value, error) {
func valueFromTreeSlice(mtype reflect.Type, tval []*Tree) (reflect.Value, error) {
mval := reflect.MakeSlice(mtype, len(tval), len(tval))
for i := 0; i < len(tval); i++ {
val, err := valueFromTree(mtype.Elem(), tval[i])
Expand Down Expand Up @@ -327,9 +327,9 @@ func valueFromToml(mtype reflect.Type, tval interface{}) (reflect.Value, error)
}
switch {
case isTree(mtype):
return valueFromTree(mtype, tval.(*TomlTree))
return valueFromTree(mtype, tval.(*Tree))
case isTreeSlice(mtype):
return valueFromTreeSlice(mtype, tval.([]*TomlTree))
return valueFromTreeSlice(mtype, tval.([]*Tree))
case isOtherSlice(mtype):
return valueFromOtherSlice(mtype, tval.([]interface{}))
default:
Expand Down
2 changes: 1 addition & 1 deletion marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestDocPartialUnmarshal(t *testing.T) {
result := testDocSubs{}

tree, _ := LoadFile("marshal_test.toml")
subTree := tree.Get("subdoc").(*TomlTree)
subTree := tree.Get("subdoc").(*Tree)
err := subTree.Unmarshal(&result)
expected := docData.Subdocs
if err != nil {
Expand Down
38 changes: 19 additions & 19 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type tomlParser struct {
flow chan token
tree *TomlTree
tree *Tree
tokensBuffer []token
currentTable []string
seenTableKeys []string
Expand Down Expand Up @@ -106,18 +106,18 @@ func (p *tomlParser) parseGroupArray() tomlParserStateFn {
}
p.tree.createSubTree(keys[:len(keys)-1], startToken.Position) // create parent entries
destTree := p.tree.GetPath(keys)
var array []*TomlTree
var array []*Tree
if destTree == nil {
array = make([]*TomlTree, 0)
} else if target, ok := destTree.([]*TomlTree); ok && target != nil {
array = destTree.([]*TomlTree)
array = make([]*Tree, 0)
} else if target, ok := destTree.([]*Tree); ok && target != nil {
array = destTree.([]*Tree)
} else {
p.raiseError(key, "key %s is already assigned and not of type table array", key)
}
p.currentTable = keys

// add a new tree to the end of the table array
newTree := newTomlTree()
newTree := newTree()
newTree.position = startToken.Position
array = append(array, newTree)
p.tree.SetPath(p.currentTable, array)
Expand Down Expand Up @@ -183,11 +183,11 @@ func (p *tomlParser) parseAssign() tomlParserStateFn {
}

// find the table to assign, looking out for arrays of tables
var targetNode *TomlTree
var targetNode *Tree
switch node := p.tree.GetPath(tableKey).(type) {
case []*TomlTree:
case []*Tree:
targetNode = node[len(node)-1]
case *TomlTree:
case *Tree:
targetNode = node
default:
p.raiseError(key, "Unknown table type for path: %s",
Expand All @@ -212,7 +212,7 @@ func (p *tomlParser) parseAssign() tomlParserStateFn {
var toInsert interface{}

switch value.(type) {
case *TomlTree, []*TomlTree:
case *Tree, []*Tree:
toInsert = value
default:
toInsert = &tomlValue{value, key.Position}
Expand Down Expand Up @@ -289,8 +289,8 @@ func tokenIsComma(t *token) bool {
return t != nil && t.typ == tokenComma
}

func (p *tomlParser) parseInlineTable() *TomlTree {
tree := newTomlTree()
func (p *tomlParser) parseInlineTable() *Tree {
tree := newTree()
var previous *token
Loop:
for {
Expand Down Expand Up @@ -360,22 +360,22 @@ func (p *tomlParser) parseArray() interface{} {
p.getToken()
}
}
// An array of TomlTrees is actually an array of inline
// An array of Trees is actually an array of inline
// tables, which is a shorthand for a table array. If the
// array was not converted from []interface{} to []*TomlTree,
// array was not converted from []interface{} to []*Tree,
// the two notations would not be equivalent.
if arrayType == reflect.TypeOf(newTomlTree()) {
tomlArray := make([]*TomlTree, len(array))
if arrayType == reflect.TypeOf(newTree()) {
tomlArray := make([]*Tree, len(array))
for i, v := range array {
tomlArray[i] = v.(*TomlTree)
tomlArray[i] = v.(*Tree)
}
return tomlArray
}
return array
}

func parseToml(flow chan token) *TomlTree {
result := newTomlTree()
func parseToml(flow chan token) *Tree {
result := newTree()
result.position = Position{1, 1}
parser := &tomlParser{
flow: flow,
Expand Down
10 changes: 5 additions & 5 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/davecgh/go-spew/spew"
)

func assertSubTree(t *testing.T, path []string, tree *TomlTree, err error, ref map[string]interface{}) {
func assertSubTree(t *testing.T, path []string, tree *Tree, err error, ref map[string]interface{}) {
if err != nil {
t.Error("Non-nil error:", err.Error())
return
Expand All @@ -20,12 +20,12 @@ func assertSubTree(t *testing.T, path []string, tree *TomlTree, err error, ref m
// NOTE: directly access key instead of resolve by path
// NOTE: see TestSpecialKV
switch node := tree.GetPath([]string{k}).(type) {
case []*TomlTree:
case []*Tree:
t.Log("\tcomparing key", nextPath, "by array iteration")
for idx, item := range node {
assertSubTree(t, nextPath, item, err, v.([]map[string]interface{})[idx])
}
case *TomlTree:
case *Tree:
t.Log("\tcomparing key", nextPath, "by subtree assestion")
assertSubTree(t, nextPath, node, err, v.(map[string]interface{}))
default:
Expand All @@ -37,14 +37,14 @@ func assertSubTree(t *testing.T, path []string, tree *TomlTree, err error, ref m
}
}

func assertTree(t *testing.T, tree *TomlTree, err error, ref map[string]interface{}) {
func assertTree(t *testing.T, tree *Tree, err error, ref map[string]interface{}) {
t.Log("Asserting tree:\n", spew.Sdump(tree))
assertSubTree(t, []string{}, tree, err, ref)
t.Log("Finished tree assertion.")
}

func TestCreateSubTree(t *testing.T) {
tree := newTomlTree()
tree := newTree()
tree.createSubTree([]string{"a", "b", "c"}, Position{})
tree.Set("a.b.c", 42)
if tree.Get("a.b.c") != 42 {
Expand Down
6 changes: 3 additions & 3 deletions query/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
// There are several filters provided with the library:
//
// tree
// Allows nodes of type TomlTree.
// Allows nodes of type Tree.
// int
// Allows nodes of type int64.
// float
Expand All @@ -138,7 +138,7 @@
//
// Compiled Queries
//
// Queries may be executed directly on a TomlTree object, or compiled ahead
// Queries may be executed directly on a Tree object, or compiled ahead
// of time and executed discretely. The former is more convienent, but has the
// penalty of having to recompile the query expression each time.
//
Expand All @@ -163,7 +163,7 @@
//
// // define the filter, and assign it to the query
// query.SetFilter("bazOnly", func(node interface{}) bool{
// if tree, ok := node.(*TomlTree); ok {
// if tree, ok := node.(*Tree); ok {
// return tree.Has("baz")
// }
// return false // reject all other node types
Expand Down
Loading

0 comments on commit 685a1f1

Please sign in to comment.