engine: util: Add a workaround for printing special resources

This let's our tests compare strings of interface fields!
This commit is contained in:
James Shubin
2023-11-15 17:04:13 -05:00
parent 90d04990ca
commit 1c9fdc79c0
2 changed files with 28 additions and 1 deletions

View File

@@ -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.