Skip to content

Commit 2c91648

Browse files
committed
extends CompositeWithSubfields to make the interface check no the specific type
1 parent bf743f3 commit 2c91648

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

field/composite.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,23 @@ import (
1717
"github.com/moov-io/iso8583/utils"
1818
)
1919

20+
// CompositeWithSubfields provides methods to manipulate subfields
21+
// and used when composite field is created without
22+
// calling NewComposite e.g. in iso8583.NewMessage(...)
23+
type CompositeWithSubfields interface {
24+
ConstructSubfields()
25+
GetSubfields() map[string]Field
26+
SetSubfields(idPath string, value string) error
27+
SetSubfield(id string, value string) error
28+
UnsetSubfields(idPaths ...string) error
29+
UnsetSubfield(id string)
30+
}
31+
2032
var (
21-
_ Field = (*Composite)(nil)
22-
_ json.Marshaler = (*Composite)(nil)
23-
_ json.Unmarshaler = (*Composite)(nil)
33+
_ Field = (*Composite)(nil)
34+
_ json.Marshaler = (*Composite)(nil)
35+
_ json.Unmarshaler = (*Composite)(nil)
36+
_ CompositeWithSubfields = (*Composite)(nil)
2437
)
2538

2639
// Composite is a wrapper object designed to hold ISO8583 TLVs, subfields and
@@ -93,12 +106,6 @@ func NewComposite(spec *Spec) *Composite {
93106
return f
94107
}
95108

96-
// CompositeWithSubfields is used when composite field is created without
97-
// calling NewComposite e.g. in iso8583.NewMessage(...)
98-
type CompositeWithSubfields interface {
99-
ConstructSubfields()
100-
}
101-
102109
// ConstructSubfields creates subfields according to the spec
103110
// this method is used when composite field is created without
104111
// calling NewComposite (when we create message spec and composite spec)
@@ -732,7 +739,7 @@ func (m *Composite) UnsetSubfields(idPaths ...string) error {
732739
return fmt.Errorf("subfield %s does not exist", id)
733740
}
734741

735-
composite, ok := f.(*Composite)
742+
composite, ok := f.(CompositeWithSubfields)
736743
if !ok {
737744
return fmt.Errorf("field %s is not a composite field and its subfields %s cannot be unset", id, path)
738745
}
@@ -767,7 +774,7 @@ func (m *Composite) SetSubfields(idPath string, value string) error {
767774
}
768775

769776
// If there's a further path, the subfield must be a composite
770-
composite, ok := subfield.(*Composite)
777+
composite, ok := subfield.(CompositeWithSubfields)
771778
if !ok {
772779
return fmt.Errorf("subfield %s is not a composite field and cannot have nested subfields", id)
773780
}

message.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ func (m *Message) UnsetFields(idPaths ...string) error {
631631
return fmt.Errorf("field %d does not exist", idx)
632632
}
633633

634-
composite, ok := f.(*field.Composite)
634+
composite, ok := f.(field.CompositeWithSubfields)
635635
if !ok {
636636
return fmt.Errorf("field %d is not a composite field and its subfields %s cannot be unset", idx, path)
637637
}
@@ -680,7 +680,7 @@ func (m *Message) SetField(path string, val string) error {
680680
}
681681

682682
// If there's a subpath, the field must be a composite
683-
composite, ok := f.(*field.Composite)
683+
composite, ok := f.(field.CompositeWithSubfields)
684684
if !ok {
685685
return fmt.Errorf("field %d is not a composite field and cannot have subfields", fieldID)
686686
}

0 commit comments

Comments
 (0)