samedi 25 novembre 2017

Converting interface into another and copy content

I've got the following method:

func ValidateParam(conf map[string]interface{}, paramName string, out interface{}) error {
    param, ok := conf[paramName]

    if !ok {
        return errors.New("some error")
    }

    // ...
}

I would like to be able to call it like so:

myVar := "some text"
err := ValidateParam(conf, "my_var_param", &myVar)

myOtherVar := &MyStruct{}
err := ValidateParam(conf, "my_struct_param", myOtherVar)

The idea is:

  • Get the param using the conf map
  • Check that this param could be converted into the same type as out
  • Hydrate out using the param

=> It is kind of the same process as for json.Unmarshal(data, &myVar) or when doing a query with mgo query.Collection("col").One(&myVar)

I can't find how to achieve this, any help would be more than welcome.

Cheers





Aucun commentaire:

Enregistrer un commentaire