Skip to content

Commit

Permalink
feat: commented annotation
Browse files Browse the repository at this point in the history
Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault committed Sep 12, 2017
1 parent c58d639 commit a421401
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 37 deletions.
9 changes: 6 additions & 3 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)

type tomlOpts struct {
name string
comment *string
commented bool
include bool
omitempty bool
}
Expand Down Expand Up @@ -148,7 +150,7 @@ func valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, error) {
if err != nil {
return nil, err
}
tval.Set(opts.name, opts.comment, val)
tval.Set(opts.name, opts.comment, opts.commented, val)
}
}
case reflect.Map:
Expand All @@ -158,7 +160,7 @@ func valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, error) {
if err != nil {
return nil, err
}
tval.Set(key.String(), nil, val)
tval.Set(key.String(), nil, false, val)
}
}
return tval, nil
Expand Down Expand Up @@ -453,7 +455,8 @@ func tomlOptions(vf reflect.StructField) tomlOpts {
if c := vf.Tag.Get("comment"); c != "" {
comment = &c
}
result := tomlOpts{vf.Name, comment, true, false}
commented, _ := strconv.ParseBool(vf.Tag.Get("commented"))
result := tomlOpts{name: vf.Name, comment: comment, commented: commented, include: true, omitempty: false}
if parse[0] != "" {
if parse[0] == "-" && len(parse) == 1 {
result.include = false
Expand Down
7 changes: 4 additions & 3 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,9 @@ func TestNestedCustomMarshaler(t *testing.T) {
}

var commentTestToml = []byte(`
# postgres it's a comment on type
# it's a comment on type
[postgres]
# isCommented = "dvalue"
noComment = "cvalue"
# A comment on AttrB
Expand All @@ -613,7 +614,6 @@ var commentTestToml = []byte(`
# a comment on My
[[postgres.My]]
# a comment on My
[[postgres.My]]
`)

Expand All @@ -625,14 +625,15 @@ func TestMarshalComment(t *testing.T) {
AttrA string `toml:"user" comment:"A comment on AttrA"`
AttrB string `toml:"password" comment:"A comment on AttrB"`
AttrC string `toml:"noComment"`
AttrD string `toml:"isCommented" commented:"true"`
My []TypeC `comment:"a comment on My"`
}
type TypeA struct {
TypeB TypeB `toml:"postgres" comment:"it's a comment on type"`
}

ta := []TypeC{{my: "Foo"}, {my: "Baar"}}
config := TypeA{TypeB{AttrA: "avalue", AttrB: "bvalue", AttrC: "cvalue", My: ta}}
config := TypeA{TypeB{AttrA: "avalue", AttrB: "bvalue", AttrC: "cvalue", AttrD: "dvalue", My: ta}}
result, err := Marshal(config)
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (p *tomlParser) parseGroupArray() tomlParserStateFn {
newTree := newTree()
newTree.position = startToken.Position
array = append(array, newTree)
p.tree.SetPath(p.currentTable, nil, array)
p.tree.SetPath(p.currentTable, nil, false, array)

// remove all keys that were children of this table array
prefix := key.val + "."
Expand Down Expand Up @@ -299,7 +299,7 @@ Loop:
key := p.getToken()
p.assume(tokenEqual)
value := p.parseRvalue()
tree.Set(key.val, nil, value)
tree.Set(key.val, nil, false, value)
case tokenComma:
if previous == nil {
p.raiseError(follow, "inline table cannot start with a comma")
Expand Down
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func assertTree(t *testing.T, tree *Tree, err error, ref map[string]interface{})
func TestCreateSubTree(t *testing.T) {
tree := newTree()
tree.createSubTree([]string{"a", "b", "c"}, Position{})
tree.Set("a.b.c", nil, 42)
tree.Set("a.b.c", nil, false, 42)
if tree.Get("a.b.c") != 42 {
t.Fail()
}
Expand Down
24 changes: 14 additions & 10 deletions toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (
)

type tomlValue struct {
value interface{} // string, int64, uint64, float64, bool, time.Time, [] of any of this list
comment *string
position Position
value interface{} // string, int64, uint64, float64, bool, time.Time, [] of any of this list
comment *string
commented bool
position Position
}

// Tree is the result of the parsing of a TOML file.
type Tree struct {
values map[string]interface{} // string -> *tomlValue, *Tree, []*Tree
comment *string
position Position
values map[string]interface{} // string -> *tomlValue, *Tree, []*Tree
comment *string
commented bool
position Position
}

func newTree() *Tree {
Expand Down Expand Up @@ -179,14 +181,14 @@ func (t *Tree) GetDefault(key string, def interface{}) interface{} {
// Set an element in the tree.
// Key is a dot-separated path (e.g. a.b.c).
// Creates all necessary intermediate trees, if needed.
func (t *Tree) Set(key string, comment *string, value interface{}) {
t.SetPath(strings.Split(key, "."), comment, value)
func (t *Tree) Set(key string, comment *string, commented bool, value interface{}) {
t.SetPath(strings.Split(key, "."), comment, commented, value)
}

// SetPath sets an element in the tree.
// Keys is an array of path elements (e.g. {"a","b","c"}).
// Creates all necessary intermediate trees, if needed.
func (t *Tree) SetPath(keys []string, comment *string, value interface{}) {
func (t *Tree) SetPath(keys []string, comment *string, commented bool, value interface{}) {
subtree := t
for _, intermediateKey := range keys[:len(keys)-1] {
nextTree, exists := subtree.values[intermediateKey]
Expand All @@ -213,13 +215,15 @@ func (t *Tree) SetPath(keys []string, comment *string, value interface{}) {
case *Tree:
toInsert = value
subtree.comment = comment
subtree.commented = commented
case []*Tree:
toInsert = value
subtree.comment = comment
subtree.commented = commented
case *tomlValue:
toInsert = value
default:
toInsert = &tomlValue{value: value, comment: comment}
toInsert = &tomlValue{value: value, comment: comment, commented: commented}
}

subtree.values[keys[len(keys)-1]] = toInsert
Expand Down
41 changes: 23 additions & 18 deletions tomltree_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,19 @@ func (t *Tree) writeTo(w io.Writer, indent, keyspace string, bytesCount int64) (
}

if v.comment != nil {
writtenBytesCountComment, errc := writeStrings(w, "\n", indent, "# ", *v.comment, "\n")
comment := strings.Replace(*v.comment, "\n", "\n"+indent, -1)
writtenBytesCountComment, errc := writeStrings(w, "\n", indent, "# ", comment, "\n")
bytesCount += int64(writtenBytesCountComment)
if errc != nil {
return bytesCount, errc
}
}

writtenBytesCount, err := writeStrings(w, indent, k, " = ", repr, "\n")
var commented string
if v.commented {
commented = "# "
}
writtenBytesCount, err := writeStrings(w, indent, commented, k, " = ", repr, "\n")
bytesCount += int64(writtenBytesCount)
if err != nil {
return bytesCount, err
Expand All @@ -140,17 +145,24 @@ func (t *Tree) writeTo(w io.Writer, indent, keyspace string, bytesCount int64) (
if keyspace != "" {
combinedKey = keyspace + "." + combinedKey
}
var commented string
if t.commented {
commented = "# "
}

if t.comment != nil {
comment := strings.Replace(*t.comment, "\n", "\n"+indent, -1)
writtenBytesCountComment, errc := writeStrings(w, "\n", indent, "# ", comment)
bytesCount += int64(writtenBytesCountComment)
if errc != nil {
return bytesCount, errc
}
}

switch node := v.(type) {
// node has to be of those two types given how keys are sorted above
case *Tree:
if t.comment != nil {
writtenBytesCountComment, errc := writeStrings(w, "\n", indent, "# ", combinedKey, " ", *t.comment)
bytesCount += int64(writtenBytesCountComment)
if errc != nil {
return bytesCount, errc
}
}
writtenBytesCount, err := writeStrings(w, "\n", indent, "[", combinedKey, "]\n")
writtenBytesCount, err := writeStrings(w, "\n", indent, commented, "[", combinedKey, "]\n")
bytesCount += int64(writtenBytesCount)
if err != nil {
return bytesCount, err
Expand All @@ -161,14 +173,7 @@ func (t *Tree) writeTo(w io.Writer, indent, keyspace string, bytesCount int64) (
}
case []*Tree:
for _, subTree := range node {
if t.comment != nil {
writtenBytesCountComment, errc := writeStrings(w, "\n", indent, "# ", *t.comment)
bytesCount += int64(writtenBytesCountComment)
if errc != nil {
return bytesCount, errc
}
}
writtenBytesCount, err := writeStrings(w, "\n", indent, "[[", combinedKey, "]]\n")
writtenBytesCount, err := writeStrings(w, "\n", indent, commented, "[[", combinedKey, "]]\n")
bytesCount += int64(writtenBytesCount)
if err != nil {
return bytesCount, err
Expand Down

0 comments on commit a421401

Please sign in to comment.