Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e163197

Browse files
authored
Adding flatfile.RecDecl which will the main unit-of-processing interface for all new flatfile formats that support hierarchy. (#163)
1 parent f8ab8e6 commit e163197

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package flatfile
2+
3+
// RecDecl defines a flat file record. It is meant to be a common facade for all the
4+
// actual unit of processing from each format, such as envelope from fixed length,
5+
// segment from EDI, and record from csv, etc.
6+
type RecDecl interface {
7+
DeclName() string // to avoid collision, since most decl has Name as a field.
8+
Target() bool
9+
Group() bool
10+
MinOccurs() int
11+
MaxOccurs() int
12+
ChildDecls() []RecDecl
13+
}
14+
15+
const (
16+
rootName = "#root"
17+
)
18+
19+
type rootDecl struct {
20+
children []RecDecl
21+
}
22+
23+
func (d rootDecl) DeclName() string { return rootName }
24+
func (d rootDecl) Target() bool { return false }
25+
func (d rootDecl) Group() bool { return true }
26+
func (d rootDecl) MinOccurs() int { return 1 }
27+
func (d rootDecl) MaxOccurs() int { return 1 }
28+
func (d rootDecl) ChildDecls() []RecDecl { return d.children }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package flatfile
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
type testDecl struct {
10+
name string
11+
target bool
12+
group bool
13+
min int
14+
max int
15+
children []testDecl
16+
}
17+
18+
func (d testDecl) DeclName() string { return d.name }
19+
func (d testDecl) Target() bool { return d.target }
20+
func (d testDecl) Group() bool { return d.group }
21+
func (d testDecl) MinOccurs() int { return d.min }
22+
func (d testDecl) MaxOccurs() int { return d.max }
23+
func (d testDecl) ChildDecls() []RecDecl { return toDeclSlice(d.children) }
24+
25+
func toDeclSlice(ds []testDecl) []RecDecl {
26+
if len(ds) <= 0 {
27+
return nil
28+
}
29+
r := make([]RecDecl, len(ds))
30+
for i, d := range ds {
31+
r[i] = d
32+
}
33+
return r
34+
}
35+
36+
func TestRootDecl(t *testing.T) {
37+
rd := rootDecl{
38+
children: toDeclSlice([]testDecl{
39+
{name: "1"},
40+
{name: "2"},
41+
}),
42+
}
43+
assert.Equal(t, rootName, rd.DeclName())
44+
assert.False(t, rd.Target())
45+
assert.True(t, rd.Group())
46+
assert.Equal(t, 1, rd.MinOccurs())
47+
assert.Equal(t, 1, rd.MaxOccurs())
48+
assert.Equal(t, 2, len(rd.ChildDecls()))
49+
assert.Equal(t, "1", rd.ChildDecls()[0].DeclName())
50+
assert.Equal(t, "2", rd.ChildDecls()[1].DeclName())
51+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /