Skip to content

Commit

Permalink
Merge pull request starlight-go#6 from awans/datatypes2
Browse files Browse the repository at this point in the history
Datatypes
  • Loading branch information
chasehensel authored Jun 4, 2020
2 parents a196b02 + 2abf172 commit 9891086
Show file tree
Hide file tree
Showing 20 changed files with 914 additions and 300 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ require (
github.com/gorilla/mux v1.7.4
github.com/hashicorp/go-immutable-radix v1.2.0
github.com/json-iterator/go v1.1.9
github.com/starlight-go/starlight v0.0.0-20181207205707-b06f321544f3
github.com/stretchr/testify v1.4.0
go.starlark.net v0.0.0-20200519165436-0aa95694c768 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ github.com/awans/logio v0.0.9 h1:Fyy3YNMgccHKfwaggcgGI8mO1eNwwrs4bmocte6zBcU=
github.com/awans/logio v0.0.9/go.mod h1:0faQuZ4AcaTOCePeYWUS07ZsutyNoIYJIuKTMAtVaoI=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -32,10 +35,15 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLD
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/starlight-go/starlight v0.0.0-20181207205707-b06f321544f3 h1:/fBh1Ot84ILt/ociFHO98wJ9LxIMA3UG8B0unUJPFpY=
github.com/starlight-go/starlight v0.0.0-20181207205707-b06f321544f3/go.mod h1:pxOc2ZuBV+CNlQgzq/HJ9Z9G/eoEMHFeuGohOvva4Co=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
go.starlark.net v0.0.0-20200519165436-0aa95694c768 h1:p1NBjkIS2bHXntFxS9zhyFmZ9VKtazqNnsn5r7okSTo=
go.starlark.net v0.0.0-20200519165436-0aa95694c768/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o=
golang.org/x/sys v0.0.0-20191002063906-3421d5a6bb1c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
10 changes: 5 additions & 5 deletions internal/api/create_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ type UniqueQuery struct {
Val interface{}
}

func newId(st db.Record) error {
func newID(st db.Record) error {
u := uuid.New()
err := st.Set("id", u)
return err
}

func (op CreateOperation) Apply(tx db.RWTx) (db.Record, error) {
err := newId(op.Record)
err := newID(op.Record)
if err != nil {
return nil, err
}
Expand All @@ -52,7 +52,7 @@ func (op CreateOperation) Apply(tx db.RWTx) (db.Record, error) {
}

func (op NestedCreateOperation) ApplyNested(tx db.RWTx, parent db.Record) (err error) {
err = newId(op.Record)
err = newID(op.Record)
if err != nil {
return err
}
Expand All @@ -72,12 +72,12 @@ func (op NestedCreateOperation) ApplyNested(tx db.RWTx, parent db.Record) (err e
return nil
}

func findOneById(tx db.Tx, modelName string, id uuid.UUID) (db.Record, error) {
func findOneByID(tx db.Tx, modelName string, id uuid.UUID) (db.Record, error) {
return tx.FindOne(modelName, db.Eq("id", id))
}

func (op NestedConnectOperation) ApplyNested(tx db.RWTx, parent db.Record) (err error) {
targetModel, err := tx.GetModelById(op.Binding.Dual().ModelId())
targetModel, err := tx.GetModelByID(op.Binding.Dual().ModelID())
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/api/create_op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestCreateApply(t *testing.T) {
op.Apply(tx)
}
for _, findCase := range testCase.output {
found, _ := findOneById(tx, findCase.modelName, findCase.st.Id())
found, _ := findOneByID(tx, findCase.modelName, findCase.st.ID())
if diff := deep.Equal(found, findCase.st); diff != nil {
t.Error(diff)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/api/include_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func (i Include) Resolve(tx db.Tx, rec db.Record) IncludeResult {

func resolve(tx db.Tx, ir *IncludeResult, i Inclusion) error {
rec := ir.Record
id := ir.Record.Id()
id := ir.Record.ID()
b := i.Binding
d := b.Dual()
targetModel, err := tx.GetModelById(d.ModelId())
targetModel, err := tx.GetModelByID(d.ModelID())
if err != nil {
return err
}
Expand Down
21 changes: 11 additions & 10 deletions internal/api/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (p Parser) parseNestedCreate(parentBinding db.Binding, data map[string]inte
unusedKeys[k] = void{}
}

targetModel, err := p.tx.GetModelById(parentBinding.Dual().ModelId())
targetModel, err := p.tx.GetModelByID(parentBinding.Dual().ModelID())
if err != nil {
return
}
Expand Down Expand Up @@ -76,15 +76,16 @@ func (p Parser) parseNestedConnect(parentBinding db.Binding, data map[string]int
if len(data) != 1 {
panic("Too many keys in a unique query")
}
m, err := p.tx.GetModelById(parentBinding.Dual().ModelId())
m, err := p.tx.GetModelByID(parentBinding.Dual().ModelID())
if err != nil {
return
}
// this should be a separate method
var uq UniqueQuery
for k, v := range data {
var val interface{}
val, err = m.AttributeByName(k).ParseFromJson(v)
d := m.AttributeByName(k).Datatype
val, err = d.FromJSON(v)
if err != nil {
return op, fmt.Errorf("error parsing %v %v: %w", m.Name, k, err)
}
Expand Down Expand Up @@ -206,9 +207,9 @@ func (p Parser) ParseFindOne(modelName string, data map[string]interface{}) (op
}

for k, v := range data {
attr := m.AttributeByName(k)
fieldName = db.JsonKeyToFieldName(k)
value, err = attr.ParseFromJson(v)
fieldName = db.JSONKeyToFieldName(k)
d := m.AttributeByName(k).Datatype
value, err = d.FromJSON(v)
if err != nil {
return
}
Expand All @@ -221,7 +222,7 @@ func (p Parser) ParseFindOne(modelName string, data map[string]interface{}) (op
},
ModelName: modelName,
}
return op, nil
return
}

func (p Parser) ParseFindMany(modelName string, data map[string]interface{}) (op FindManyOperation, err error) {
Expand Down Expand Up @@ -344,8 +345,8 @@ func parseFieldCriteria(m db.Model, data map[string]interface{}) (fieldCriteria
}

func parseFieldCriterion(key string, a db.Attribute, value interface{}) (fc FieldCriterion, err error) {
fieldName := db.JsonKeyToFieldName(key)
parsedValue, err := a.ParseFromJson(value)
fieldName := db.JSONKeyToFieldName(key)
parsedValue, err := a.Datatype.FromJSON(value)
fc = FieldCriterion{
// TODO handle function values like {startsWith}
Key: fieldName,
Expand Down Expand Up @@ -388,7 +389,7 @@ func (p Parser) parseAggregateRelationshipCriterion(b db.Binding, value interfac

func (p Parser) parseRelationshipCriterion(b db.Binding, value interface{}) (rc RelationshipCriterion, err error) {
mapValue := value.(map[string]interface{})
m, err := p.tx.GetModelById(b.Dual().ModelId())
m, err := p.tx.GetModelByID(b.Dual().ModelID())
if err != nil {
return
}
Expand Down
102 changes: 102 additions & 0 deletions internal/api/starlark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package api

import (
"awans.org/aft/internal/bus"
"awans.org/aft/internal/db"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

var Andrew = db.Datatype{
ID: uuid.MustParse("46c0ee11-3943-452d-9420-925dd9be8208"),
Name: "andrew",
Validator: AndrewValidator,
StorageType: db.StringType,
}

var AndrewValidator = db.Code{
ID: uuid.MustParse("aaea187b-d153-4c4a-a7e7-cda599b02ba6"),
Name: "andrewValidator",
Runtime: db.Starlark,
Function: db.Validator,
Code: `
def func(arg):
if arg == "Andrew":
return ""
return "arg should be Andrew!!!"
arg.Error = func(arg.Value)
`,
}

var UserStarlark = db.Model{
ID: uuid.MustParse("887a91b8-3857-4b4d-a633-a6386a4fae25"),
Name: "userStarlark",
Attributes: map[string]db.Attribute{
"firstName": db.Attribute{
ID: uuid.MustParse("2afdc6d7-9715-41eb-80d0-20b5132efe94"),
Datatype: Andrew,
},
"lastName": db.Attribute{
ID: uuid.MustParse("462212e7-dd94-403e-8314-e271fd7ccec9"),
Datatype: db.String,
},
"age": db.Attribute{
ID: uuid.MustParse("7b0f19ab-a615-49f7-b5a6-d2054d442a76"),
Datatype: db.Int,
},
"emailAddress": db.Attribute{
ID: uuid.MustParse("0fe6bd01-9828-43ac-b004-37620083344d"),
Datatype: db.EmailAddress,
},
},
LeftRelationships: []db.Relationship{
db.UserPosts,
db.UserProfile,
},
}

func TestCreateAndrewType(t *testing.T) {
appDB := db.New()
tx := appDB.NewRWTx()
r := db.RecordForModel(db.DatatypeModel)
db.SaveDatatype(r, Andrew)
tx.Insert(r)

r = db.RecordForModel(db.CodeModel)
db.SaveCode(r, AndrewValidator)
tx.Insert(r)
tx.SaveModel(UserStarlark)
tx.Commit()

eventbus := bus.New()
db.AddSampleModels(appDB)
req, err := http.NewRequest("POST", "/userStarlark.create", strings.NewReader(
`{"data":{
"firstName":"Chase",
"lastName":"Wansley",
"age": 32,
"emailAddress": "[email protected]",
"profile": {
"create": {
"text": "hello"
}
}
},
"include": {
"profile": true
}
}`))

req = mux.SetURLVars(req, map[string]string{"modelName": "userStarlark"})

cs := CreateHandler{db: appDB, bus: eventbus}
w := httptest.NewRecorder()
err = cs.ServeHTTP(w, req)
assert.Error(t, err)
}
Loading

0 comments on commit 9891086

Please sign in to comment.