Skip to content

Commit

Permalink
resolve value parser test
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Oct 23, 2023
1 parent dc186b3 commit 15c684c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (r *resolver) resolve(f ResolveCallback) error {
// check if all ResolveValue fields were resolved.
if _, ok := fieldValue.(ResolveValue); ok {
if rv, ok := ctx.resolved[fieldName]; ok {
if rgen, ok := fieldValue.(ResolveGenerate); ok {
if rgen, ok := fieldValue.(*ResolveGenerate); ok {
rv, err = r.parseResolvedValue(rgen.Type, rv)
if err != nil {
return errors.Join(ResolveCallbackError,
Expand Down
69 changes: 69 additions & 0 deletions resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"testing/fstest"

"github.com/google/uuid"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
Expand Down Expand Up @@ -330,3 +331,71 @@ func TestResolveReturnResolved(t *testing.T) {
assert.Assert(t, is.Len(retData.Tables["tags"].Rows, 1))
assert.Equal(t, 935, retData.Tables["tags"].Rows[0].Fields["tag_id"])
}

func TestResolveGeneratedWithType(t *testing.T) {
provider := NewFSFileProvider(fstest.MapFS{
"users.dbf.yaml": &fstest.MapFile{
Data: []byte(`tags:
rows:
- tag_id: !dbfexpr generated:int
tag_name: "All"
`),
},
})

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

rowCount := map[string]int{}

resolved, err := Resolve(data, func(ctx ResolveContext, fields map[string]any) error {
rowCount[ctx.TableID()]++
assert.DeepEqual(t, &ResolveGenerate{Type: "int"}, fields["tag_id"])
ctx.ResolveField("tag_id", "45")
return nil
})
assert.NilError(t, err)

assert.DeepEqual(t, map[string]int{
"tags": 1,
}, rowCount)

assert.DeepEqual(t, map[string]any{
"tag_id": int64(45),
"tag_name": "All",
}, resolved.Tables["tags"].Rows[0].Fields)
}

func TestResolveGeneratedWithUUIDType(t *testing.T) {
provider := NewFSFileProvider(fstest.MapFS{
"users.dbf.yaml": &fstest.MapFile{
Data: []byte(`tags:
rows:
- tag_id: !dbfexpr generated:uuid
tag_name: "All"
`),
},
})

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

rowCount := map[string]int{}

resolved, err := Resolve(data, func(ctx ResolveContext, fields map[string]any) error {
rowCount[ctx.TableID()]++
assert.DeepEqual(t, &ResolveGenerate{Type: "uuid"}, fields["tag_id"])
ctx.ResolveField("tag_id", "305e1f2b-dfea-4939-862a-069abace0a40")
return nil
}, WithResolvedValueParser(&ResolvedValueParserUUID{}))
assert.NilError(t, err)

assert.DeepEqual(t, map[string]int{
"tags": 1,
}, rowCount)

assert.DeepEqual(t, map[string]any{
"tag_id": uuid.MustParse("305e1f2b-dfea-4939-862a-069abace0a40"),
"tag_name": "All",
}, resolved.Tables["tags"].Rows[0].Fields)
}
25 changes: 25 additions & 0 deletions resolver_value_parser_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package debefix

import (
"fmt"

"github.com/google/uuid"
)

// ResolvedValueParserUUID is a [ResolvedValueParser] to parse "uuid" type to [uuid.UUID].
type ResolvedValueParserUUID struct {
}

func (r ResolvedValueParserUUID) Parse(typ string, value any) (bool, any, error) {
if typ != "uuid" {
return false, nil, nil
}

switch vv := value.(type) {
case uuid.UUID:
return true, vv, nil
default:
v, err := uuid.Parse(fmt.Sprint(value))
return true, v, err
}
}

0 comments on commit 15c684c

Please sign in to comment.