Skip to content
Snippets Groups Projects
Commit d2ec2f90 authored by ale's avatar ale
Browse files

Implement IntOrStr deserialization for YAML, not JSON

Oops.
parent 593ec9b5
No related branches found
No related tags found
No related merge requests found
Pipeline #86266 passed
package floatschema
import (
"encoding/json"
"strconv"
"gopkg.in/yaml.v3"
)
type IntOrStr int
func (i *IntOrStr) UnmarshalJSON(b []byte) error {
if len(b) > 0 && b[0] == '"' {
var tmp string
if err := json.Unmarshal(b, &tmp); err != nil {
func (i *IntOrStr) UnmarshalYAML(value *yaml.Node) error {
var tmpI int
if err := value.Decode(&tmpI); err == nil {
*i = IntOrStr(tmpI)
return nil
}
var tmpS string
if err := value.Decode(&tmpS); err != nil {
return err
}
ii, err := strconv.Atoi(tmp)
ii, err := strconv.Atoi(tmpS)
if err != nil {
return err
}
*i = IntOrStr(ii)
return nil
}
var tmp int
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
*i = IntOrStr(tmp)
return nil
}
func IntOrStrListToIntList(l []IntOrStr) []int {
o := make([]int, 0, len(l))
......
package floatschema
import (
"encoding/json"
"testing"
"gopkg.in/yaml.v3"
)
func TestIntOrStr(t *testing.T) {
......@@ -17,7 +18,7 @@ func TestIntOrStr(t *testing.T) {
{"\"foo\"", false, 0},
} {
var i IntOrStr
err := json.Unmarshal([]byte(td.s), &i)
err := yaml.Unmarshal([]byte(td.s), &i)
ok := err == nil
if ok != td.ok {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment