forked from moov-io/iso8583
-
Notifications
You must be signed in to change notification settings - Fork 2
/
message_spec.go
40 lines (29 loc) · 916 Bytes
/
message_spec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package iso8583
import (
"reflect"
"github.com/mercadolibre/iso8583/field"
)
type MessageSpec struct {
Name string
Fields map[int]field.Field
}
// Creates a map with new instances of Fields (Field interface)
// based on the field type in MessageSpec.
func (s *MessageSpec) CreateMessageFields() map[int]field.Field {
fields := map[int]field.Field{}
for k, specField := range s.Fields {
fields[k] = createMessageField(specField)
}
return fields
}
func createMessageField(specField field.Field) field.Field {
fieldType := reflect.TypeOf(specField).Elem()
// create new field and convert it to field.Field interface
fl := reflect.New(fieldType).Interface().(field.Field)
fl.SetSpec(specField.Spec())
// if it's a composite field, we have to recusively create its subfields as well
if composite, ok := fl.(field.CompositeWithSubfields); ok {
composite.ConstructSubfields()
}
return fl
}