Skip to content

Commit

Permalink
ridl rpc succinct form (#229)
Browse files Browse the repository at this point in the history
* alias type and idl rpc succinct method format

* passing tests

* ridl succinct form

* revert back to string from VarName

* revert back to string from VarName
  • Loading branch information
darkhorse0311 committed Aug 7, 2023
1 parent 00bc507 commit caeda55
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 46 deletions.
122 changes: 122 additions & 0 deletions schema/ridl/_example/example1-golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
}
]
},
{
"name": "amount",
"type": "uint64"
},
{
"name": "role",
"type": "string",
Expand Down Expand Up @@ -133,6 +137,55 @@
"type": "string"
}
]
},
{
"kind": "struct",
"name": "FlattenRequest",
"fields": [
{
"name": "name",
"type": "string",
"meta": [
{
"go.tag.db": "name"
}
]
},
{
"name": "amount",
"type": "uint64",
"optional": true,
"meta": [
{
"go.tag.db": "amount"
}
]
}
]
},
{
"kind": "struct",
"name": "FlattenResponse",
"fields": [
{
"name": "id",
"type": "uint64",
"meta": [
{
"go.field.name": "ID"
}
]
},
{
"name": "count",
"type": "uint64",
"meta": [
{
"json": "counter"
}
]
}
]
}
],
"errors": [],
Expand Down Expand Up @@ -299,6 +352,75 @@
]
}
]
},
{
"name": "Another",
"methods": [
{
"name": "Flatten",
"inputs": [
{
"name": "name",
"type": "string",
"optional": false,
"meta": [
{
"go.tag.db": "name"
}
]
},
{
"name": "amount",
"type": "uint64",
"optional": true,
"meta": [
{
"go.tag.db": "amount"
}
]
}
],
"outputs": [
{
"name": "id",
"type": "uint64",
"optional": false,
"meta": [
{
"go.field.name": "ID"
}
]
},
{
"name": "count",
"type": "uint64",
"optional": false,
"meta": [
{
"json": "counter"
}
]
}
]
},
{
"name": "GetAccountBalance",
"inputs": [
{
"name": "name",
"type": "string",
"optional": false
}
],
"outputs": [
{
"name": "balance",
"type": "uint64",
"optional": false
}
]
}
]
}
]
}
20 changes: 20 additions & 0 deletions schema/ridl/_example/example1.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import example1-partials.ridl
- Empty
- GetUserRequest


struct Role
- name: string

Expand All @@ -30,6 +31,8 @@ struct User
+ json = USERNAME
+ go.tag.db = username

- amount: uint64

- role: string
+ go.tag.db = -

Expand Down Expand Up @@ -62,3 +65,20 @@ service ExampleService
- VerifyUsers(seq: int32, header?: map<string,[]string>, ids: []uint64) => (code: bool, ids: []bool)

- MoreTest(n: uint64, stuff: []map<uint64,string>, etc: string) => (code?: bool)


struct FlattenRequest
- name: string
+ go.tag.db = name
- amount?: uint64
+ go.tag.db = amount

struct FlattenResponse
- id: uint64
+ go.field.name = ID
- count: uint64
+ json = counter

