-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from moov-io/4010-837I
support rule of 4010-837I
- Loading branch information
Showing
16 changed files
with
5,123 additions
and
780 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// Copyright 2020 The Moov Authors | ||
// Use of this source code is governed by an Apache License | ||
// license that can be found in the LICENSE file. | ||
|
||
package segments | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/moov-io/x12/pkg/rules" | ||
"github.com/moov-io/x12/pkg/util" | ||
) | ||
|
||
func NewCR6(rule *rules.ElementSetRule) SegmentInterface { | ||
|
||
newSegment := CR6{} | ||
|
||
if rule == nil { | ||
newRule := make(rules.ElementSetRule) | ||
newSegment.SetRule(&newRule) | ||
} else { | ||
newSegment.SetRule(rule) | ||
} | ||
|
||
return &newSegment | ||
} | ||
|
||
type CR6 struct { | ||
Field1 string `index:"01" json:"01,omitempty" xml:"01,omitempty"` | ||
Field2 string `index:"02" json:"02,omitempty" xml:"02,omitempty"` | ||
Field3 string `index:"03" json:"03,omitempty" xml:"03,omitempty"` | ||
Field4 string `index:"04" json:"04,omitempty" xml:"04,omitempty"` | ||
Field5 string `index:"05" json:"05,omitempty" xml:"05,omitempty"` | ||
Field6 string `index:"06" json:"06,omitempty" xml:"06,omitempty"` | ||
Field7 string `index:"07" json:"07,omitempty" xml:"07,omitempty"` | ||
Field8 string `index:"08" json:"08,omitempty" xml:"08,omitempty"` | ||
Field9 string `index:"09" json:"09,omitempty" xml:"09,omitempty"` | ||
Field10 string `index:"10" json:"10,omitempty" xml:"10,omitempty"` | ||
Field11 string `index:"11" json:"11,omitempty" xml:"11,omitempty"` | ||
Field12 string `index:"12" json:"12,omitempty" xml:"12,omitempty"` | ||
Field13 string `index:"13" json:"13,omitempty" xml:"13,omitempty"` | ||
Field14 string `index:"14" json:"14,omitempty" xml:"14,omitempty"` | ||
Field15 string `index:"15" json:"15,omitempty" xml:"15,omitempty"` | ||
Field16 string `index:"16" json:"16,omitempty" xml:"16,omitempty"` | ||
Field17 string `index:"17" json:"17,omitempty" xml:"17,omitempty"` | ||
Field18 string `index:"18" json:"18,omitempty" xml:"18,omitempty"` | ||
Field19 string `index:"19" json:"19,omitempty" xml:"19,omitempty"` | ||
Field20 string `index:"20" json:"20,omitempty" xml:"20,omitempty"` | ||
Field21 string `index:"21" json:"21,omitempty" xml:"21,omitempty"` | ||
|
||
Element | ||
} | ||
|
||
func (r CR6) defaultMask() string { | ||
return rules.MASK_OPTIONAL | ||
} | ||
|
||
func (r CR6) fieldCount() int { | ||
return 21 | ||
} | ||
|
||
func (r CR6) Name() string { | ||
return "CR6" | ||
} | ||
|
||
func (r *CR6) SetFieldByIndex(index string, data any) error { | ||
return util.SetFieldByIndex(r, index, data) | ||
} | ||
|
||
func (r CR6) GetFieldByIndex(index string) any { | ||
return util.GetFieldByIndex(r, index) | ||
} | ||
|
||
func (r *CR6) Validate(rule *rules.ElementSetRule) error { | ||
|
||
if rule == nil { | ||
rule = r.GetRule() | ||
} | ||
|
||
for i := 1; i <= r.fieldCount(); i++ { | ||
|
||
idx := fmt.Sprintf("%02d", i) | ||
|
||
if err := util.ValidateField(r.GetFieldByIndex(idx), rule.Get(idx), r.defaultMask()); err != nil { | ||
return fmt.Errorf("cr6's element (%s) has invalid value, %s", idx, err.Error()) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *CR6) Parse(data string, args ...string) (int, error) { | ||
|
||
var line string | ||
var err error | ||
var size int | ||
|
||
length := util.GetRecordSize(data, args...) | ||
codeLen := len(r.Name()) | ||
read := codeLen + 1 | ||
|
||
if length < int64(read) { | ||
return 0, errors.New("cr6 segment has not enough input data") | ||
} else { | ||
line = data[:length] | ||
} | ||
|
||
if r.Name() != data[:codeLen] { | ||
return 0, errors.New("cr6 segment contains invalid code") | ||
} | ||
|
||
for i := 1; i <= r.fieldCount(); i++ { | ||
|
||
var value string | ||
idx := fmt.Sprintf("%02d", i) | ||
|
||
if value, size, err = util.ReadField(line, read, r.GetRule().Get(idx), r.defaultMask(), args...); err != nil { | ||
return 0, fmt.Errorf("unable to parse cr6's element (%s), %s", idx, err.Error()) | ||
} else { | ||
read += size | ||
r.SetFieldByIndex(idx, value) | ||
} | ||
} | ||
|
||
return read, nil | ||
} | ||
|
||
func (r CR6) String(args ...string) string { | ||
|
||
var buf string | ||
|
||
for i := r.fieldCount(); i > 0; i-- { | ||
|
||
idx := fmt.Sprintf("%02d", i) | ||
value := r.GetFieldByIndex(idx) | ||
|
||
if buf == "" { | ||
mask := r.GetRule().GetMask(idx, r.defaultMask()) | ||
if mask == rules.MASK_NOTUSED { | ||
continue | ||
} | ||
if mask == rules.MASK_OPTIONAL && (value == nil || fmt.Sprintf("%v", value) == "") { | ||
continue | ||
} | ||
} | ||
|
||
if buf == "" { | ||
buf = fmt.Sprintf("%v%s", value, util.GetSegmentTerminator(args...)) | ||
} else { | ||
buf = fmt.Sprintf("%v%s", value, util.DataElementSeparator) + buf | ||
} | ||
} | ||
|
||
if buf == "" { | ||
buf = fmt.Sprintf("%s%s", r.Name(), util.GetSegmentTerminator(args...)) | ||
} else { | ||
buf = fmt.Sprintf("%s%s", r.Name(), util.DataElementSeparator) + buf | ||
} | ||
|
||
return buf | ||
} |
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,90 @@ | ||
// Copyright 2020 The Moov Authors | ||
// Use of this source code is governed by an Apache License | ||
// license that can be found in the LICENSE file. | ||
|
||
package segments | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/moov-io/x12/pkg/rules" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestForCR6(t *testing.T) { | ||
|
||
t.Run("parsing of cr6 segment", func(t *testing.T) { | ||
|
||
seg := NewCR6(nil) | ||
|
||
in := "CR6*19~" | ||
read, err := seg.Parse(in) | ||
require.NoError(t, err) | ||
require.Equal(t, len(in), read) | ||
|
||
in = "CR6*19**********~" | ||
read, err = seg.Parse(in) | ||
require.NoError(t, err) | ||
require.Equal(t, len(in), read) | ||
|
||
in = "CR6*19********~" | ||
read, err = seg.Parse(in) | ||
require.NoError(t, err) | ||
require.Equal(t, len(in), read) | ||
|
||
in = "CR6" | ||
read, err = seg.Parse(in) | ||
require.Error(t, err) | ||
require.Equal(t, "cr6 segment has not enough input data", err.Error()) | ||
require.Equal(t, 0, read) | ||
|
||
in = "PAA~" | ||
read, err = seg.Parse(in) | ||
require.Error(t, err) | ||
require.Equal(t, "cr6 segment contains invalid code", err.Error()) | ||
require.Equal(t, 0, read) | ||
}) | ||
|
||
t.Run("encoding of cr6 segment", func(t *testing.T) { | ||
|
||
seg := NewCR6(nil) | ||
|
||
require.Equal(t, "CR6~", seg.String()) | ||
|
||
in := "CR6*19********~" | ||
_, err := seg.Parse(in) | ||
require.NoError(t, err) | ||
require.Equal(t, "CR6*19~", seg.String()) | ||
|
||
require.NoError(t, seg.Validate(nil)) | ||
}) | ||
|
||
t.Run("parsing and encoding of cr6 segment with specified rule", func(t *testing.T) { | ||
|
||
rule := rules.ElementSetRule{ | ||
"01": {AcceptValues: []string{"19"}, Mask: rules.MASK_REQUIRED}, | ||
"08": {Mask: rules.MASK_OPTIONAL}, | ||
"09": {Mask: rules.MASK_NOTUSED}, | ||
"10": {Mask: rules.MASK_NOTUSED}, | ||
} | ||
|
||
seg := NewCR6(&rule) | ||
|
||
in := "CR6*19********~" | ||
read, err := seg.Parse(in) | ||
require.NoError(t, err) | ||
require.Equal(t, len(in), read) | ||
require.Equal(t, "CR6*19~", seg.String()) | ||
|
||
seg.SetFieldByIndex("01", "20") | ||
err = seg.Validate(nil) | ||
require.Error(t, err) | ||
require.Equal(t, "cr6's element (01) has invalid value, the element contains unexpected value", err.Error()) | ||
|
||
in = "CR6*20********~" | ||
read, err = seg.Parse(in) | ||
require.Error(t, err) | ||
require.Equal(t, "unable to parse cr6's element (01), the element contains unexpected value", err.Error()) | ||
require.Equal(t, 0, read) | ||
}) | ||
} |
Oops, something went wrong.