Skip to content

Commit

Permalink
fix pkey constraint name (#26)
Browse files Browse the repository at this point in the history
* fix pkey constraint name

* remove eywa tag pattern

* add TableName method in Model interface

* use context while querying

* fix actions test

* lint
  • Loading branch information
imperfect-fourth authored Nov 28, 2024
1 parent dba2f95 commit 87a41d1
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 34 deletions.
5 changes: 3 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eywa

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -40,7 +41,7 @@ func NewClient(gqlEndpoint string, opt *ClientOpts) *Client {
return c
}

func (c *Client) Do(q Queryable) (*bytes.Buffer, error) {
func (c *Client) Do(ctx context.Context, q Queryable) (*bytes.Buffer, error) {
reqObj := graphqlRequest{
Query: q.Query(),
Variables: q.Variables(),
Expand All @@ -51,7 +52,7 @@ func (c *Client) Do(q Queryable) (*bytes.Buffer, error) {
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, c.endpoint, &reqBytes)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpoint, &reqBytes)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/eywagen/eywatest/eywa_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package eywatest

import (
"github.com/imperfect-fourth/eywa"
"fmt"
"bytes"
"github.com/google/uuid"
)


var testTable_PkeyConstraint = eywa.Constraint[testTable](fmt.Sprintf("%s_pkey", (new(testTable)).TableName()))
const testTable_Name eywa.FieldName[testTable] = "name"

func testTable_NameField(val string) eywa.Field[testTable] {
Expand Down Expand Up @@ -38,7 +40,6 @@ func testTable_AgeVar(val *int) eywa.Field[testTable] {
Value: eywa.QueryVar("testTable_Age", eywa.NullableIntVar[*int](val)),
}
}
const testTable_PkeyConstraint eywa.Constraint[testTable] = "testTable_pkey"
const testTable_ID eywa.FieldName[testTable] = "id"

func testTable_IDField(val int) eywa.Field[testTable] {
Expand Down Expand Up @@ -86,7 +87,7 @@ func testTable_customVar[T interface{eywa.JSONValue | eywa.JSONBValue;eywa.Typed
}

func testTable_testTable2(subField eywa.FieldName[testTable2], subFields ...eywa.FieldName[testTable2]) eywa.FieldName[testTable] {
buf := bytes.NewBuffer([]byte("test_table2 {\n"))
buf := bytes.NewBuffer([]byte("testTable2 {\n"))
buf.WriteString(string(subField))
for _, f := range subFields {
buf.WriteString("\n")
Expand Down Expand Up @@ -156,7 +157,7 @@ func testTable_FVar(val X[string, int]) eywa.Field[testTable] {
}
}

const testTable2_PkeyConstraint eywa.Constraint[testTable2] = "testTable2_pkey"
var testTable2_PkeyConstraint = eywa.Constraint[testTable2](fmt.Sprintf("%s_pkey", (new(testTable2)).TableName()))
const testTable2_ID eywa.FieldName[testTable2] = "id"

func testTable2_IDField(val uuid.UUID) eywa.Field[testTable2] {
Expand Down
10 changes: 5 additions & 5 deletions cmd/eywagen/eywatest/eywa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestRelationshipSelectQuery(t *testing.T) {

expected := `query get_test_table {
test_table(limit: 2, offset: 1, distinct_on: name, where: {_or: [{name: {_eq: "abcd"}}, {age: {_eq: 10}}]}, order_by: {name: desc}) {
test_table2 {
testTable2 {
id
}
name
Expand Down Expand Up @@ -135,8 +135,8 @@ func TestInsertOneQuery(t *testing.T) {
).Select(
testTable2_ID,
)
expected := fmt.Sprintf(`mutation insert_test_table2_one {
insert_test_table2_one(object: {id: "%s"}) {
expected := fmt.Sprintf(`mutation insert_testTable2_one {
insert_testTable2_one(object: {id: "%s"}) {
id
}
}`, id.String())
Expand Down Expand Up @@ -181,8 +181,8 @@ func TestInsertOneQueryOnConflict(t *testing.T) {
testTable2_ID,
testTable2_Age,
)
expected := fmt.Sprintf(`mutation insert_test_table2_one {
insert_test_table2_one(object: {age: 20, id: "%s"}, on_conflict: {constraint: testTable2_pkey, update_columns: [age]}) {
expected := fmt.Sprintf(`mutation insert_testTable2_one {
insert_testTable2_one(object: {age: 20, id: "%s"}, on_conflict: {constraint: testTable2_pkey, update_columns: [age]}) {
age
id
}
Expand Down
11 changes: 9 additions & 2 deletions cmd/eywagen/eywatest/eywatest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type testTable struct {
ID int `json:"id,omitempty",eywa:"pkey"`
IDd int32 `json:"idd,omitempty"`
custom *customType `json:"custom"`
testTable2 *testTable2 `json:"test_table2"`
testTable2 *testTable2 `json:"testTable2"`
JsonBCol jsonbcol `json:"jsonb_col"`
RR R `json:"r"`
Status eywa.Enum[status] `json:"status"`
Expand All @@ -29,6 +29,10 @@ var (

type R string

func (t testTable) TableName() string {
return "test_table"
}

func (t testTable) ModelName() string {
return "test_table"
}
Expand All @@ -40,8 +44,11 @@ type testTable2 struct {
Age int `json:"age"`
}

func (t testTable2) TableName() string {
return "testTable2"
}
func (t testTable2) ModelName() string {
return "test_table2"
return "testTable2"
}

type jsonbcol struct {
Expand Down
21 changes: 3 additions & 18 deletions cmd/eywagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func usage() {
}

var tagPattern = re.MustCompile(`json:"([^"]+)"`)
var eywaTagPattern = re.MustCompile(`eywa:"([^"]+)"`)

const (
genHeader = "// generated by eywa. DO NOT EDIT. Any changes will be overwritten.\npackage "
Expand Down Expand Up @@ -68,7 +67,7 @@ func %s(subField eywa.FieldName[%s], subFields ...eywa.FieldName[%s]) eywa.Field
)

func pkeyConstraint(typeName string) string {
return fmt.Sprintf("const %s_PkeyConstraint eywa.Constraint[%s] = \"%s_pkey\"\n", typeName, typeName, typeName)
return fmt.Sprintf("var %s_PkeyConstraint = eywa.Constraint[%s](fmt.Sprintf(\"%%s_pkey\", (new(%s)).TableName()))\n", typeName, typeName, typeName)
}

func main() {
Expand Down Expand Up @@ -146,9 +145,8 @@ func parseType(typeName string, pkg *types.Package, contents *fileContent) error
}

contents.content.WriteString("\n")
contents.content.WriteString(pkeyConstraint(typeName))
recurseParse := make([]string, 0, typeStruct.NumFields())
foundPkey := false
pkey := ""
for i := 0; i < typeStruct.NumFields(); i++ {
tag := tagPattern.FindStringSubmatch(typeStruct.Tag(i))
if tag == nil {
Expand All @@ -159,20 +157,7 @@ func parseType(typeName string, pkg *types.Package, contents *fileContent) error
continue
}
fieldName := tagValues[0]
if eywaTag := eywaTagPattern.FindStringSubmatch(typeStruct.Tag(i)); eywaTag != nil {
if eywaTagValues := strings.Split(eywaTag[1], ","); len(eywaTagValues) != 0 {
for _, v := range eywaTagValues {
if v == "pkey" && foundPkey {
return fmt.Errorf("model %s has two primary keys: %s, %s", typeName, pkey, fieldName)
}
if v == "pkey" {
foundPkey = true
contents.content.WriteString(pkeyConstraint(typeName))
pkey = fieldName
}
}
}
}
contents.importsMap["fmt"] = true
field := typeStruct.Field(i)
fieldType := field.Type()
importPackages, fieldTypeNameFull := parseFieldTypeName(field.Type().String(), pkg.Path())
Expand Down
1 change: 1 addition & 0 deletions eywa.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type GraphQLError struct {

type Model interface {
ModelName() string
TableName() string
}

type ModelPtr[T Model] interface {
Expand Down
7 changes: 6 additions & 1 deletion get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eywa

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -82,7 +83,11 @@ func (sq GetQuery[M]) Variables() map[string]interface{} {
}

func (sq GetQuery[M]) Exec(client *Client) ([]M, error) {
respBytes, err := client.Do(sq)
return sq.ExecWithContext(context.Background(), client)
}

func (sq GetQuery[M]) ExecWithContext(ctx context.Context, client *Client) ([]M, error) {
respBytes, err := client.Do(ctx, sq)
if err != nil {
return nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion insert_one.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eywa

import (
"context"
"encoding/json"
"fmt"
)
Expand Down Expand Up @@ -77,7 +78,10 @@ func (iq InsertOneQuery[M]) Variables() map[string]interface{} {
}

func (iq InsertOneQuery[M]) Exec(client *Client) (*M, error) {
respBytes, err := client.Do(iq)
return iq.ExecWithContext(context.Background(), client)
}
func (iq InsertOneQuery[M]) ExecWithContext(ctx context.Context, client *Client) (*M, error) {
respBytes, err := client.Do(ctx, iq)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion unsafe/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package unsafe

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -93,7 +94,7 @@ func (aq ActionQuery[M]) Variables() map[string]interface{} {
}

func (aq ActionQuery[M]) Exec(client *eywa.Client) (*M, error) {
respBytes, err := client.Do(aq)
respBytes, err := client.Do(context.Background(), aq)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions unsafe/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type testAction struct {
func (a testAction) ModelName() string {
return "test_action"
}
func (a testAction) TableName() string {
return "test_action"
}

func TestActionQuery(t *testing.T) {
const state2 = "state2"
Expand Down
7 changes: 6 additions & 1 deletion update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eywa

import (
"context"
"encoding/json"
"fmt"
)
Expand Down Expand Up @@ -81,7 +82,11 @@ func (uq UpdateQuery[M]) Variables() map[string]interface{} {
}

func (uq UpdateQuery[M]) Exec(client *Client) ([]M, error) {
respBytes, err := client.Do(uq)
return uq.ExecWithContext(context.Background(), client)
}

func (uq UpdateQuery[M]) ExecWithContext(ctx context.Context, client *Client) ([]M, error) {
respBytes, err := client.Do(ctx, uq)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 87a41d1

Please sign in to comment.