Commit a9009387 authored by Aaron Raddon's avatar Aaron Raddon

Json Helper utilities

parent 33600153
......@@ -5,6 +5,7 @@ import (
"fmt"
"math"
"strconv"
"strings"
)
// Coerce types (string,int,int64, float, []byte) into String type
......@@ -49,6 +50,28 @@ func CoerceStringShort(v interface{}) string {
return val
}
// CoerceStrings Coerce type to strings, will split on comma by default.
func CoerceStrings(v interface{}) []string {
switch val := v.(type) {
case string:
return strings.Split(val, ",")
case []string:
return val
case []interface{}:
sva := make([]string, 0)
for _, av := range val {
switch aval := av.(type) {
case string:
sva = append(sva, aval)
default:
//Warnf("Kind ? %T v=%v", aval, aval)
}
}
return sva
}
return []string{CoerceStringShort(v)}
}
func CoerceFloat(v interface{}) (float64, error) {
switch val := v.(type) {
case int:
......@@ -121,6 +144,30 @@ func CoerceIntShort(v interface{}) int {
return 0
}
func CoerceInts(v interface{}) []int {
switch val := v.(type) {
case []string:
iva := make([]int, 0)
for _, av := range val {
avAsInt, ok := valToInt(av)
if ok {
iva = append(iva, avAsInt)
}
}
return iva
case []interface{}:
iva := make([]int, 0)
for _, av := range val {
avAsInt, ok := valToInt(av)
if ok {
iva = append(iva, avAsInt)
}
}
return iva
}
return []int{CoerceIntShort(v)}
}
// Coerce a val(interface{}) into a Uint64
func CoerceUint(v interface{}) (uint64, error) {
u64, ok := valToUint64(v)
......
......@@ -98,61 +98,6 @@ func (m *JsonRawWriter) Raw() json.RawMessage {
return json.RawMessage(m.Bytes())
}
// A simple wrapper to help json data be consumed when not
// using Strongly typed structs.
type JsonInterface struct {
data interface{}
}
// Encode returns its marshaled data as `[]byte`
func (j *JsonInterface) Encode() ([]byte, error) {
return j.MarshalJSON()
}
// Implements the json.Marshaler interface.
func (j *JsonInterface) MarshalJSON() ([]byte, error) {
return json.Marshal(&j.data)
}
// Implements the json.Unmarshal interface.
func (j *JsonInterface) UnmarshalJSON(raw []byte) error {
return json.Unmarshal(raw, &j.data)
}
// Coerce to a String
func (j *JsonInterface) String() (string, error) {
return CoerceString(j.data)
}
// Coerce to a string, may be zero length if missing, or zero length
func (j JsonInterface) StringSh() string {
val, _ := CoerceString(j.data)
return val
}
// Coerce to Int
func (j *JsonInterface) Int() (int, error) {
return CoerceInt(j.data)
}
// Coerce to Int, 0 returned if missing or zero
func (j JsonInterface) IntSh() int {
val, _ := CoerceInt(j.data)
return val
}
// Coerce to Float, return err if needed
func (j *JsonInterface) Float() (float32, error) {
val, err := CoerceFloat(j.data)
return float32(val), err
}
// Coerce to Float, 0 returned if 0 or missing
func (j JsonInterface) FloatSh() float32 {
val, _ := CoerceFloat(j.data)
return float32(val)
}
// A wrapper around a map[string]interface{} to facilitate coercion
// of json data to what you want
//
......@@ -203,6 +148,14 @@ func NewJsonHelpers(b []byte) []JsonHelper {
return jhl
}
func NewJsonHelperMapString(m map[string]string) JsonHelper {
jh := make(JsonHelper)
for k, v := range m {
jh[k] = v
}
return jh
}
// Make a JsonHelper from http response. This will automatically
// close the response body
func NewJsonHelperFromResp(resp *http.Response) (JsonHelper, error) {
......@@ -395,28 +348,7 @@ func (j JsonHelper) String(n string) string {
}
func (j JsonHelper) Strings(n string) []string {
if v := j.Get(n); v != nil {
//Debugf("Strings(%s) => %T %#v", n, v, v)
switch val := v.(type) {
case string:
return strings.Split(val, ",")
case []string:
//Debug("type []string")
return val
case []interface{}:
//Debug("Kind = []interface{} n=", n, " v=", v)
sva := make([]string, 0)
for _, av := range val {
switch aval := av.(type) {
case string:
sva = append(sva, aval)
default:
//Warnf("Kind ? %T v=%v", aval, aval)
}
}
return sva
default:
return []string{j.String(n)}
}
return CoerceStrings(v)
}
return nil
}
......@@ -425,17 +357,7 @@ func (j JsonHelper) Ints(n string) []int {
if v == nil {
return nil
}
if sl, isSlice := v.([]interface{}); isSlice {
iva := make([]int, 0)
for _, av := range sl {
avAsInt, ok := valToInt(av)
if ok {
iva = append(iva, avAsInt)
}
}
return iva
}
return nil
return CoerceInts(v)
}
func (j JsonHelper) StringSafe(n string) (string, bool) {
v := j.Get(n)
......
......@@ -138,30 +138,6 @@ func TestJsonHelper(t *testing.T) {
assert.True(t, jhm[0].Int("sub") == 2, "Should get list of helpers")
}
func TestJsonInterface(t *testing.T) {
var jim map[string]JsonInterface
err := json.Unmarshal([]byte(`{
"nullstring":null,
"string":"string",
"int":22,
"float":22.2,
"floatstr":"22.2",
"intstr":"22"
}`), &jim)
assert.True(t, err == nil, t, "no error:%v ", err)
assert.True(t, jim["nullstring"].StringSh() == "", "nullstring: %v", jim["nullstring"])
assert.True(t, jim["string"].StringSh() == "string", "nullstring: %v", jim["string"])
assert.True(t, jim["int"].IntSh() == 22, "int: %v", jim["int"])
assert.True(t, jim["int"].StringSh() == "22", "int->string: %v", jim["int"])
assert.True(t, jim["int"].FloatSh() == float32(22), "int->float: %v", jim["int"])
assert.True(t, jim["float"].FloatSh() == 22.2, "float: %v", jim["float"])
assert.True(t, jim["float"].StringSh() == "22.2", "float->string: %v", jim["float"])
assert.True(t, jim["float"].IntSh() == 22, "float->int: %v", jim["float"])
assert.True(t, jim["intstr"].IntSh() == 22, "intstr: %v", jim["intstr"])
assert.True(t, jim["intstr"].FloatSh() == float32(22), "intstr->float: %v", jim["intstr"])
}
func TestJsonCoercion(t *testing.T) {
assert.True(t, jh.Int("intstr") == 1, "get string as int %s", jh.String("intstr"))
assert.True(t, jh.String("int") == "1", "get int as string %s", jh.String("int"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment