-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalise including resources' fields
`includes` and `fields` are quite similar concepts. The difference is that `include` inludes the complete resource, while `fields` select the fields of the resource to be included. With this commit we abstract that difference away. `IncludedResource` now has a `SelectJSONFields` method that allows picking the fields one want to see in the response. Under the hood `SelectJSONFields` uses json marshal/unmarshal, tehrefore fields selectors work on the resource json representation.
- Loading branch information
1 parent
2a5725f
commit cea5f69
Showing
4 changed files
with
156 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,40 @@ | ||
package model | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type IncludedResource struct { | ||
Type string | ||
Resource any | ||
} | ||
|
||
func (r IncludedResource) SelectJSONFields(fields ...string) (IncludedResource, error) { | ||
resourceBytes, err := json.Marshal(r.Resource) | ||
if err != nil { | ||
return IncludedResource{}, fmt.Errorf("failed to marshal resource: %w", err) | ||
} | ||
|
||
resourceMap := map[string]any{} | ||
if err := json.Unmarshal(resourceBytes, &resourceMap); err != nil { | ||
return IncludedResource{}, fmt.Errorf("failed to unmarshal resource: %w", err) | ||
} | ||
|
||
if len(fields) == 0 { | ||
return IncludedResource{ | ||
Type: r.Type, | ||
Resource: resourceMap, | ||
}, nil | ||
} | ||
|
||
resourceFromFields := map[string]any{} | ||
for _, field := range fields { | ||
resourceFromFields[field] = resourceMap[field] | ||
} | ||
|
||
return IncludedResource{ | ||
Type: r.Type, | ||
Resource: resourceFromFields, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package model_test | ||
|
||
import ( | ||
"code.cloudfoundry.org/korifi/model" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
. "github.com/onsi/gomega/gstruct" | ||
) | ||
|
||
var _ = Describe("IncludedResource", func() { | ||
var resource model.IncludedResource | ||
|
||
BeforeEach(func() { | ||
resource = model.IncludedResource{ | ||
Type: "my-resource-type", | ||
Resource: struct { | ||
StringField string `json:"string_field"` | ||
IntField int `json:"int_field"` | ||
StructField struct { | ||
Foo string `json:"foo"` | ||
} `json:"struct_field"` | ||
}{ | ||
StringField: "my_string", | ||
IntField: 5, | ||
StructField: struct { | ||
Foo string `json:"foo"` | ||
}{ | ||
Foo: "bar", | ||
}, | ||
}, | ||
} | ||
}) | ||
|
||
Describe("SelectJSONFields", func() { | ||
var ( | ||
resourceWithFields model.IncludedResource | ||
fields []string | ||
) | ||
|
||
BeforeEach(func() { | ||
fields = []string{} | ||
}) | ||
|
||
JustBeforeEach(func() { | ||
var err error | ||
resourceWithFields, err = resource.SelectJSONFields(fields...) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("returns a resource with all fields", func() { | ||
Expect(resourceWithFields).To(MatchAllFields(Fields{ | ||
"Type": Equal("my-resource-type"), | ||
"Resource": MatchAllKeys(Keys{ | ||
"string_field": Equal("my_string"), | ||
"int_field": BeEquivalentTo(5), | ||
"struct_field": MatchAllKeys(Keys{ | ||
"foo": Equal("bar"), | ||
}), | ||
}), | ||
})) | ||
}) | ||
|
||
When("fields are selected", func() { | ||
BeforeEach(func() { | ||
fields = []string{"int_field"} | ||
}) | ||
|
||
It("returns a resource with selected fields only", func() { | ||
Expect(resourceWithFields).To(MatchAllFields(Fields{ | ||
"Type": Equal("my-resource-type"), | ||
"Resource": MatchAllKeys(Keys{ | ||
"int_field": BeEquivalentTo(5), | ||
}), | ||
})) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package model_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestModel(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Model Suite") | ||
} |