Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore the missing attribute error to its previous form #602

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions interpreter/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,7 @@ func (a *absoluteAttribute) Resolve(vars Activation) (any, error) {
}
}
}
nsName := "<unknown>"
if len(a.namespaceNames) > 0 {
nsName = a.namespaceNames[0]
}
return nil, missingVariable(nsName)
return nil, missingAttribute(a.String())
}

type conditionalAttribute struct {
Expand Down Expand Up @@ -502,7 +498,7 @@ func (a *maybeAttribute) Resolve(vars Activation) (any, error) {
return nil, err
}
// If this was not a missing variable error, return it.
if !resErr.isMissingVariable() {
if !resErr.isMissingAttribute() {
return nil, err
}
// When the variable is missing in a maybe attribute we defer erroring.
Expand Down Expand Up @@ -1336,13 +1332,13 @@ func refQualify(adapter ref.TypeAdapter, obj any, idx ref.Val, presenceTest, pre
// resolutionError is a custom error type which encodes the different error states which may
// occur during attribute resolution.
type resolutionError struct {
missingVariable string
missingIndex ref.Val
missingKey ref.Val
missingAttribute string
missingIndex ref.Val
missingKey ref.Val
}

func (e *resolutionError) isMissingVariable() bool {
return e.missingVariable != ""
func (e *resolutionError) isMissingAttribute() bool {
return e.missingAttribute != ""
}

func missingIndex(missing ref.Val) *resolutionError {
Expand All @@ -1357,9 +1353,9 @@ func missingKey(missing ref.Val) *resolutionError {
}
}

func missingVariable(variable string) *resolutionError {
func missingAttribute(attr string) *resolutionError {
return &resolutionError{
missingVariable: variable,
missingAttribute: attr,
}
}

Expand All @@ -1371,8 +1367,8 @@ func (e *resolutionError) Error() string {
if e.missingIndex != nil {
return fmt.Sprintf("index out of bounds: %v", e.missingIndex)
}
if e.missingVariable != "" {
return fmt.Sprintf("no such variable: %s", e.missingVariable)
if e.missingAttribute != "" {
return fmt.Sprintf("no such attribute: %s", e.missingAttribute)
}
return "invalid attribute"
}
Expand Down
7 changes: 7 additions & 0 deletions interpreter/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,13 @@ func TestAttributesOptional(t *testing.T) {
},
err: errors.New("no such key: c"),
},
{
// a, no bindings
varName: "a",
quals: []any{},
vars: map[string]any{},
err: errors.New("no such attribute: id: 1, names: [a]"),
},
}
for i, tst := range tests {
tc := tst
Expand Down