Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support encoding comments and specifying the encoding format #328

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
13 changes: 8 additions & 5 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,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 @@ -462,6 +463,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 @@ -473,6 +475,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