lang: types, unification: Don't recurse into private fields

We forgot to omit looking deeper into private struct fields. I don't
know why we didn't catch this earlier, I can only assume some subtlety
changed, since we've previously used many of the resources this would
fail on. Maybe golang broke some API that they didn't consider stable?

This also adds a new test for this, and ensures each resource can be
inspected too!
This commit is contained in:
James Shubin
2024-07-31 15:59:13 -04:00
parent 0a183dfff9
commit bfb5d983c1
2 changed files with 56 additions and 0 deletions

View File

@@ -34,10 +34,13 @@ package solvers
import (
"context"
"fmt"
"reflect"
"strings"
"testing"
"github.com/purpleidea/mgmt/engine"
_ "github.com/purpleidea/mgmt/engine/resources" // import so the resources register
engineUtil "github.com/purpleidea/mgmt/engine/util"
"github.com/purpleidea/mgmt/lang/ast"
"github.com/purpleidea/mgmt/lang/funcs/operators"
"github.com/purpleidea/mgmt/lang/funcs/vars"
@@ -982,3 +985,41 @@ func TestUnification1(t *testing.T) {
})
}
}
func TestLangFieldNameToStructType1(t *testing.T) {
for _, kind := range engine.RegisteredResourcesNames() {
t.Logf("res.Kind(): %s", kind)
res, err := engineUtil.LangFieldNameToStructType(kind)
if err != nil {
t.Errorf("error trying to inspect kind %s: %s", kind, err.Error())
continue
}
if res == nil {
t.Errorf("got nil when trying to inspect kind: %s", kind)
continue
}
}
}
func TestLangFieldNameToStructType2(t *testing.T) {
// This tests that we don't infinitely recurse inside of this function.
k := "consul:kv"
res, err := engineUtil.LangFieldNameToStructType(k)
expected := map[string]*types.Type{
"address": types.TypeStr,
"key": types.TypeStr,
"scheme": types.TypeStr,
"token": types.TypeStr,
"value": types.TypeStr,
}
if err != nil {
t.Errorf("error trying to get the field name type map: %s", err.Error())
return
}
if !reflect.DeepEqual(res, expected) {
t.Errorf("unexpected result: %+v", res)
return
}
}