Skip to content

Commit

Permalink
schema: Add most all of our fields (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
betterengineering authored Feb 1, 2022
1 parent 367b79e commit e69169a
Show file tree
Hide file tree
Showing 17 changed files with 1,220 additions and 8 deletions.
86 changes: 86 additions & 0 deletions schema/datetime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package schema

import (
"fmt"

"github.com/mitchellh/hashstructure/v2"
"go.starlark.net/starlark"
)

type DateTime struct {
SchemaField
}

func newDateTime(
thread *starlark.Thread,
_ *starlark.Builtin,
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
var (
id starlark.String
name starlark.String
desc starlark.String
icon starlark.String
)

if err := starlark.UnpackArgs(
"DateTime",
args, kwargs,
"id", &id,
"name", &name,
"desc", &desc,
"icon", &icon,
); err != nil {
return nil, fmt.Errorf("unpacking arguments for DateTime: %s", err)
}

s := &DateTime{}
s.SchemaField.Type = "datetime"
s.ID = id.GoString()
s.Name = name.GoString()
s.Description = desc.GoString()
s.Icon = icon.GoString()

return s, nil
}

func (s *DateTime) AsSchemaField() SchemaField {
return s.SchemaField
}

func (s *DateTime) AttrNames() []string {
return []string{
"id", "name", "desc", "icon",
}
}

func (s *DateTime) Attr(name string) (starlark.Value, error) {
switch name {

case "id":
return starlark.String(s.ID), nil

case "name":
return starlark.String(s.Name), nil

case "desc":
return starlark.String(s.Description), nil

case "icon":
return starlark.String(s.Icon), nil

default:
return nil, nil
}
}

func (s *DateTime) String() string { return "DateTime(...)" }
func (s *DateTime) Type() string { return "DateTime" }
func (s *DateTime) Freeze() {}
func (s *DateTime) Truth() starlark.Bool { return true }

func (s *DateTime) Hash() (uint32, error) {
sum, err := hashstructure.Hash(s, hashstructure.FormatV2, nil)
return uint32(sum), err
}
41 changes: 41 additions & 0 deletions schema/datetime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package schema_test

import (
"testing"

"github.com/stretchr/testify/assert"
"tidbyt.dev/pixlet/runtime"
)

var dateTimeSource = `
load("schema.star", "schema")
def assert(success, message=None):
if not success:
fail(message or "assertion failed")
t = schema.DateTime(
id = "event_name",
name = "Event Name",
desc = "The time of the event.",
icon = "cog",
)
assert(t.id == "event_name")
assert(t.name == "Event Name")
assert(t.desc == "The time of the event.")
assert(t.icon == "cog")
def main():
return []
`

func TestDateTime(t *testing.T) {
app := &runtime.Applet{}
err := app.Load("date_time.star", []byte(dateTimeSource), nil)
assert.NoError(t, err)

screens, err := app.Run(map[string]string{})
assert.NoError(t, err)
assert.NotNil(t, screens)
}
94 changes: 94 additions & 0 deletions schema/locationbased.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package schema

import (
"fmt"

"github.com/mitchellh/hashstructure/v2"
"go.starlark.net/starlark"
)

type LocationBased struct {
SchemaField
starlarkHandler *starlark.Function
}

func newLocationBased(
thread *starlark.Thread,
_ *starlark.Builtin,
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
var (
id starlark.String
name starlark.String
desc starlark.String
icon starlark.String
handler *starlark.Function
)

if err := starlark.UnpackArgs(
"LocationBased",
args, kwargs,
"id", &id,
"name", &name,
"desc", &desc,
"icon", &icon,
"handler", &handler,
); err != nil {
return nil, fmt.Errorf("unpacking arguments for LocationBased: %s", err)
}

s := &LocationBased{}
s.SchemaField.Type = "locationbased"
s.ID = id.GoString()
s.Name = name.GoString()
s.Description = desc.GoString()
s.Icon = icon.GoString()
s.Handler = handler.Name()
s.starlarkHandler = handler

return s, nil
}

func (s *LocationBased) AsSchemaField() SchemaField {
return s.SchemaField
}

func (s *LocationBased) AttrNames() []string {
return []string{
"id", "name", "desc", "icon", "handler",
}
}

func (s *LocationBased) Attr(name string) (starlark.Value, error) {
switch name {

case "id":
return starlark.String(s.ID), nil

case "name":
return starlark.String(s.Name), nil

case "desc":
return starlark.String(s.Description), nil

case "icon":
return starlark.String(s.Icon), nil

case "handler":
return s.starlarkHandler, nil

default:
return nil, nil
}
}

func (s *LocationBased) String() string { return "LocationBased(...)" }
func (s *LocationBased) Type() string { return "LocationBased" }
func (s *LocationBased) Freeze() {}
func (s *LocationBased) Truth() starlark.Bool { return true }

func (s *LocationBased) Hash() (uint32, error) {
sum, err := hashstructure.Hash(s, hashstructure.FormatV2, nil)
return uint32(sum), err
}
71 changes: 71 additions & 0 deletions schema/locationbased_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package schema_test

import (
"testing"

"github.com/stretchr/testify/assert"
"tidbyt.dev/pixlet/runtime"
)

var locationBasedSource = `
load("encoding/json.star", "json")
load("schema.star", "schema")
DEFAULT_LOCATION = """
{
"lat": "40.6781784",
"lng": "-73.9441579",
"description": "Brooklyn, NY, USA",
"locality": "Brooklyn",
"place_id": "ChIJCSF8lBZEwokRhngABHRcdoI",
"timezone": "America/New_York"
}
"""
def assert(success, message = None):
if not success:
fail(message or "assertion failed")
def get_stations(location):
loc = json.decode(location)
lat, lng = float(loc["lat"]), float(loc["lng"])
return [
schema.Option(
display = "Grand Central",
value = "abc123",
),
schema.Option(
display = "Penn Station",
value = "xyz123",
),
]
t = schema.LocationBased(
id = "station",
name = "Train Station",
desc = "A list of train stations based on a location.",
icon = "train",
handler = get_stations,
)
assert(t.id == "station")
assert(t.name == "Train Station")
assert(t.desc == "A list of train stations based on a location.")
assert(t.icon == "train")
assert(t.handler(DEFAULT_LOCATION)[0].display == "Grand Central")
def main():
return []
`

func TestLocationBased(t *testing.T) {
app := &runtime.Applet{}
err := app.Load("location_based.star", []byte(locationBasedSource), nil)
assert.NoError(t, err)

screens, err := app.Run(map[string]string{})
assert.NoError(t, err)
assert.NotNil(t, screens)
}
18 changes: 12 additions & 6 deletions schema/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ func LoadModule() (starlark.StringDict, error) {
ModuleName: &starlarkstruct.Module{
Name: ModuleName,
Members: starlark.StringDict{
"Schema": starlark.NewBuiltin("Schema", newSchema),
"Toggle": starlark.NewBuiltin("Toggle", newToggle),
"Option": starlark.NewBuiltin("Option", newOption),
"Dropdown": starlark.NewBuiltin("Dropdown", newDropdown),
"Location": starlark.NewBuiltin("Location", newLocation),
"Text": starlark.NewBuiltin("Text", newText),
"Schema": starlark.NewBuiltin("Schema", newSchema),
"Toggle": starlark.NewBuiltin("Toggle", newToggle),
"Option": starlark.NewBuiltin("Option", newOption),
"Dropdown": starlark.NewBuiltin("Dropdown", newDropdown),
"Location": starlark.NewBuiltin("Location", newLocation),
"Text": starlark.NewBuiltin("Text", newText),
"LocationBased": starlark.NewBuiltin("LocationBased", newLocationBased),
"DateTime": starlark.NewBuiltin("DateTime", newDateTime),
"OAuth2": starlark.NewBuiltin("OAuth2", newOAuth2),
"Radio": starlark.NewBuiltin("Radio", newRadio),
"PhotoSelect": starlark.NewBuiltin("PhotoSelect", newPhotoSelect),
"Typeahead": starlark.NewBuiltin("Typeahead", newTypeahead),
},
},
}
Expand Down
Loading

0 comments on commit e69169a

Please sign in to comment.