From 1c9fdc79c0aeecc09f5a78a8daa394655c5afb30 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 15 Nov 2023 17:04:13 -0500 Subject: [PATCH] engine: util: Add a workaround for printing special resources This let's our tests compare strings of interface fields! --- engine/util/util.go | 16 +++++++++++++++- lang/types/value.go | 13 +++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/engine/util/util.go b/engine/util/util.go index b5545e55..9668b2ef 100644 --- a/engine/util/util.go +++ b/engine/util/util.go @@ -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. diff --git a/lang/types/value.go b/lang/types/value.go index 4dd99824..d8d04b9f 100644 --- a/lang/types/value.go +++ b/lang/types/value.go @@ -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) }