Skip to content

Commit

Permalink
Use dhall struct tag instead of json (#15)
Browse files Browse the repository at this point in the history
I originally used `json` to try to piggyback on existing intrumented
code.  However, this is problematic if a user wants to be able to decode
as Dhall in one way but serialize as JSON in another way.

Therefore, this commit switches us to use the `dhall` tag.

Note also that `json` tags support extra metadata after a comma; we
never used that metadata but we had code to remove it.  Also `json`
supports ignoring a field completely with a tag of `"-"`.  This commit
removes all that code; `dhall` tags are just simple strings.

It's possible that in future we will still want to piggyback on `json`
tags, or add extra metadata to `dhall` tags, but for now I want to keep
dhall-golang as simple as possible and not add features until they are
demonstrably required.

Resolves #10.
  • Loading branch information
philandstuff authored Feb 23, 2020
1 parent 5eaebb8 commit e736957
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 16 deletions.
6 changes: 3 additions & 3 deletions example_tagged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

// TaggedMessage is the struct we want to unmarshal from Dhall
type TaggedMessage struct {
Name string `json:"name"`
Body string `json:"entity,string"`
Time int64 `json:"instant"`
Name string `dhall:"name"`
Body string `dhall:"entity"`
Time int64 `dhall:"instant"`
}

// dhallTaggedMessage is the Dhall source we want to unmarshal
Expand Down
14 changes: 2 additions & 12 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dhall
import (
"fmt"
"reflect"
"strings"

"github.com/philandstuff/dhall-golang/core"
"github.com/philandstuff/dhall-golang/imports"
Expand Down Expand Up @@ -106,10 +105,7 @@ func encode(val reflect.Value, typ core.Value) core.Value {
for key, typ := range e {
structType := val.Type()
for i := 0; i < structType.NumField(); i++ {
tag := structType.Field(i).Tag.Get("json")
if idx := strings.Index(tag, ","); idx != -1 {
tag = tag[:idx]
}
tag := structType.Field(i).Tag.Get("dhall")
if key == tag {
rec[key] = encode(val.Field(i), typ)
continue fields
Expand Down Expand Up @@ -187,13 +183,7 @@ func decode(e core.Value, v reflect.Value) {
structType := v.Type()
for i := 0; i < structType.NumField(); i++ {
// FIXME ignores fields in RecordLit not in Struct
tag := structType.Field(i).Tag.Get("json")
if tag == "-" {
continue
}
if idx := strings.Index(tag, ","); idx != -1 {
tag = tag[:idx]
}
tag := structType.Field(i).Tag.Get("dhall")
if tag != "" {
decode(e[tag], v.Field(i))
} else {
Expand Down
2 changes: 1 addition & 1 deletion unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type testStruct struct {
}

type testTaggedStruct struct {
Foo int `json:"baz,string"`
Foo int `dhall:"baz"`
Bar string
}

Expand Down

0 comments on commit e736957

Please sign in to comment.