Skip to content

Commit

Permalink
Locate and replace multi line redefinitions (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgmitche authored Oct 6, 2020
1 parent 4f44404 commit 276855b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
7 changes: 4 additions & 3 deletions cmd/pkg/copybook/copybook.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ func (c *Copybook) RemoveRecord(r *Record) error {
// over multiple lines in a copybook file, and replace its name with
// the source of the REDEFINES statement, otherwise failing if
// the target is not found.
func (c *Copybook) RedefineRecord(want *Record) error {
func (c *Copybook) RedefineRecord(want *Record, was string) error {
cc := *c
for i, rec := range cc.Records {
if rec.Name == want.Name {
if rec.Name == was {
cc.Records[i] = want
return nil
}
}

return fmt.Errorf("replacement target %s not found", want.Name)
return fmt.Errorf("replacement target %s not found", was)
}
14 changes: 7 additions & 7 deletions cmd/pkg/decoder/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ func (d *decoder) findDataRecord(line string, c *copybook.Copybook) (*copybook.R
return nil, nil

case redefinesMulti:
want, err := d.multiLineRedefinedRecord(line)
want, replace, err := d.multiLineRedefinedRecord(line)
if err != nil {
return nil, err
}

if err := c.RedefineRecord(want); err != nil {
if err := c.RedefineRecord(want, replace); err != nil {
return nil, err
}

Expand Down Expand Up @@ -251,27 +251,27 @@ func (d *decoder) findRedefinesTarget(line string) (*copybook.Record, error) {
// Multi-line:
// 000420 15 DUMMY-5 REDEFINES 00000142
// 000420 DUMMY-4 PIC XX. 00000143
func (d *decoder) multiLineRedefinedRecord(line string) (*copybook.Record, error) {
func (d *decoder) multiLineRedefinedRecord(line string) (*copybook.Record, string, error) {
ss := strings.Split(line, " ")
if len(ss) != multiLineRedefinesSplitSize {
return nil, errors.New("multiLineRedefinedRecord: does not match expected length/format")
return nil, "", errors.New("multiLineRedefinedRecord: does not match expected length/format")
}

if ok := d.s.Scan(); !ok {
d.done = true
return nil, nil
return nil, "", nil
}

replace := string(d.s.Bytes())
r, err := d.findRedefinesTarget(replace)
if err != nil {
return nil, err
return nil, "", err
}

want := *r
want.Name = ss[2]
d.cache.Delete(r.Name)
return d.toCache(&want), nil
return d.toCache(&want), r.Name, nil
}

// redefinedRecord locates a replacement record and removes it
Expand Down
4 changes: 2 additions & 2 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type Copybookexample struct {
DUMMYGROUP1OBJECTC uint `pic:"4"` // start:6 end:9
DUMMYGROUP1OBJECTD string `pic:"40"` // start:10 end:49
DUMMYGROUP1OBJECTE string `pic:"8"` // start:50 end:57
DUMMYGROUP1OBJECTF string `pic:"2"` // start:58 end:59
DUMMYGROUP1OBJECTG string `pic:"2"` // start:58 end:59
DUMMYGROUP1OBJECTH uint `pic:"4"` // start:60 end:63
DUMMYGROUP2OBJECTA string `pic:"14"` // start:64 end:77
DUMMYGROUP2OBJECTB uint `pic:"7"` // start:78 end:84
DUMMYGROUP2OBJECTC string `pic:"4"` // start:85 end:88
DUMMYGROUP2OBJECTD string `pic:"1"` // start:89 end:89
DUMMYGROUP2OBJECTE string `pic:"7"` // start:90 end:96
DUMMYGROUP2OBJECTF string `pic:"7"` // start:90 end:96
DUMMYSUBGROUP2OBJECTA []string `pic:"12,12"` // start:97 end:240
DUMMYGROUP2OBJECTG string `pic:"12"` // start:241 end:252
DUMMYGROUP2OBJECTH string `pic:"12"` // start:253 end:264
Expand Down

0 comments on commit 276855b

Please sign in to comment.