-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
schema: Add most all of our fields (#137)
- Loading branch information
1 parent
367b79e
commit e69169a
Showing
17 changed files
with
1,220 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.