Skip to content

Commit

Permalink
more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hyorigo committed Feb 22, 2024
1 parent e7006c8 commit ba01d36
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dataconv/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ var (
}
)

// shardDictLen returns the length of the underlying dictionary.
func shardDictLen(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 0); err != nil {
return nil, err
Expand All @@ -257,6 +258,8 @@ func shardDictLen(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple,
return starlark.MakeInt(l), nil
}

// shardDictPerform calls the given function with the underlying receiver dictionary, and returns the result.
// The function must be callable, like def perform(fn).
func shardDictPerform(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
// get the perform function
var pr starlark.Value
Expand Down
22 changes: 22 additions & 0 deletions dataconv/share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,28 @@ func TestSharedDict_Functions(t *testing.T) {
`),
wantErr: `len: got 1 arguments, want 0`,
},
{
name: `attr: custom perform`,
script: itn.HereDoc(`
load('share', 'sd')
def act(d):
d["cnt"] = d.get("cnt", 100) + 1
assert.eq(sd.get("cnt"), None)
sd.perform(act)
assert.eq(sd["cnt"], 101)
`),
},
{
name: `attr: custom perform -- error`,
script: itn.HereDoc(`
load('share', 'sd')
def ungood(d):
fail("not good~{}".format(d))
sd["foo"] = "bar"
sd.perform(ungood)
`),
wantErr: `fail: not good~{"foo": "bar"}`,
},
{
name: `attr: custom perform`,
script: itn.HereDoc(`
Expand Down

0 comments on commit ba01d36

Please sign in to comment.