You problem is that your incoming data type of Value is map[string]interface{}, and there's no direct/native way in Go to convert map into your type (while there's definitely code out there).
OK. If we assume that we totally have no control over incoming data in the Value field, but still, we can identify data type by a combination of its attributes, right? Because by definition, you should know possible options. We can create a universal incoming object instead of interface{}. AWS is using similar approach in their Go SDK, at least for DynamoDB service, setting optional attributes via pointers: https://github.com/aws/aws-sdk-go/blob/master/service/dynamodb/examples_test.go#L32
So, the approach is: your UnknownObj struct will have optional attributes that may be filled (and may be not) on json.Unmarshal. Knowing what fields were delivered via the switch, you can guess the data sent.
package main
import (
"encoding/json"
"fmt"
)
type session struct {
Value UnknownObj
Flash map[string]string
}
type UnknownObj struct {
Name *string
Age *float64
SomeOtherField *map[string]string
}
func Get() UnknownObj {
marshalledString := `{"Value":{"Name":"bob","Age":3},"Flash":null}`
var sess session
json.Unmarshal([]byte(marshalledString), &sess)
return sess.Value
}
func main() {
v := Get()
switch {
case v.Name != nil && v.Age != nil:
fmt.Println("This is a Person")
default:
fmt.Println("Unknown data type")
}
}
However, if you have control over the root/Values field and you can request to send you specific fields for each of the types instead of pushing all under Values, then you could have:
type session struct {
Person *Person
Car *Car
Building *Buidling
Etc *Etc
...
}
This way, your solution will be even easier -> you'll just need to check what property is not nil.
Hope this helps.
Cheers.
Update Dec 15, 2016
To reply on your comment regarding the framework: what you are describing is a process of binding of user's request to an arbitrary data-type.
OK. Unfortunately, its too much code to post here, but here's a link as a starting point:
https://github.com/go-playground/validator/blob/v8.18.1/validator.go#L498
This is a package and approach Gin framework is using for binding here: https://github.com/gin-gonic/gin/blob/master/binding/json.go
Good luck!