Skip to content

Commit

Permalink
feat(error, set_met): added error & set_meta methods
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed May 25, 2018
1 parent bbd4975 commit fa5ed07
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
50 changes: 43 additions & 7 deletions lib.go → library.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,48 @@ func (c *commit) Do(thread *skylark.Thread, _ *skylark.Builtin, args skylark.Tup
return skylark.True, nil
}

// config fetches configuration details from a qri transformation
type config struct {
// datasetBuiltins defines commands that interact with the context dataset
type datasetBuiltins struct {
ds *dataset.Dataset
}

func newConfig(ds *dataset.Dataset) *config {
return &config{
func newDatasetBuiltins(ds *dataset.Dataset) *datasetBuiltins {
return &datasetBuiltins{
ds: ds,
}
}

// GetConfig returns transformation configuration details
func (c *config) GetConfig(thread *skylark.Thread, _ *skylark.Builtin, args skylark.Tuple, kwargs []skylark.Tuple) (skylark.Value, error) {
if c.ds.Transform.Config == nil {
func (dsb *datasetBuiltins) GetConfig(thread *skylark.Thread, _ *skylark.Builtin, args skylark.Tuple, kwargs []skylark.Tuple) (skylark.Value, error) {
if dsb.ds.Transform.Config == nil {
return skylark.None, nil
}

return parseJSONSkylark(c.ds.Transform.Config)
return parseJSONSkylark(dsb.ds.Transform.Config)
}

// SetMeta sets a dataset meta field
func (dsb *datasetBuiltins) SetMeta(thread *skylark.Thread, _ *skylark.Builtin, args skylark.Tuple, kwargs []skylark.Tuple) (skylark.Value, error) {
var keyx, valx skylark.Value
if err := skylark.UnpackPositionalArgs("set_meta", args, kwargs, 2, &keyx, &valx); err != nil {
return nil, err
}

if keyx.Type() != "string" {
return nil, fmt.Errorf("expected key to be a string")
}

key, err := asString(keyx)
if err != nil {
return nil, fmt.Errorf("parsing string key: %s", err.Error())
}

val, err := parse(valx)
if err != nil {
return nil, err
}

return skylark.None, dsb.ds.Meta.Set(key, val)
}

// httpRequests joins http tools to a dataset, allowing dataset
Expand Down Expand Up @@ -150,3 +174,15 @@ func parseJSONSkylark(data interface{}) (v skylark.Value, err error) {
}
return
}

// Error halts program execution with an error
func Error(thread *skylark.Thread, _ *skylark.Builtin, args skylark.Tuple, kwargs []skylark.Tuple) (skylark.Value, error) {
var msg skylark.Value
if err := skylark.UnpackPositionalArgs("error", args, kwargs, 1, &msg); err != nil {
return nil, err
}
if str, err := asString(msg); err == nil {
return nil, fmt.Errorf("transform error: %s", str)
}
return nil, fmt.Errorf("tranform errored (no valid message provided)")
}
13 changes: 8 additions & 5 deletions transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"bytes"
"fmt"
"io/ioutil"
"log"

"github.com/google/skylark"
"github.com/google/skylark/repl"
Expand Down Expand Up @@ -60,19 +59,23 @@ func ExecFile(ds *dataset.Dataset, filename string, opts ...func(o *ExecOpts)) (
ds.Transform.SyntaxVersion = Version
ds.Transform.Script = bytes.NewReader(scriptdata)

skylark.Universe["error"] = skylark.NewBuiltin("error", Error)

cm := commit{}
cf := newConfig(ds)
hr := newHTTPRequests(ds)
skylark.Universe["commit"] = skylark.NewBuiltin("commit", cm.Do)
skylark.Universe["get_config"] = skylark.NewBuiltin("get_config", cf.GetConfig)

dsb := newDatasetBuiltins(ds)
skylark.Universe["get_config"] = skylark.NewBuiltin("get_config", dsb.GetConfig)
skylark.Universe["set_meta"] = skylark.NewBuiltin("set_meta", dsb.SetMeta)

hr := newHTTPRequests(ds)
skylark.Universe["fetch_json_url"] = skylark.NewBuiltin("fetch_json_url", hr.FetchJSONUrl)

thread := &skylark.Thread{Load: repl.MakeLoad()}

// Execute specified file.
_, err = skylark.ExecFile(thread, filename, nil, nil)
if err != nil {
log.Print(err.Error())
return nil, err
}

Expand Down

0 comments on commit fa5ed07

Please sign in to comment.