Debian Codegraph @

element/data_element.go

raw
1 package element
2
3 import (
4 "bytes"
5 "fmt"
6 "reflect"
7 "sort"
8 "strconv"
9 "strings"
10 "time"
11
12 "github.com/mitch000001/go-hbci/charset"
13 )
14
15 // DataElement represent the general interface of a DataElement used by HBCI
16 type DataElement interface {
17 // Value returns the underlying value
18 Value() interface{}
19 // IsValid returns true if the DataElement and all its grouped elements
20 // are valid, false otherwise
21 IsValid() bool
22 Length() int
23 String() string
24 MarshalHBCI() ([]byte, error)
25 UnmarshalHBCI([]byte) error
26 }
27
28 // OptionalDataElement represents a DataElement that can be marked as optional or required.
29 type OptionalDataElement interface {
30 DataElement
31 Optional() bool
32 }
33
34 // DataElementGroup represents a DataElement which consists of subelements that
35 // represent a logical group.
36 type DataElementGroup interface {
37 DataElement
38 // GroupDataElements returns the grouped DataElements
39 GroupDataElements() []DataElement
40 }
41
42 // GroupDataElementGroup represents a DataElement which is composed by subelements
43 type GroupDataElementGroup interface {
44 Elements() []DataElement
45 }
46
47 // New returns a new DataElement with the provided type, value and maxLengt for validation
48 func New(typ DataElementType, value interface{}, maxLength int) DataElement {
49 return &basicDataElement{value, typ, maxLength, false}
50 }
51
52 type basicDataElement struct {
53 val interface{}
54 typ DataElementType
55 maxLength int
56 optional bool
57 }
58
59 // Value returns the underlying value
60 func (d *basicDataElement) Value() interface{} { return d.val }
61 func (d *basicDataElement) Type() DataElementType { return d.typ }
62 func (d *basicDataElement) IsValid() bool { return d.Length() <= d.maxLength }
63 func (d *basicDataElement) Length() int { return len(d.String()) }
64 func (d *basicDataElement) String() string { return fmt.Sprintf("%v", d.val) }
65 func (d *basicDataElement) Optional() bool { return d.optional }
66 func (d *basicDataElement) SetOptional() { d.optional = true }
67 func (d *basicDataElement) MarshalHBCI() ([]byte, error) {
68 return charset.ToISO8859_1(d.String()), nil
69 }
70 func (d *basicDataElement) UnmarshalHBCI(value []byte) error {
71 return fmt.Errorf("Not implemented")
72 }
73 func (d *basicDataElement) MarshalYAML() (interface{}, error) {
74 return d.val, nil
75 }
76
77 // NewDataElementGroup returns a new DataElement that embodies group
78 func NewDataElementGroup(typ DataElementType, elementCount int, group DataElementGroup) DataElement {
79 return &elementGroup{elements: group.GroupDataElements, elementCount: elementCount, typ: typ}
80 }
81
82 // NewGroupDataElementGroup returns a new DataElement which embodies group
83 func NewGroupDataElementGroup(typ DataElementType, elementCount int, group GroupDataElementGroup) DataElement {
84 return &elementGroup{elements: group.Elements, elementCount: elementCount, typ: typ}
85 }
86
87 // groupDataElementGroup defines a group of GroupDataElements.
88 // It implements the DataElement and the DataElementGroup interface
89 type elementGroup struct {
90 elements func() []DataElement
91 typ DataElementType
92 elementCount int
93 optional bool
94 }
95
96 // Value returns the values of all GroupDataElements as []interface{}
97 func (g *elementGroup) Value() interface{} {
98 values := make([]interface{}, len(g.elements()))
99 for i, elem := range g.elements() {
100 if !reflect.ValueOf(elem).IsNil() {
101 values[i] = elem.Value()
102 }
103 }
104 return values
105 }
106
107 func (g *elementGroup) Type() DataElementType { return g.typ }
108
109 func (g *elementGroup) IsValid() bool {
110 if g.elementCount != len(g.elements()) {
111 return false
112 }
113 for _, elem := range g.elements() {
114 if !reflect.ValueOf(elem).IsNil() {
115 if !elem.IsValid() {
116 return false
117 }
118 }
119 }
120 return true
121 }
122
123 func (g *elementGroup) Length() int {
124 length := 0
125 for _, elem := range g.elements() {
126 if !reflect.ValueOf(elem).IsNil() {
127 length += elem.Length()
128 }
129 }
130 return length
131 }
132
133 func (g *elementGroup) String() string {
134 elementStrings := make([]string, len(g.elements()))
135 for i, e := range g.elements() {
136 if !reflect.ValueOf(e).IsNil() {
137 elementStrings[i] = e.String()
138 }
139 }
140 return strings.Join(elementStrings, ":")
141 }
142
143 func (g *elementGroup) MarshalHBCI() ([]byte, error) {
144 elementBytes := make([][]byte, len(g.elements()))
145 for i, e := range g.elements() {
146 if !reflect.ValueOf(e).IsNil() {
147 marshaled, err := e.MarshalHBCI()
148 if err != nil {
149 return nil, err
150 }
151 elementBytes[i] = marshaled
152 }
153 }
154 marshaled := bytes.Join(elementBytes, []byte(":"))
155 marshaled = bytes.TrimRight(marshaled, ":")
156 return marshaled, nil
157 }
158
159 func (g *elementGroup) UnmarshalHBCI(value []byte) error {
160 marshaledElements := bytes.Split(value, []byte(":"))
161 var errors []string
162 elements := g.elements()
163 for i, elem := range marshaledElements {
164 if len(elem) == 0 {
165 continue
166 }
167 elemToMarshal := reflect.New(reflect.TypeOf(elements[i]).Elem())
168 err := elemToMarshal.Interface().(DataElement).UnmarshalHBCI(elem)
169 if err == nil {
170 elements[i] = elemToMarshal.Interface().(DataElement)
171 } else {
172 errors = append(errors, fmt.Sprintf("%T:%q", err, err))
173 }
174 }
175 if len(errors) != 0 {
176 return fmt.Errorf("Errors while unmarshaling elements:\n%s", strings.Join(errors, "\n"))
177 }
178 return nil
179 }
180
181 func (g *elementGroup) Optional() bool {
182 return g.optional
183 }
184
185 func (g *elementGroup) SetOptional() {
186 g.optional = true
187 }
188
189 // newArrayElementGroup returns a new DataElement which has multiple occurrences in its parents
190 func newArrayElementGroup(typ DataElementType, min, max int, elems []DataElement) *arrayElementGroup {
191 e := &arrayElementGroup{
192 array: elems,
193 minLength: min,
194 maxLength: max,
195 }
196 e.DataElement = NewDataElementGroup(typ, len(elems), e)
197 return e
198 }
199
200 type arrayElementGroup struct {
201 DataElement
202 minLength int
203 maxLength int
204 array []DataElement
205 }
206
207 func (a *arrayElementGroup) IsValid() bool {
208 if len(a.array) < a.minLength || len(a.array) > a.maxLength {
209 return false
210 }
211 return a.DataElement.IsValid()
212 }
213
214 // GroupDataElements returns the grouped DataElements
215 func (a *arrayElementGroup) GroupDataElements() []DataElement {
216 return a.array
217 }
218
219 func (a *arrayElementGroup) MarshalYAML() (interface{}, error) {
220 return a.array, nil
221 }
222
223 func escape(in string) string {
224 replacer := strings.NewReplacer("?", "??", "@", "?@", "'", "?'", ":", "?:", "+", "?+")
225 return replacer.Replace(in)
226 }
227
228 func unescape(in string) string {
229 replacer := strings.NewReplacer("??", "?", "?@", "@", "?'", "'", "?:", ":", "?+", "+")
230 return replacer.Replace(in)
231 }
232
233 // NewAlphaNumeric returns a new AlphaNumeric DataElement
234 func NewAlphaNumeric(val string, maxLength int) *AlphaNumericDataElement {
235 return &AlphaNumericDataElement{&basicDataElement{val, alphaNumericDE, maxLength, false}}
236 }
237
238 // An AlphaNumericDataElement represents a DataElement which contains alphanumeric characters
239 type AlphaNumericDataElement struct {
240 *basicDataElement
241 }
242
243 // Val returns the data as string
244 func (a *AlphaNumericDataElement) Val() string { return a.val.(string) }
245
246 // IsValid returns false if a contains '\n' and '\r', true otherwise
247 func (a *AlphaNumericDataElement) IsValid() bool {
248 if strings.ContainsAny(a.Val(), "\n & \r") {
249 return false
250 }
251 return a.basicDataElement.IsValid()
252 }
253
254 func (a *AlphaNumericDataElement) String() string {
255 return escape(a.basicDataElement.String())
256 }
257
258 // MarshalHBCI marshals a into a byte representation ready to be sent over the wire.
259 func (a *AlphaNumericDataElement) MarshalHBCI() ([]byte, error) {
260 val := charset.ToISO8859_1(escape(a.basicDataElement.String()))
261 return val, nil
262 }
263
264 // UnmarshalHBCI unmarshals the given bytes into a
265 func (a *AlphaNumericDataElement) UnmarshalHBCI(value []byte) error {
266 decoded := charset.ToUTF8(value)
267 unescaped := unescape(decoded)
268 *a = AlphaNumericDataElement{&basicDataElement{unescaped, alphaNumericDE, len(unescaped), false}}
269 return nil
270 }
271
272 // NewText returns a new TextDataElement
273 func NewText(val string, maxLength int) *TextDataElement {
274 return &TextDataElement{&basicDataElement{val, textDE, maxLength, false}}
275 }
276
277 // TextDataElement represents a DataElement that can hold alphanumeric
278 // characters, but also '\n' and '\r'
279 type TextDataElement struct {
280 *basicDataElement
281 }
282
283 // Val returns the value of a as a string
284 func (a *TextDataElement) Val() string { return a.val.(string) }
285 func (a *TextDataElement) String() string {
286 return escape(a.basicDataElement.String())
287 }
288
289 // MarshalHBCI marshals a into the HBCI wire format
290 func (a *TextDataElement) MarshalHBCI() ([]byte, error) {
291 val := charset.ToISO8859_1(escape(a.basicDataElement.String()))
292 return val, nil
293 }
294
295 // UnmarshalHBCI unmarshals value into a
296 func (a *TextDataElement) UnmarshalHBCI(value []byte) error {
297 decoded := charset.ToUTF8(value)
298 unescaped := unescape(decoded)
299 *a = TextDataElement{&basicDataElement{unescaped, textDE, len(unescaped), false}}
300 return nil
301 }
302
303 // NewDigit returns a new DigitDataElement
304 func NewDigit(val, maxLength int) *DigitDataElement {
305 return &DigitDataElement{&basicDataElement{val, digitDE, maxLength, false}}
306 }
307
308 // DigitDataElement represents numbers from 0 to 9 with leading zeros
309 type DigitDataElement struct {
310 *basicDataElement
311 }
312
313 // Val returns the digit as int
314 func (d *DigitDataElement) Val() int { return d.val.(int) }
315
316 func (d *DigitDataElement) String() string {
317 fmtString := fmt.Sprintf("%%0%dd", d.maxLength)
318 return fmt.Sprintf(fmtString, d.Val())
319 }
320
321 // MarshalHBCI marshals d into the HBCI wire format
322 func (d *DigitDataElement) MarshalHBCI() ([]byte, error) {
323 return charset.ToISO8859_1(d.String()), nil
324 }
325
326 // UnmarshalHBCI unmarshals value into d
327 func (d *DigitDataElement) UnmarshalHBCI(value []byte) error {
328 val, err := strconv.Atoi(charset.ToUTF8(value))
329 if err != nil {
330 return err
331 }
332 *d = DigitDataElement{&basicDataElement{val, digitDE, len(value), false}}
333 return nil
334 }
335
336 // NewNumber returns a new NumberDataElement
337 func NewNumber(val, maxLength int) *NumberDataElement {
338 return &NumberDataElement{&basicDataElement{val, numberDE, maxLength, false}}
339 }
340
341 // A NumberDataElement represents a number containing 0 - 9 without leading zeros
342 type NumberDataElement struct {
343 *basicDataElement
344 }
345
346 // Val returns the value of n as int
347 func (n *NumberDataElement) Val() int { return n.val.(int) }
348
349 // MarshalHBCI marshals n into the HBCI wire format
350 func (n *NumberDataElement) MarshalHBCI() ([]byte, error) {
351 return charset.ToISO8859_1(n.String()), nil
352 }
353
354 // UnmarshalHBCI unmarshals value into n
355 func (n *NumberDataElement) UnmarshalHBCI(value []byte) error {
356 val, err := strconv.Atoi(charset.ToUTF8(value))
357 if err != nil {
358 return err
359 }
360 *n = NumberDataElement{&basicDataElement{val, numberDE, len(value), false}}
361 return nil
362 }
363
364 // NewFloat returns a new FloatDataElement
365 func NewFloat(val float64, maxLength int) *FloatDataElement {
366 return &FloatDataElement{&basicDataElement{val, floatDE, maxLength, false}}
367 }
368
369 // FloatDataElement represents a float in HBCI
370 type FloatDataElement struct {
371 *basicDataElement
372 }
373
374 // Val returns the value of f as float64
375 func (f *FloatDataElement) Val() float64 { return f.val.(float64) }
376
377 func (f *FloatDataElement) String() string {
378 str := strconv.FormatFloat(f.Val(), 'f', -1, 64)
379 str = strings.Replace(str, ".", ",", 1)
380 if !strings.Contains(str, ",") {
381 str = str + ","
382 }
383 return str
384 }
385
386 // MarshalHBCI marshals f into HBCI wire format
387 func (f *FloatDataElement) MarshalHBCI() ([]byte, error) {
388 return charset.ToISO8859_1(f.String()), nil
389 }
390
391 // UnmarshalHBCI unmarshals value into f
392 func (f *FloatDataElement) UnmarshalHBCI(value []byte) error {
393 str := strings.Replace(charset.ToUTF8(value), ",", ".", 1)
394 val, err := strconv.ParseFloat(str, 64)
395 if err != nil {
396 return err
397 }
398 *f = FloatDataElement{&basicDataElement{val, floatDE, len(value), false}}
399 return nil
400 }
401
402 // NewDtausCharset returns a new DtausCharsetDataElement
403 func NewDtausCharset(data []byte, maxLength int) *DtausCharsetDataElement {
404 b := NewBinary(data, maxLength)
405 b.typ = dtausCharsetDE
406 return &DtausCharsetDataElement{b}
407 }
408
409 // DtausCharsetDataElement represents binary data in DTAUS charset
410 type DtausCharsetDataElement struct {
411 *BinaryDataElement
412 }
413
414 // UnmarshalHBCI unmarshals value into d
415 func (d *DtausCharsetDataElement) UnmarshalHBCI(value []byte) error {
416 var bin BinaryDataElement
417 err := bin.UnmarshalHBCI(value)
418 if err != nil {
419 return err
420 }
421 *d = DtausCharsetDataElement{&bin}
422 return nil
423 }
424
425 // NewBinary returns a new BinaryDataElement
426 func NewBinary(data []byte, maxLength int) *BinaryDataElement {
427 return &BinaryDataElement{&basicDataElement{data, binaryDE, maxLength, false}}
428 }
429
430 // A BinaryDataElement embodies binary data into a DataElement
431 type BinaryDataElement struct {
432 *basicDataElement
433 }
434
435 // Val returns b as []byte
436 func (b *BinaryDataElement) Val() []byte {
437 return b.Value().([]byte)
438 }
439
440 func (b *BinaryDataElement) String() string {
441 return fmt.Sprintf("@%d@%s", len(b.Val()), b.Val())
442 }
443
444 // MarshalHBCI marshals b into HBCI wire format
445 func (b *BinaryDataElement) MarshalHBCI() ([]byte, error) {
446 var buf []byte
447 binaryFormat := fmt.Sprintf("@%d@", len(b.Val()))
448 buf = append(buf, charset.ToISO8859_1(binaryFormat)...)
449 buf = append(buf, b.Val()...)
450 return buf, nil
451 }
452
453 // UnmarshalHBCI unmarshals value into b
454 func (b *BinaryDataElement) UnmarshalHBCI(value []byte) error {
455 buf := bytes.NewBuffer(value)
456 r, _, err := buf.ReadRune()
457 if err != nil {
458 return err
459 }
460 if r != '@' {
461 return fmt.Errorf("BinaryDataElement: Malformed input")
462 }
463 binSizeStr, err := buf.ReadString('@')
464 if err != nil {
465 return err
466 }
467 binSize, err := strconv.Atoi(binSizeStr[:len(binSizeStr)-1])
468 if err != nil {
469 return fmt.Errorf("Error while parsing binary size: %T:%v", err, err)
470 }
471 binData := make([]byte, binSize)
472 _, err = buf.Read(binData)
473 if err != nil {
474 return fmt.Errorf("Error while reading binary data: %T:%v", err, err)
475 }
476 *b = BinaryDataElement{&basicDataElement{binData, binaryDE, binSize, false}}
477 return nil
478 }
479
480 // NewBoolean returns a new BooleanDataElement
481 func NewBoolean(val bool) *BooleanDataElement {
482 return &BooleanDataElement{&basicDataElement{val, booleanDE, 1, false}}
483 }
484
485 // A BooleanDataElement represents a boolean in HBCI
486 type BooleanDataElement struct {
487 *basicDataElement
488 }
489
490 // Val returns the value of b as bool
491 func (b *BooleanDataElement) Val() bool {
492 return b.Value().(bool)
493 }
494
495 func (b *BooleanDataElement) String() string {
496 if b.Val() {
497 return "J"
498 }
499 return "N"
500 }
501
502 // MarshalHBCI marshals b into the HBCI wire format
503 func (b *BooleanDataElement) MarshalHBCI() ([]byte, error) {
504 return charset.ToISO8859_1(b.String()), nil
505 }
506
507 // UnmarshalHBCI unmarshals value into b
508 func (b *BooleanDataElement) UnmarshalHBCI(value []byte) error {
509 val := charset.ToUTF8(value)
510 if val == "J" {
511 *b = BooleanDataElement{&basicDataElement{true, booleanDE, 1, false}}
512 } else if val == "N" {
513 *b = BooleanDataElement{&basicDataElement{false, booleanDE, 1, false}}
514 } else {
515 return fmt.Errorf("BooleanDataElement: Malformed input")
516 }
517 return nil
518 }
519
520 // NewCode returns a new CodeDataElement
521 func NewCode(val string, maxLength int, validSet []string) *CodeDataElement {
522 sort.Strings(validSet)
523 return &CodeDataElement{
524 AlphaNumericDataElement: NewAlphaNumeric(val, maxLength),
525 validSet: validSet,
526 }
527 }
528
529 // A CodeDataElement represents a value from a know bounded set of values
530 type CodeDataElement struct {
531 *AlphaNumericDataElement
532 validSet []string
533 }
534
535 // Type returns the DataElementType of c
536 func (c *CodeDataElement) Type() DataElementType {
537 return codeDE
538 }
539
540 // IsValid returns true if the value is in the valid set and the underlying
541 // AlphaNumericDataElement is valid, false otherwise.
542 func (c *CodeDataElement) IsValid() bool {
543 if sort.SearchStrings(c.validSet, c.Val()) >= len(c.validSet) {
544 return false
545 }
546 return c.AlphaNumericDataElement.IsValid()
547 }
548
549 // UnmarshalHBCI unmarshals value into a. a will always be invalid, because the
550 // valid set is not known when unmarshaling
551 func (c *CodeDataElement) UnmarshalHBCI(value []byte) error {
552 c.AlphaNumericDataElement = &AlphaNumericDataElement{}
553 return c.AlphaNumericDataElement.UnmarshalHBCI(value)
554 }
555
556 // NewDate returns a new DateDataElement
557 func NewDate(date time.Time) *DateDataElement {
558 return &DateDataElement{&basicDataElement{date, dateDE, 8, false}}
559 }
560
561 // A DateDataElement represents a date within HBCI. It does not include a time.
562 type DateDataElement struct {
563 *basicDataElement
564 }
565
566 // Val returns the value of d as time.Time. The time component of it will always be zero.
567 func (d *DateDataElement) Val() time.Time {
568 return d.Value().(time.Time)
569 }
570
571 func (d *DateDataElement) String() string {
572 return d.Val().Format("20060102")
573 }
574
575 // MarshalHBCI marshals d into HBCI wire format
576 func (d *DateDataElement) MarshalHBCI() ([]byte, error) {
577 return charset.ToISO8859_1(d.String()), nil
578 }
579
580 // UnmarshalHBCI unmarshals value into d
581 func (d *DateDataElement) UnmarshalHBCI(value []byte) error {
582 t, err := time.Parse("20060102", charset.ToUTF8(value))
583 if err != nil {
584 return err
585 }
586 *d = DateDataElement{&basicDataElement{t, dateDE, 8, false}}
587 return nil
588 }
589
590 // IsValid returns true if the underlying date is not Zero.
591 func (d *DateDataElement) IsValid() bool {
592 return !d.Val().IsZero()
593 }
594
595 // NewVirtualDate returns a new VirtualDateDataElement
596 func NewVirtualDate(date int) *VirtualDateDataElement {
597 n := NewNumber(date, 8)
598 n.typ = virtualDateDE
599 return &VirtualDateDataElement{n}
600 }
601
602 // VirtualDateDataElement represents a virtual date
603 // TODO: modelling a virtual date?!
604 type VirtualDateDataElement struct {
605 *NumberDataElement
606 }
607
608 // Valid returns true if the length of v is 8, false otherwise
609 func (v *VirtualDateDataElement) Valid() bool {
610 return v.Length() == 8
611 }
612
613 // NewTime returns a new TimeDataElement
614 func NewTime(date time.Time) *TimeDataElement {
615 return &TimeDataElement{&basicDataElement{date, dateDE, 6, false}}
616 }
617
618 // A TimeDataElement represents a time of a date component. It always contains
619 // a time component, but the date components will always be 0, i.e. 0000-00-00.
620 type TimeDataElement struct {
621 *basicDataElement
622 }
623
624 // Val returns the value of t as time.Time.
625 func (t *TimeDataElement) Val() time.Time {
626 return t.Value().(time.Time)
627 }
628
629 func (t *TimeDataElement) String() string {
630 return t.Val().Format("150405")
631 }
632
633 // MarshalHBCI marshals t into HBCI wire format
634 func (t *TimeDataElement) MarshalHBCI() ([]byte, error) {
635 return charset.ToISO8859_1(t.String()), nil
636 }
637
638 // UnmarshalHBCI unmarshals value into t
639 func (t *TimeDataElement) UnmarshalHBCI(value []byte) error {
640 parsedTime, err := time.Parse("150405", charset.ToUTF8(value))
641 if err != nil {
642 return err
643 }
644 *t = TimeDataElement{&basicDataElement{parsedTime, timeDE, 6, false}}
645 return nil
646 }
647
648 // IsValid returns true if the underlying date is not Zero.
649 func (t *TimeDataElement) IsValid() bool {
650 return !t.Val().IsZero()
651 }
652
653 // NewIdentification returns a new IdentificationDataElement
654 func NewIdentification(id string) *IdentificationDataElement {
655 an := NewAlphaNumeric(id, 30)
656 an.typ = identificationDE
657 return &IdentificationDataElement{an}
658 }
659
660 // An IdentificationDataElement represents unique identifier. It is
661 // only valid if the underlying id is 30 characters or less.
662 type IdentificationDataElement struct {
663 *AlphaNumericDataElement
664 }
665
666 // Type returns the DataElementType of i
667 func (i *IdentificationDataElement) Type() DataElementType {
668 return identificationDE
669 }
670
671 // UnmarshalHBCI unmarshals value into i
672 func (i *IdentificationDataElement) UnmarshalHBCI(value []byte) error {
673 var alpha AlphaNumericDataElement
674 err := alpha.UnmarshalHBCI(value)
675 if err != nil {
676 return err
677 }
678 *i = IdentificationDataElement{&alpha}
679 return nil
680 }
681
682 // NewCountryCode returns a new CountryCodeDataElement
683 func NewCountryCode(code int) *CountryCodeDataElement {
684 d := NewDigit(code, 3)
685 d.typ = countryCodeDE
686 return &CountryCodeDataElement{d}
687 }
688
689 // A CountryCodeDataElement represents a Country code as defined by ISO 3166-1
690 // (numeric code)
691 type CountryCodeDataElement struct {
692 *DigitDataElement
693 }
694
695 // Type represents the DataElement Type of c
696 func (c *CountryCodeDataElement) Type() DataElementType {
697 return countryCodeDE
698 }
699
700 // UnmarshalHBCI unmarshals value into c
701 func (c *CountryCodeDataElement) UnmarshalHBCI(value []byte) error {
702 var d DigitDataElement
703 err := d.UnmarshalHBCI(value)
704 if err != nil {
705 return nil
706 }
707 *c = CountryCodeDataElement{&d}
708 return nil
709 }
710
711 // NewCurrency returns a new CurrencyDataElement
712 func NewCurrency(cur string) *CurrencyDataElement {
713 an := NewAlphaNumeric(cur, 3)
714 an.typ = currencyDE
715 return &CurrencyDataElement{an}
716 }
717
718 // A CurrencyDataElement represents a currency code as defined by ISO 4217
719 // alpha-3 in upcase letters
720 type CurrencyDataElement struct {
721 *AlphaNumericDataElement
722 }
723
724 // IsValid returns true if the currency format matches ISO 4217 alpha-3
725 func (c *CurrencyDataElement) IsValid() bool {
726 return c.Length() == 3
727 }
728
729 // Type returns the DataElementType of c
730 func (c *CurrencyDataElement) Type() DataElementType {
731 return currencyDE
732 }
733
734 // UnmarshalHBCI unmarshals value into c
735 func (c *CurrencyDataElement) UnmarshalHBCI(value []byte) error {
736 var a AlphaNumericDataElement
737 err := a.UnmarshalHBCI(value)
738 if err != nil {
739 return err
740 }
741 *c = CurrencyDataElement{&a}
742 return nil
743 }
744
745 // NewValue returns a new ValueDataElement
746 func NewValue(val float64) *ValueDataElement {
747 f := NewFloat(val, 15)
748 f.typ = valueDE
749 return &ValueDataElement{f}
750 }
751
752 // A ValueDataElement represents a float which can have upto 15 characters
753 type ValueDataElement struct {
754 *FloatDataElement
755 }
756
757 // Type returns the DataElementType of v
758 func (v *ValueDataElement) Type() DataElementType {
759 return valueDE
760 }
761
762 // UnmarshalHBCI unmarshals value into v
763 func (v *ValueDataElement) UnmarshalHBCI(value []byte) error {
764 var f FloatDataElement
765 err := f.UnmarshalHBCI(value)
766 if err != nil {
767 return err
768 }
769 *v = ValueDataElement{&f}
770 return nil
771 }