Skip to content

Commit

Permalink
unmarshal: support encoding.TextUnmarshaler
Browse files Browse the repository at this point in the history
This PR adds support for decoding fields of primitive types that implement
encoding.TextUnmarshaler by calling the custom method.

Fields in anonymous structs are not supported at this point.
  • Loading branch information
oncilla committed Apr 28, 2020
1 parent 2b8e33f commit 8142192
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
34 changes: 34 additions & 0 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const (
var timeType = reflect.TypeOf(time.Time{})
var marshalerType = reflect.TypeOf(new(Marshaler)).Elem()
var textMarshalerType = reflect.TypeOf(new(encoding.TextMarshaler)).Elem()
var textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
var localDateType = reflect.TypeOf(LocalDate{})
var localTimeType = reflect.TypeOf(LocalTime{})
var localDateTimeType = reflect.TypeOf(LocalDateTime{})
Expand Down Expand Up @@ -155,6 +156,14 @@ func callTextMarshaler(mval reflect.Value) ([]byte, error) {
return mval.Interface().(encoding.TextMarshaler).MarshalText()
}

func isTextUnmarshaler(mtype reflect.Type) bool {
return mtype.Implements(textUnmarshalerType)
}

func callTextUnmarshaler(mval reflect.Value, text []byte) error {
return mval.Interface().(encoding.TextUnmarshaler).UnmarshalText(text)
}

// Marshaler is the interface implemented by types that
// can marshal themselves into valid TOML.
type Marshaler interface {
Expand Down Expand Up @@ -866,6 +875,14 @@ func (d *Decoder) valueFromToml(mtype reflect.Type, tval interface{}, mval1 *ref
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a slice", tval, tval)
default:
d.visitor.visit()
// Check if pointer to value implements the encoding.TextUnmarshaler.
if mvalPtr := reflect.New(mtype); isTextUnmarshaler(mvalPtr.Type()) && !isTimeType(mtype) {
if err := d.unmarshalText(tval, mvalPtr); err != nil {
return reflect.ValueOf(nil), fmt.Errorf("unmarshal text: %v", err)
}
return mvalPtr.Elem(), nil
}

switch mtype.Kind() {
case reflect.Bool, reflect.Struct:
val := reflect.ValueOf(tval)
Expand Down Expand Up @@ -983,6 +1000,23 @@ func (d *Decoder) unwrapPointer(mtype reflect.Type, tval interface{}, mval1 *ref
return mval, nil
}

func (d *Decoder) unmarshalText(tval interface{}, mval reflect.Value) error {
var s string
switch val := tval.(type) {
case string:
s = val
case bool:
s = fmt.Sprintf("%v", val)
case int64:
s = fmt.Sprintf("%d", val)
case float64:
s = fmt.Sprintf("%f", val)
default:
return fmt.Errorf("unspported type: %t", val)
}
return callTextUnmarshaler(mval, []byte(s))
}

func tomlOptions(vf reflect.StructField, an annotation) tomlOpts {
tag := vf.Tag.Get(an.tag)
parse := strings.Split(tag, ",")
Expand Down
73 changes: 73 additions & 0 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"reflect"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -3226,3 +3227,75 @@ func TestDecoderStrictValid(t *testing.T) {
t.Fatal("unexpected error:", err)
}
}

type intWrapper struct {
Value int
}

func (w *intWrapper) UnmarshalText(text []byte) error {
var err error
if w.Value, err = strconv.Atoi(string(text)); err == nil {
return nil
}
if b, err := strconv.ParseBool(string(text)); err == nil {
if b {
w.Value = 1
}
return nil
}
if f, err := strconv.ParseFloat(string(text), 32); err == nil {
w.Value = int(f)
return nil
}
return fmt.Errorf("unsupported: %s", text)
}

func TestTextUnmarshal(t *testing.T) {
var doc struct {
UnixTime intWrapper
Version *intWrapper

Bool intWrapper
Int intWrapper
Float intWrapper
}

input := `
UnixTime = "12"
Version = "42"
Bool = true
Int = 21
Float = 2.0
`

if err := Unmarshal([]byte(input), &doc); err != nil {
t.Fatalf("unexpected err: %s", err.Error())
}
if doc.UnixTime.Value != 12 {

t.Fatalf("expected UnixTime: 12 got: %d", doc.UnixTime.Value)
}
if doc.Version.Value != 42 {
t.Fatalf("expected Version: 42 got: %d", doc.Version.Value)
}
if doc.Bool.Value != 1 {
t.Fatalf("expected Bool: 1 got: %d", doc.Bool.Value)
}
if doc.Int.Value != 21 {
t.Fatalf("expected Int: 21 got: %d", doc.Int.Value)
}
if doc.Float.Value != 2 {
t.Fatalf("expected Float: 2 got: %d", doc.Float.Value)
}
}

func TestTextUnmarshalError(t *testing.T) {
var doc struct {
Failer intWrapper
}

input := `Failer = "hello"`
if err := Unmarshal([]byte(input), &doc); err == nil {
t.Fatalf("expected err, got none")
}
}

0 comments on commit 8142192

Please sign in to comment.