Skip to content

Commit bebbe66

Browse files
authored
fix(tree): skip unrecognised providers in FromProto instead of erroring (#75)
1 parent 20ed76a commit bebbe66

2 files changed

Lines changed: 14 additions & 20 deletions

File tree

pkg/tree/proto.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tree
22

33
import (
4-
"fmt"
54
"reflect"
65

76
"github.com/infracost/go-proto/pkg/address"
@@ -151,21 +150,12 @@ func FromProto(p *prototree.Tree) (*Tree, error) {
151150

152151
}
153152

154-
if len(p.Providers) > 0 {
155-
for name := range p.Providers {
156-
found := false
157-
for i := range tt.NumField() {
158-
if tt.Field(i).Tag.Get("tree") == name {
159-
found = true
160-
break
161-
}
162-
}
163-
if !found {
164-
return nil, fmt.Errorf("unmapped provider: %s", name)
165-
}
166-
}
167-
}
168-
153+
// Providers present in the proto but with no matching struct field are
154+
// ignored, mirroring how unknown services and resource types are skipped
155+
// above. This keeps FromProto forward-compatible: a reader built against an
156+
// older go-proto must degrade gracefully when a newer emitter introduces a
157+
// provider it doesn't know about, decoding what it recognises rather than
158+
// failing the entire tree.
169159
t.UnsupportedResources = make([]*resource.Resource, len(p.UnsupportedResources))
170160
for i, p := range p.UnsupportedResources {
171161
t.UnsupportedResources[i] = ResourceFromProto(p)

pkg/tree/proto_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,21 @@ func TestRoundTrip_ListField(t *testing.T) {
115115
assert.Equal(t, "t3.small", items[1].Value())
116116
}
117117

118-
func TestFromProtoUnmappedProvider(t *testing.T) {
118+
func TestFromProtoUnknownProviderIgnored(t *testing.T) {
119+
// A provider the struct has no field for (e.g. one added by a newer
120+
// go-proto emitter) must be skipped, not error the whole tree — otherwise
121+
// an older reader drops every resource the moment a new provider appears.
119122
proto := &prototree.Tree{
120123
Providers: map[string]*prototree.Provider{
121-
"gcp": {
124+
"some-future-provider": {
122125
Services: map[string]*prototree.Service{},
123126
},
124127
},
125128
}
126129

127-
_, err := FromProto(proto)
128-
assert.ErrorContains(t, err, "unmapped provider: gcp")
130+
result, err := FromProto(proto)
131+
require.NoError(t, err)
132+
assert.Empty(t, result.ToResources(true))
129133
}
130134

131135
func TestBaseResourceRoundTrip(t *testing.T) {

0 commit comments

Comments
 (0)