Skip to content

Commit

Permalink
fix empty queries
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Jun 13, 2024
1 parent 32f7ac5 commit 7ea7abe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 4 additions & 1 deletion tfstate/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (s *TFState) Lookup(key string) (*Object, error) {
var foundName string
for name, ins := range s.scanned {
if strings.HasPrefix(key, name) {
// logest match
// longest match
if len(foundName) < len(name) {
found = ins
foundName = name
Expand All @@ -219,6 +219,9 @@ func (s *TFState) Lookup(key string) (*Object, error) {
}

query := strings.TrimPrefix(key, foundName)
if query == "" {
query = "." // empty query means the whole object
}
if strings.HasPrefix(query, "[") { // e.g. output.foo[0]
query = "." + query
}
Expand Down
18 changes: 10 additions & 8 deletions tfstate/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,16 @@ var TestSuitesOK = []TestSuite{

func testLookupState(t *testing.T, state *tfstate.TFState) {
for _, ts := range TestSuitesOK {
res, err := state.Lookup(ts.Key)
if err != nil {
t.Error(err)
}
t.Log(ts.Key, res)
if diff := cmp.Diff(res.Value, ts.Result); diff != "" {
t.Errorf("%s unexpected result %s", ts.Key, diff)
}
t.Run(ts.Key, func(t *testing.T) {
res, err := state.Lookup(ts.Key)
if err != nil {
t.Error(err)
}
t.Log(ts.Key, res)
if diff := cmp.Diff(res.Value, ts.Result); diff != "" {
t.Errorf("%s unexpected result %s", ts.Key, diff)
}
})
}
}

Expand Down

0 comments on commit 7ea7abe

Please sign in to comment.