-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from vimeo/parsingduration_json_cue
json/cue: substitute time.Duration with type using time.ParseDuration
- Loading branch information
Showing
6 changed files
with
193 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Package jsontypes contains helper types used by the JSON and Cue decoders to | ||
// facilitate more natural decoding. | ||
package jsontypes | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// ParsingDuration implements [encoding/json.Unmarshaler], supporting both | ||
// quoted strings that are parseable with [time.ParseDuration], and integer nanoseconds if it's a number | ||
type ParsingDuration int64 | ||
|
||
// UnmarshalJSON implements [encoding/json.Unmarshaler] for ParsingDuration. | ||
func (p *ParsingDuration) UnmarshalJSON(b []byte) error { | ||
d := json.NewDecoder(bytes.NewReader(b)) | ||
d.UseNumber() | ||
n, tokErr := d.Token() | ||
if tokErr != nil { | ||
return fmt.Errorf("failed to parse token: %w", tokErr) | ||
} | ||
switch v := n.(type) { | ||
case string: | ||
dur, durParseErr := time.ParseDuration(v) | ||
if durParseErr != nil { | ||
return fmt.Errorf("failed to parse %q as duration: %w", v, durParseErr) | ||
} | ||
*p = ParsingDuration(dur) | ||
return nil | ||
case json.Number: | ||
i, intParseErr := v.Int64() | ||
if intParseErr != nil { | ||
return fmt.Errorf("failed to parse number as integer nanoseconds: %w", intParseErr) | ||
} | ||
*p = ParsingDuration(i) | ||
return nil | ||
default: | ||
return fmt.Errorf("unexpected JSON token-type %T; expected string or number", n) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package jsontypes | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func ptrVal[V any](v V) *V { | ||
return &v | ||
} | ||
|
||
func TestParsingDuration(t *testing.T) { | ||
t.Parallel() | ||
type decStruct struct { | ||
Dur ParsingDuration | ||
DurPtr *ParsingDuration | ||
} | ||
for _, tbl := range []struct { | ||
name string | ||
inJSON string | ||
expStruct any | ||
expErr bool | ||
}{ | ||
{ | ||
name: "integer_value_no_ptr", | ||
inJSON: `{"dur": 1234}`, | ||
expStruct: decStruct{Dur: 1234}, | ||
expErr: false, | ||
}, | ||
{ | ||
name: "string_value_no_ptr", | ||
inJSON: `{"dur": "3s"}`, | ||
expStruct: decStruct{Dur: ParsingDuration(3 * time.Second)}, | ||
expErr: false, | ||
}, | ||
{ | ||
name: "string_value_ptr", | ||
inJSON: `{"dur": "3s", "durptr": "9m"}`, | ||
expStruct: decStruct{Dur: ParsingDuration(3 * time.Second), DurPtr: ptrVal(ParsingDuration(9 * time.Minute))}, | ||
expErr: false, | ||
}, | ||
{ | ||
name: "integer_value_ptr", | ||
inJSON: `{"dur": "3s", "durptr": 2048}`, | ||
expStruct: decStruct{Dur: ParsingDuration(3 * time.Second), DurPtr: ptrVal(ParsingDuration(2048))}, | ||
expErr: false, | ||
}, | ||
{ | ||
name: "error_array_val", | ||
inJSON: `{"dur": [], "durptr": 2048}`, | ||
expErr: true, | ||
}, | ||
{ | ||
name: "error_object_val", | ||
inJSON: `{"dur": {}, "durptr": 2048}`, | ||
expErr: true, | ||
}, | ||
{ | ||
name: "error_unparsable_str_val", | ||
inJSON: `{"dur": "sssssssssss", "durptr": 2048}`, | ||
expErr: true, | ||
}, | ||
{ | ||
name: "error_float_fractional_val", | ||
inJSON: `{"dur": 0.333333, "durptr": 2048}`, | ||
expErr: true, | ||
}, | ||
} { | ||
t.Run(tbl.name, func(t *testing.T) { | ||
v := decStruct{} | ||
decErr := json.Unmarshal([]byte(tbl.inJSON), &v) | ||
if decErr != nil { | ||
if !tbl.expErr { | ||
t.Errorf("unexpected error unmarshaling: %s", decErr) | ||
} else { | ||
t.Logf("expected error: %s", decErr) | ||
} | ||
return | ||
} | ||
if !reflect.DeepEqual(v, tbl.expStruct) { | ||
t.Errorf("unexpected value\n got: %+v\nwant:%+v", tbl.expStruct, v) | ||
} | ||
}) | ||
|
||
} | ||
} |