Skip to content

Commit

Permalink
Better printing of newlines, some round-tripping support
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Nov 24, 2021
1 parent 91e9a00 commit 367083a
Show file tree
Hide file tree
Showing 11 changed files with 503 additions and 336 deletions.
5 changes: 3 additions & 2 deletions cmd/toml-test-decoder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ func main() {
}

var decoded interface{}
if _, err := toml.DecodeReader(os.Stdin, &decoded); err != nil {
meta, err := toml.DecodeReader(os.Stdin, &decoded)
if err != nil {
log.Fatalf("Error decoding TOML: %s", err)
}

j := json.NewEncoder(os.Stdout)
j.SetIndent("", " ")
if err := j.Encode(tag.Add("", decoded)); err != nil {
if err := j.Encode(tag.Add(meta, "", decoded)); err != nil {
log.Fatalf("Error encoding JSON: %s", err)
}
}
21 changes: 12 additions & 9 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ func (dec *Decoder) Decode(v interface{}) (MetaData, error) {
return MetaData{}, err
}
md := MetaData{
mapping: p.mapping,
types: p.types,
keys: p.ordered,
decoded: make(map[string]bool, len(p.ordered)),
context: nil,
mapping: p.mapping,
types: p.types,
keys: p.ordered,
comments: p.comments,
decoded: make(map[string]bool, len(p.ordered)),
context: nil,
}
return md, md.unify(p.mapping, indirect(rv))
}
Expand Down Expand Up @@ -244,17 +245,17 @@ func (md *MetaData) unifyStruct(mapping interface{}, rv reflect.Value) error {
for _, i := range f.index {
subv = indirect(subv.Field(i))
}

if isUnifiable(subv) {
md.decoded[md.context.add(key).String()] = true
md.context = append(md.context, key)
if err := md.unify(datum, subv); err != nil {
err := md.unify(datum, subv)
if err != nil {
return err
}
md.context = md.context[0 : len(md.context)-1]
} else if f.name != "" {
// Bad user! No soup for you!
return e("cannot write unexported field %s.%s",
rv.Type().String(), f.name)
return e("cannot write unexported field %s.%s", rv.Type().String(), f.name)
}
}
}
Expand Down Expand Up @@ -445,6 +446,7 @@ func (md *MetaData) unifyText(data interface{}, v encoding.TextUnmarshaler) erro
var s string
switch sdata := data.(type) {
case Marshaler:
fmt.Println("unifyText (Marshaler)", data, "in to", v)
text, err := sdata.MarshalTOML()
if err != nil {
return err
Expand All @@ -456,6 +458,7 @@ func (md *MetaData) unifyText(data interface{}, v encoding.TextUnmarshaler) erro
return err
}
s = string(text)
// fmt.Println("unifyText (TextMarshaler)", data, "in to", v, "=", s)
case fmt.Stringer:
s = sdata.String()
case string:
Expand Down
Loading

0 comments on commit 367083a

Please sign in to comment.