engine: util: Add a workaround for printing special resources
This let's our tests compare strings of interface fields!
This commit is contained in:
@@ -291,7 +291,10 @@ func LangFieldNameToStructType(kind string) (map[string]*types.Type, error) {
|
||||
|
||||
// ResToParamValues returns a list of field names and their corresponding values
|
||||
// if they are non-zero. This is meant for testing, and should be improved for
|
||||
// robustness or with tests if it's ever used for value extraction.
|
||||
// robustness or with tests if it's ever used for value extraction. This also
|
||||
// contains a hack to specifically print from resources that contain interface
|
||||
// fields too. Consider moving that into types.ValueOf after testing if that
|
||||
// doesn't break other code paths.
|
||||
func ResToParamValues(res engine.Res) (map[string]types.Value, error) {
|
||||
|
||||
ret := make(map[string]types.Value)
|
||||
@@ -320,6 +323,17 @@ func ResToParamValues(res engine.Res) (map[string]types.Value, error) {
|
||||
continue // skip zero values
|
||||
}
|
||||
|
||||
// hack to support Value resource
|
||||
// TODO: consider letting types.ValueOf turn an interface into a variant
|
||||
typ := rval.Type()
|
||||
kind := typ.Kind()
|
||||
if kind == reflect.Interface && rval.CanInterface() && !rval.IsNil() {
|
||||
s := fmt.Sprintf("%v", rval) // magic concrete value printer
|
||||
val, _ := types.ValueOfGolang(s)
|
||||
ret[name] = val
|
||||
continue
|
||||
}
|
||||
|
||||
val, err := types.ValueOf(rval)
|
||||
if err != nil {
|
||||
// This can happen for bad fields like "Base" and so on.
|
||||
|
||||
@@ -232,6 +232,19 @@ func ValueOf(v reflect.Value) (Value, error) {
|
||||
V: f,
|
||||
}, nil
|
||||
|
||||
// TODO: should this return a variant value?
|
||||
// TODO: add this into ConfigurableValueOf like ConfigurableTypeOf ?
|
||||
//case reflect.Interface:
|
||||
// t, err := TypeOf(value.Type())
|
||||
// if err != nil {
|
||||
// return nil, errwrap.Wrapf(err, "can't determine type of %+v", value)
|
||||
// }
|
||||
//
|
||||
// return &VariantValue{
|
||||
// T: NewType(?),
|
||||
// V: ?,
|
||||
// }, nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unable to represent value of %+v which has kind: %v", v, kind)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user