convert interface{} to certain type
Asked Answered
N

3

17

I am developing web service that will receive JSON. Go converts types too strict.

So I did following function to convert interface{} in bool

func toBool(i1 interface{}) bool {
    if i1 == nil {
        return false
    }
    switch i2 := i1.(type) {
    default:
        return false
    case bool:
        return i2
    case string:
        return i2 == "true"
    case int:
        return i2 != 0
    case *bool:
        if i2 == nil {
            return false
        }
        return *i2
    case *string:
        if i2 == nil {
            return false
        }
        return *i2 == "true"
    case *int:
        if i2 == nil {
            return false
        }
        return *i2 != 0
    }
    return false
}

I believe that function is still not perfect and I need functions to convert interface{} in string, int, int64, etc

So my question: Is there library (set of functions) in Go that will convert interface{} to certain types

UPDATE

My web service receive JSON. I decode it in map[string]interface{} I do not have control on those who encode it.

So all values I receive are interface{} and I need way to cast it in certain types.

So it could be nil, int, float64, string, [...], {...} and I wish to cast it to what it should be. e.g. int, float64, string, []string, map[string]string with handling of all possible cases including nil, wrong values, etc

UPDATE2

I receive {"s": "wow", "x":123,"y":true}, {"s": 123, "x":"123","y":"true"}, {a:["a123", "a234"]}, {}

var m1 map[string]interface{}
json.Unmarshal(b, &m1)
s := toString(m1["s"])
x := toInt(m1["x"])
y := toBool(m1["y"])
arr := toStringArray(m1["a"])
Nominative answered 18/1, 2014 at 18:28 Comment(8)
I don't think there is one.Niggard
You do know that Go's encoding/json package will do all that for you, right? But anyway, the reflect library can look at an interface and tell you what the real underlying values is, if that's what you mean.Chuckle
Why? Can you tell me more about what kind of real-world issue you are are trying to address? - you know gobyexample.com/json ?Alice
And if you really have this issue - i doubt that - but anyway. Have a look at blog.golang.org/laws-of-reflectionAlice
I am not sure what I can tell more. I even included function I coded myself. I wondering if something similar already exists. Because I try to use existing libraries like gorilla and I wonder if there is casting libary.Nominative
Ah wait. Your second update is what you want do archive? If yes - than is "type assertions" what you are looking for. You don't need any library for that. You just say: s :=m1["s"].(string)Alice
objx is what he is looking. x:= nil; x.(string) will panicLoadstar
I'm not so sure. His example contains two times the key "s". One time with "wow" one time with 123. So it's not nil or not nil. Keys can contain totally diffrent datatypes. So he has to use reflection. But I don't understand the real world use case. I mean you should have at least an Idea about want a key means or could mean.Alice
P
12

objx package makes exactly what you want, it can work directly with JSON, and will give you default values and other cool features:

Objx provides the objx.Map type, which is a map[string]interface{} that exposes a powerful Get method (among others) that allows you to easily and quickly get access to data within the map, without having to worry too much about type assertions, missing data, default values etc.

This is a small example of the usage:

o := objx.New(m1) 
s := o.Get("m1").Str() 
x := o.Get("x").Int() 
y := o.Get("y").Bool()

arr := objx.New(m1["a"])

A example from doc working with JSON:

// use MustFromJSON to make an objx.Map from some JSON
m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`)

// get the details
name := m.Get("name").Str()
age := m.Get("age").Int()

// get their nickname (or use their name if they
// don't have one)
nickname := m.Get("nickname").Str(name)

Obviously you can use something like this with the plain runtime:

switch record[field].(type) {
case int:
    value = record[field].(int)
case float64:
    value = record[field].(float64)
case string:
    value = record[field].(string)
}

But if you check objx accessors you can see a complex code similar to this but with many case of usages, so i think that the best solution is use objx library.

Polycotyledon answered 20/1, 2014 at 7:21 Comment(0)
I
8

Fast/Best way is 'Cast' in time execution (if you know the object):

E.g.

package main    
import "fmt"    
func main() {
    var inter (interface{})
    inter = "hello"
    var value string
    value = inter.(string)
    fmt.Println(value)
}

Try here

Innermost answered 4/12, 2020 at 16:51 Comment(0)
E
4

I came here trying to convert from interface{} to bool and Reflect gave me a clean way to do it:

Having:

v := interface{}
v = true

The solution 1:

if value, ok := v.(bool); ok {
  //you can use variable `value`
}

The solution 2:

reflect.ValueOf(v).Bool()

Then reflect offers a function for the Type you need.

Erewhile answered 24/1, 2022 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.