Decoding error position tracking

This commit is contained in:
Thomas Pelletier
2021-03-30 21:43:57 -04:00
parent 18d45c446b
commit 32da85ab11
8 changed files with 150 additions and 34 deletions
+16 -1
View File
@@ -268,11 +268,22 @@ func (d *decoder) unmarshalValue(x target, node ast.Node) error {
return unmarshalLocalDateTime(x, node)
case ast.DateTime:
return unmarshalDateTime(x, node)
case ast.LocalDate:
return unmarshalLocalDate(x, node)
default:
panic(fmt.Errorf("unhandled unmarshalValue kind %s", node.Kind))
}
}
func unmarshalLocalDate(x target, node ast.Node) error {
assertNode(ast.LocalDate, node)
v, err := parseLocalDate(node.Data)
if err != nil {
return err
}
return setDate(x, v)
}
func unmarshalLocalDateTime(x target, node ast.Node) error {
assertNode(ast.LocalDateTime, node)
v, rest, err := parseLocalDateTime(node.Data)
@@ -280,7 +291,7 @@ func unmarshalLocalDateTime(x target, node ast.Node) error {
return err
}
if len(rest) > 0 {
return fmt.Errorf("extra characters at the end of a local date time")
return newDecodeError(rest, "extra characters at the end of a local date time")
}
return setLocalDateTime(x, v)
}
@@ -302,6 +313,10 @@ func setDateTime(x target, v time.Time) error {
return x.set(reflect.ValueOf(v))
}
func setDate(x target, v LocalDate) error {
return x.set(reflect.ValueOf(v))
}
func unmarshalString(x target, node ast.Node) error {
assertNode(ast.String, node)
return setString(x, string(node.Data))