Skip to content

Commit 1410239

Browse files
jeremyrickardcarolynvs-msft
authored andcommitted
Reintroduce ConvertValue(interface{}) (#41)
* Reintroduce ConvertValue(interface{}) This PR re-introduces the ConvertValue(interface{}) method, but relocates it to the definitions package. * Updated ConvertValue * Fix lint issues
1 parent 62eeee4 commit 1410239

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

bundle/definition/schema.go

+33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package definition
22

33
import (
44
"encoding/json"
5+
"strconv"
6+
"strings"
57

68
"github.com/pkg/errors"
79
"github.com/qri-io/jsonschema"
@@ -107,3 +109,34 @@ func (s *Schema) UnmarshalJSON(data []byte) error {
107109
}
108110
return json.Unmarshal(data, &wrapper)
109111
}
112+
113+
// ConvertValue attempts to convert the given string value to the type from the
114+
// definition. Note: this is only applicable to string, number, integer and boolean types
115+
func (s *Schema) ConvertValue(val string) (interface{}, error) {
116+
dataType, ok, err := s.GetType()
117+
if !ok {
118+
return nil, errors.Wrapf(err, "unable to determine type: %v", s.Type)
119+
}
120+
switch dataType {
121+
case "string":
122+
return val, nil
123+
case "number":
124+
num, err := strconv.ParseFloat(val, 64)
125+
if err != nil {
126+
return nil, errors.Wrapf(err, "unable to convert %s to number", val)
127+
}
128+
return num, nil
129+
case "integer":
130+
return strconv.Atoi(val)
131+
case "boolean":
132+
if strings.ToLower(val) == "true" {
133+
return true, nil
134+
} else if strings.ToLower(val) == "false" {
135+
return false, nil
136+
} else {
137+
return false, errors.Errorf("%q is not a valid boolean", val)
138+
}
139+
default:
140+
return nil, errors.New("invalid definition")
141+
}
142+
}

bundle/definition/schema_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,69 @@ func valueTestJSON(kind, def, enum string) []byte {
269269
"enum": [ %s ]
270270
}`, kind, def, enum))
271271
}
272+
273+
func TestConvertValue(t *testing.T) {
274+
pd := Schema{
275+
Type: "boolean",
276+
}
277+
is := assert.New(t)
278+
279+
out, _ := pd.ConvertValue("true")
280+
is.True(out.(bool))
281+
out, _ = pd.ConvertValue("false")
282+
is.False(out.(bool))
283+
out, _ = pd.ConvertValue("barbeque")
284+
is.False(out.(bool))
285+
286+
pd.Type = "string"
287+
out, err := pd.ConvertValue("hello")
288+
is.NoError(err)
289+
is.Equal("hello", out.(string))
290+
291+
pd.Type = "integer"
292+
out, err = pd.ConvertValue("123")
293+
is.NoError(err)
294+
is.Equal(123, out.(int))
295+
296+
_, err = pd.ConvertValue("onetwothree")
297+
is.Error(err)
298+
299+
pd.Type = "number"
300+
out, err = pd.ConvertValue("123")
301+
is.NoError(err)
302+
is.Equal(float64(123), out.(float64))
303+
304+
out, err = pd.ConvertValue("5.5")
305+
is.NoError(err)
306+
is.Equal(5.5, out.(float64))
307+
308+
_, err = pd.ConvertValue("nope")
309+
is.Error(err)
310+
311+
pd.Type = "array"
312+
_, err = pd.ConvertValue("nope")
313+
is.Error(err)
314+
315+
_, err = pd.ConvertValue("123")
316+
is.Error(err)
317+
318+
_, err = pd.ConvertValue("true")
319+
is.Error(err)
320+
321+
_, err = pd.ConvertValue("123.5")
322+
is.Error(err)
323+
324+
pd.Type = "object"
325+
_, err = pd.ConvertValue("nope")
326+
is.Error(err)
327+
328+
_, err = pd.ConvertValue("123")
329+
is.Error(err)
330+
331+
_, err = pd.ConvertValue("true")
332+
is.Error(err)
333+
334+
_, err = pd.ConvertValue("123.5")
335+
is.Error(err)
336+
337+
}

0 commit comments

Comments
 (0)