Skip to content

Commit 6daeeda

Browse files
author
Antoine Pelisse
committed
Make ParseableType into an interface
1 parent 98a788b commit 6daeeda

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

internal/fixture/state.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
// any time that Live and Managers match the expectations.
3131
type State struct {
3232
Live *typed.TypedValue
33-
Parser *typed.ParseableType
33+
Parser typed.ParseableType
3434
Managers fieldpath.ManagedFields
3535
Updater *merge.Updater
3636
}
@@ -228,7 +228,7 @@ type TestCase struct {
228228
}
229229

230230
// Test runs the test-case using the given parser.
231-
func (tc TestCase) Test(parser *typed.ParseableType) error {
231+
func (tc TestCase) Test(parser typed.ParseableType) error {
232232
state := State{
233233
Updater: &merge.Updater{Converter: &dummyConverter{}},
234234
Parser: parser,

merge/leaf_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"sigs.k8s.io/structured-merge-diff/typed"
2626
)
2727

28-
var leafFieldsParser = func() *typed.ParseableType {
28+
var leafFieldsParser = func() typed.ParseableType {
2929
parser, err := typed.NewParser(`types:
3030
- name: leafFields
3131
struct:
@@ -262,7 +262,7 @@ func TestUpdateLeaf(t *testing.T) {
262262
Apply{
263263
Manager: "default",
264264
APIVersion: "v1",
265-
Object: "",
265+
Object: "",
266266
},
267267
},
268268
Object: `

merge/set_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"sigs.k8s.io/structured-merge-diff/typed"
2525
)
2626

27-
var setFieldsParser = func() *typed.ParseableType {
27+
var setFieldsParser = func() typed.ParseableType {
2828
parser, err := typed.NewParser(`types:
2929
- name: sets
3030
struct:

typed/parser.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,36 @@ func (p *Parser) TypeNames() (names []string) {
6868

6969
// Type returns a helper which can produce objects of the given type. Any
7070
// errors are deferred until a further function is called.
71-
func (p *Parser) Type(name string) *ParseableType {
72-
return &ParseableType{
71+
func (p *Parser) Type(name string) ParseableType {
72+
return &parseableType{
7373
parser: p,
7474
typename: name,
7575
}
7676
}
7777

7878
// ParseableType allows for easy production of typed objects.
79-
type ParseableType struct {
79+
type ParseableType interface {
80+
IsValid() bool
81+
FromYAML(YAMLObject) (TypedValue, error)
82+
FromUnstructured(interface{}) (TypedValue, error)
83+
}
84+
85+
type parseableType struct {
8086
parser *Parser
8187
typename string
8288
}
8389

90+
var _ ParseableType = &parseableType{}
91+
8492
// IsValid return true if p's schema and typename are valid.
85-
func (p *ParseableType) IsValid() bool {
93+
func (p *parseableType) IsValid() bool {
8694
_, ok := p.parser.Schema.Resolve(schema.TypeRef{NamedType: &p.typename})
8795
return ok
8896
}
8997

9098
// FromYAML parses a yaml string into an object with the current schema
9199
// and the type "typename" or an error if validation fails.
92-
func (p *ParseableType) FromYAML(object YAMLObject) (TypedValue, error) {
100+
func (p *parseableType) FromYAML(object YAMLObject) (TypedValue, error) {
93101
v, err := value.FromYAML([]byte(object))
94102
if err != nil {
95103
return TypedValue{}, err
@@ -99,7 +107,7 @@ func (p *ParseableType) FromYAML(object YAMLObject) (TypedValue, error) {
99107

100108
// FromUnstructured converts a go interface to a TypedValue. It will return an
101109
// error if the resulting object fails schema validation.
102-
func (p *ParseableType) FromUnstructured(in interface{}) (TypedValue, error) {
110+
func (p *parseableType) FromUnstructured(in interface{}) (TypedValue, error) {
103111
v, err := value.FromUnstructured(in)
104112
if err != nil {
105113
return TypedValue{}, err

0 commit comments

Comments
 (0)