Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: db 2.0 enforced relations, enum string literals & more funcs #328

Merged
merged 10 commits into from
Dec 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix
marcbinz committed Dec 12, 2024
commit 7e7754a45ae1aeafab9808112bd1aa470312de41
3 changes: 0 additions & 3 deletions core/codegen/base.builder.go
Original file line number Diff line number Diff line change
@@ -117,9 +117,6 @@ func (b *build) buildInterfaceFile() error {
func (b *build) buildSchemaFile() error {
statements := []string{string(embed.CodegenComment), ""}

// TODO: literal types
// https://github.com/surrealdb/surrealdb/pull/4557

var fieldFn func(table string, f field.Field, prefix string)
fieldFn = func(table string, f field.Field, prefix string) {
fieldType := f.TypeDatabase()
10 changes: 5 additions & 5 deletions core/codegen/field/field.enum.go
Original file line number Diff line number Diff line change
@@ -33,18 +33,18 @@ func (f *Enum) TypeDatabase() string {

sort.Strings(f.values)

var values []string
var formattedValues []string
for _, value := range f.values {
values = append(values, fmt.Sprintf(`"%s"`, value))
formattedValues = append(formattedValues, fmt.Sprintf(`"%s"`, value))
}

in := strings.Join(values, ", ")
literals := strings.Join(formattedValues, " | ")

if f.source.Pointer() {
return "option<string | null> ASSERT $value == NULL OR $value INSIDE [" + in + "]"
return "option<" + literals + " | null>"
}

return "string ASSERT $value INSIDE [" + in + "]"
return literals
}

func (f *Enum) CodeGen() *CodeGen {
2 changes: 1 addition & 1 deletion core/codegen/field/field.node.go
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ func (f *Node) typeConv(_ Context) jen.Code {

func (f *Node) TypeDatabase() string {
// Linked records are always considered optional.
return fmt.Sprintf("option<record<%s>>", f.table.NameDatabase())
return fmt.Sprintf("option<record<%s> | null>", f.table.NameDatabase())
}

func (f *Node) Table() *NodeTable {
2 changes: 2 additions & 0 deletions internal/tests/basic/gen/som/internal/lib/builder.query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/tests/basic/gen/som/internal/lib/filter.string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions internal/tests/basic/gen/som/schema/tables.surql
Original file line number Diff line number Diff line change
@@ -90,14 +90,14 @@ DEFINE FIELD url_ptr ON TABLE all_field_types TYPE option<string | null> ASSERT
DEFINE FIELD url_nil ON TABLE all_field_types TYPE option<string | null> ASSERT $value == NONE OR $value == NULL OR string::is::url($value);
DEFINE FIELD url_slice ON TABLE all_field_types TYPE option<array | null>;
DEFINE FIELD url_slice.* ON TABLE all_field_types TYPE string ASSERT $value == "" OR string::is::url($value);
DEFINE FIELD role ON TABLE all_field_types TYPE string ASSERT $value INSIDE ["", "admin", "user"];
DEFINE FIELD enum_ptr ON TABLE all_field_types TYPE option<string | null> ASSERT $value == NULL OR $value INSIDE ["", "admin", "user"];
DEFINE FIELD role ON TABLE all_field_types TYPE "" | "admin" | "user";
DEFINE FIELD enum_ptr ON TABLE all_field_types TYPE option<"" | "admin" | "user" | null>;
DEFINE FIELD roles ON TABLE all_field_types TYPE option<array | null>;
DEFINE FIELD roles.* ON TABLE all_field_types TYPE string ASSERT $value INSIDE ["", "admin", "user"];
DEFINE FIELD roles.* ON TABLE all_field_types TYPE "" | "admin" | "user";
DEFINE FIELD enum_ptr_slice ON TABLE all_field_types TYPE option<array | null>;
DEFINE FIELD enum_ptr_slice.* ON TABLE all_field_types TYPE option<string | null> ASSERT $value == NULL OR $value INSIDE ["", "admin", "user"];
DEFINE FIELD enum_ptr_slice.* ON TABLE all_field_types TYPE option<"" | "admin" | "user" | null>;
DEFINE FIELD enum_ptr_slice_ptr ON TABLE all_field_types TYPE option<array | null>;
DEFINE FIELD enum_ptr_slice_ptr.* ON TABLE all_field_types TYPE option<string | null> ASSERT $value == NULL OR $value INSIDE ["", "admin", "user"];
DEFINE FIELD enum_ptr_slice_ptr.* ON TABLE all_field_types TYPE option<"" | "admin" | "user" | null>;
DEFINE FIELD login ON TABLE all_field_types TYPE object;
DEFINE FIELD login.username ON TABLE all_field_types TYPE string;
DEFINE FIELD login.password ON TABLE all_field_types TYPE string;