service Another
- Flatten(FlattenRequest) => (FlattenResponse)
- GetAccountBalance(name: string) => (balance: uint64)
7 changes: 3 additions & 4 deletions schema/ridl/parser_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const (
TokenNodeType
DefinitionNodeType
ImportNodeType
EnumNodeType
StructNodeType
ErrorNodeType
EnumNodeType
ArgumentNodeType
MethodNodeType
ServiceNodeType
Expand Down Expand Up @@ -285,10 +285,9 @@ type ArgumentNode struct {

name *TokenNode
argumentType *TokenNode
optional bool

optional bool

stream bool //TODO: should be deprecated
inlineStruct *TokenNode
}

func (an *ArgumentNode) Name() *TokenNode {
Expand Down
24 changes: 24 additions & 0 deletions schema/ridl/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,30 @@ func TestParserService(t *testing.T) {
}
}

func TestParserServiceSuccint(t *testing.T) {
p, err := newStringParser(`
struct FlattenRequest
- name: string
+ go.tag.db = name
- amount: uint64
+ go.tag.db = amount
struct FlattenResponse
- id: uint64
+ go.field.name = ID
- count: uint64
+ json = counter
service Demo
- DemoService(in: input) => (out: output)
- Flatten(FlattenRequest) => (FlattenResponse)
`)
assert.NoError(t, err)

err = p.run()
assert.NoError(t, err)
}

func TestParserExamples(t *testing.T) {
{
p, err := newStringParser(`
Expand Down
25 changes: 24 additions & 1 deletion schema/ridl/ridl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

var (
schemaTypeKindAlias = "alias"
schemaTypeKindEnum = "enum"
schemaTypeKindStruct = "struct"
)
Expand Down Expand Up @@ -169,6 +168,7 @@ func (p *Parser) parse() (*schema.WebRPCSchema, error) {
for _, line := range q.root.Enums() {
name := line.Name().String()
enumDef := s.GetTypeByName(string(name))

if enumDef == nil {
return nil, fmt.Errorf("unexpected error, could not find definition for: %v", name)
}
Expand Down Expand Up @@ -305,6 +305,29 @@ func isImportAllowed(name string, whitelist []string) bool {
func buildArgumentsList(s *schema.WebRPCSchema, args []*ArgumentNode) ([]*schema.MethodArgument, error) {
output := []*schema.MethodArgument{}

// succint form
if len(args) == 1 && args[0].inlineStruct != nil {
node := args[0].inlineStruct
structName := node.tok.val

typ := s.GetTypeByName(structName)
if typ.Kind != "struct" {
return nil, fmt.Errorf("expecting struct type for inline definition of '%s'", structName)
}

for _, arg := range typ.Fields {
methodArgument := &schema.MethodArgument{
Name: arg.Name,
Type: arg.Type,
Optional: arg.Optional,
TypeExtra: arg.TypeExtra,
}
output = append(output, methodArgument)
}

return output, nil
}

// normal form
for _, arg := range args {

Expand Down
2 changes: 1 addition & 1 deletion schema/ridl/ridl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func TestRIDLImportsExampleDir(t *testing.T) {

current := []byte(jout)

golden, err := ioutil.ReadFile("./_example/example1-golden.json")
golden, err := os.ReadFile("./_example/example1-golden.json")
assert.NoError(t, err)

if *updateFlag == "./_example/example1-golden.json" {
Expand Down
69 changes: 43 additions & 26 deletions schema/ridl/type_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,37 +82,54 @@ loop:
p.next()

case tokenWord:
var argument []*token
var name *token

optional := false

matches, err := p.match(tokenWord, tokenQuestionMark, tokenColon, tokenWhitespace)
if err == nil {
argument = []*token{matches[0], matches[1], matches[2]}
name = matches[0]
optional = true
} else {
matches, err = p.match(tokenWord, tokenColon, tokenWhitespace)
if err != nil {
return nil, err
// succinct form, Method(InRequest) => (OutReponse)
{
matches, err := p.match(tokenWord, tokenCloseParen)
if err == nil {
values = append(values, &ArgumentNode{
inlineStruct: newTokenNode(matches[0]),
})

tokens = append(tokens, tok)
p.next()
break loop
}
argument = []*token{matches[0], matches[1]}
name = matches[0]
}

varType, err := p.expectType()
if err != nil {
return nil, err
}
// normal form, Method(arg1: type, arg2?: type) => (out: type)
{
var argument []*token
var name *token

optional := false

matches, err := p.match(tokenWord, tokenQuestionMark, tokenColon, tokenWhitespace)
if err == nil {
argument = []*token{matches[0], matches[1], matches[2]}
name = matches[0]
optional = true
} else {
matches, err = p.match(tokenWord, tokenColon, tokenWhitespace)
if err != nil {
return nil, err
}
argument = []*token{matches[0], matches[1]}
name = matches[0]
}

values = append(values, &ArgumentNode{
name: newTokenNode(name),
argumentType: newTokenNode(varType),
optional: optional,
})
varType, err := p.expectType()
if err != nil {
return nil, err
}

tokens = append(tokens, append(argument, varType)...)
values = append(values, &ArgumentNode{
name: newTokenNode(name),
argumentType: newTokenNode(varType),
optional: optional,
})

tokens = append(tokens, append(argument, varType)...)
}

default:
return nil, errUnexpectedToken
Expand Down
2 changes: 2 additions & 0 deletions schema/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type MethodArgument struct {

InputArg bool `json:"-"` // denormalize/back-reference
OutputArg bool `json:"-"` // denormalize/back-reference

TypeExtra `json:",omitempty"`
}

func (s *Service) Parse(schema *WebRPCSchema) error {
Expand Down
1 change: 0 additions & 1 deletion schema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

const (
// TODO: type Kind (struct | enum | coreType | ...)
TypeKind_Struct = "struct"
TypeKind_Enum = "enum"
)
Expand Down
Loading

0 comments on commit caeda55

Please sign in to comment.