Skip to content

Commit

Permalink
load initial data
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Oct 22, 2023
1 parent 9717a26 commit 16520f1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
14 changes: 12 additions & 2 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ func Load(fileProvider FileProvider, options ...LoadOption) (*Data, error) {
if err != nil {
return nil, err
}
return &l.data, nil
return l.data, nil
}

// WithLoadInitialData sets the initial data. Can be used to merge more items into an existing data.
func WithLoadInitialData(initialData *Data) LoadOption {
return fnLoadOption(func(l *loader) {
l.data = initialData
})
}

// WithLoadProgress sets a callback to report load progress.
Expand All @@ -43,12 +50,15 @@ func WithLoadTaggedValueParser(parser TaggedValueParser) LoadOption {

type loader struct {
fileProvider FileProvider
data Data
data *Data
progress func(filename string)
taggedValueParser []TaggedValueParser
}

func (l *loader) load() error {
if l.data == nil {
l.data = &Data{}
}
return l.fileProvider.Load(func(info FileInfo) error {
if l.progress != nil {
l.progress(info.Name)
Expand Down
55 changes: 55 additions & 0 deletions loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,61 @@ func TestLoad(t *testing.T) {
assert.Equal(t, "janedoe", usersTable.Rows[1].Config.RefID)
}

func TestLoadInitialData(t *testing.T) {
initialProvider := NewFSFileProvider(fstest.MapFS{
"users.dbf.yaml": &fstest.MapFile{
Data: []byte(`users:
config:
table_name: "public.user"
rows:
- user_id: 1
name: "John Doe"
_dbfconfig:
refid: "johndoe"
`),
},
})

initialData, err := Load(initialProvider)
assert.NilError(t, err)

provider := NewFSFileProvider(fstest.MapFS{
"users.dbf.yaml": &fstest.MapFile{
Data: []byte(`users:
config:
table_name: "public.user"
rows:
- user_id: 2
name: "Jane Doe"
_dbfconfig:
refid: "janedoe"
`),
},
})

data, err := Load(provider, WithLoadInitialData(initialData))
assert.NilError(t, err)

usersTable, ok := data.Tables["users"]
assert.Assert(t, ok, "users table not found")

assert.Equal(t, "public.user", usersTable.Config.TableName)

assert.Assert(t, is.Len(usersTable.Rows, 2))

assert.DeepEqual(t, map[string]any{
"user_id": uint64(1),
"name": "John Doe",
}, usersTable.Rows[0].Fields)
assert.Equal(t, "johndoe", usersTable.Rows[0].Config.RefID)

assert.DeepEqual(t, map[string]any{
"user_id": uint64(2),
"name": "Jane Doe",
}, usersTable.Rows[1].Fields)
assert.Equal(t, "janedoe", usersTable.Rows[1].Config.RefID)
}

func TestLoad2TablesSameFile(t *testing.T) {
provider := NewFSFileProvider(fstest.MapFS{
"users.dbf.yaml": &fstest.MapFile{
Expand Down

0 comments on commit 16520f1

Please sign in to comment.