Skip to content

Commit

Permalink
Support enum strings (#247)
Browse files Browse the repository at this point in the history
* allow string enum

* update go example with enum string support

* do not return error when type is not enum

* remove error out on structType func
  • Loading branch information
LukasJenicek committed Feb 28, 2024
1 parent c02074c commit cbba50d
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 22 deletions.
57 changes: 52 additions & 5 deletions _examples/golang-basics/example.gen.go

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

10 changes: 10 additions & 0 deletions _examples/golang-basics/example.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ enum Kind: uint32
- USER
- ADMIN

enum Intent: string
- openSession
- closeSession
- validateSession

struct Empty

Expand All @@ -25,6 +29,12 @@ struct User
- role: string
+ go.tag.db = -

- kind: Kind
+ json = kind

- intent: Intent
+ json = intent

struct SearchFilter
- q: string

Expand Down
5 changes: 4 additions & 1 deletion _examples/golang-basics/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ func TestGetUser(t *testing.T) {
{
arg1 := map[string]string{"a": "1"}
code, user, err := client.GetUser(context.Background(), arg1, 12)
intent := Intent_openSession
kind := Kind_ADMIN

assert.Equal(t, uint32(200), code)
assert.Equal(t, &User{ID: 12, Username: "hihi"}, user)
assert.Equal(t, &User{ID: 12, Username: "hihi", Intent: &intent, Kind: &kind}, user)
assert.NoError(t, err)
}

Expand Down
5 changes: 5 additions & 0 deletions _examples/golang-basics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ func (s *ExampleServiceRPC) GetUser(ctx context.Context, header map[string]strin
return 0, nil, ErrorWithCause(ErrUserNotFound, fmt.Errorf("unknown user id %d", userID))
}

kind := Kind_ADMIN
intent := Intent_openSession

return 200, &User{
ID: userID,
Username: "hihi",
Kind: &kind,
Intent: &intent,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions _examples/hello-webrpc-ts/webapp/src/client.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,4 @@ const webrpcErrorByCode: { [code: number]: any } = {
}

export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>

1 change: 1 addition & 0 deletions _examples/node-ts/webapp/client.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,4 @@ const webrpcErrorByCode: { [code: number]: any } = {
}

export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>

28 changes: 14 additions & 14 deletions gen/funcmap_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,40 @@ func isCoreType(v interface{}) bool {
}

// Returns true if given type is struct.
func isStructType(v interface{}) (bool, error) {
func isStructType(v interface{}) bool {
switch t := v.(type) {
case schema.Type:
return t.Kind == "struct", nil
return t.Kind == "struct"
case *schema.Type:
return t.Kind == "struct", nil
return t.Kind == "struct"
case schema.VarType:
return t.Type == schema.T_Struct, nil
return t.Type == schema.T_Struct
case *schema.VarType:
if t != nil {
return t.Type == schema.T_Struct, nil
return t.Type == schema.T_Struct
}
return false, nil
return false
default:
return false, fmt.Errorf("isStructType(): unexpected type %T: %+v", v, v)
return false
}
}

// Returns true if given type is enum.
func isEnumType(v interface{}) (bool, error) {
func isEnumType(v interface{}) bool {
switch t := v.(type) {
case schema.Type:
return t.Kind == "enum", nil
return t.Kind == "enum"
case *schema.Type:
return t.Kind == "enum", nil
return t.Kind == "enum"
case schema.VarType:
return t.Struct.Type.Kind == "enum", nil
return t.Struct.Type.Kind == "enum"
case *schema.VarType:
if t != nil {
return t.Struct.Type.Kind == "enum", nil
return t.Struct.Type.Kind == "enum"
}
return false, nil
return false
default:
return false, fmt.Errorf("isEnumType(): unexpected type %T: %+v", v, v)
return false
}
}

Expand Down
5 changes: 3 additions & 2 deletions schema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ func (t *Type) Parse(schema *WebRPCSchema) error {

// ensure enum type is one of the allowed types.. aka integer
fieldType := t.Type
if !isValidVarType(fieldType.String(), VarIntegerCoreTypes) {
return fmt.Errorf("schema error: enum '%s' field '%s' is invalid. must be an integer type.", t.Name, fieldType.String())
validCoreTypes := append(VarIntegerCoreTypes, T_String)
if !isValidVarType(fieldType.String(), validCoreTypes) {
return fmt.Errorf("schema error: enum '%s' field '%s' is invalid. must be an integer type", t.Name, fieldType.String())
}
}

Expand Down

0 comments on commit cbba50d

Please sign in to comment.