Skip to content

Commit

Permalink
Accept io.FS in RIDL parser (#178)
Browse files Browse the repository at this point in the history
* Remove unused schema from templateFuncMap()

* Accept io.FS in RIDL parser

Fixes #173

* Simplify reading JSON schema file

* Remove invalid wrapping of nil error

* Remove ridl.mockImport

* Fix building all tests

* Don't regenerate golden file on each test run

* Improve RIDL parser tests

* TestRIDLImports(): Use fstest.MapFS, test import ../parent.ridl

* TestRIDLImportsExampleDir(): Use os.DirFS() to test _example/ dir

* TestRIDLImports(): Test imported struct types
  • Loading branch information
VojtechVitek committed Dec 29, 2022
1 parent 92e3dbe commit 4fed3b0
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 245 deletions.
2 changes: 0 additions & 2 deletions cmd/webrpc-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ func main() {
os.Exit(1)
}

// spew.Dump(schema)

// Test the schema file (useful for ridl files)
if *testFlag {
out, err := schema.ToJSON(true)
Expand Down
3 changes: 1 addition & 2 deletions gen/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"strings"

"github.com/golang-cz/textcase"
"github.com/webrpc/webrpc/schema"
)

// Template functions are part of webrpc-gen API. Keep backward-compatible.
func templateFuncMap(proto *schema.WebRPCSchema, opts map[string]interface{}) map[string]interface{} {
func templateFuncMap(opts map[string]interface{}) map[string]interface{} {
return map[string]interface{}{
// Template flow.
"stderrPrintf": stderrPrintf, // v0.7.0
Expand Down
8 changes: 3 additions & 5 deletions gen/template_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

func loadTemplates(proto *schema.WebRPCSchema, target string, config *Config) (*template.Template, *TemplateSource, error) {
s, err := NewTemplateSource(proto, target, config)
s, err := NewTemplateSource(target, config)
if err != nil {
return nil, nil, err
}
Expand All @@ -41,7 +41,6 @@ const (

type TemplateSource struct {
tmpl *template.Template
proto *schema.WebRPCSchema
target string
config *Config

Expand All @@ -52,11 +51,10 @@ type TemplateSource struct {
CacheRefreshErr error
}

func NewTemplateSource(proto *schema.WebRPCSchema, target string, config *Config) (*TemplateSource, error) {
tmpl := template.New(target).Funcs(templateFuncMap(proto, config.TemplateOptions))
func NewTemplateSource(target string, config *Config) (*TemplateSource, error) {
tmpl := template.New(target).Funcs(templateFuncMap(config.TemplateOptions))
return &TemplateSource{
tmpl: tmpl,
proto: proto,
target: target,
config: config,
}, nil
Expand Down
5 changes: 2 additions & 3 deletions schema/ridl/lexer_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ridl

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -533,7 +532,7 @@ func TestLexerSimpleTokens(t *testing.T) {
}

for _, input := range inputs {
tokens, err := tokenize(strings.NewReader(input.in))
tokens, err := tokenize([]byte(input.in))
assert.NoError(t, err)

assert.Equal(t, len(input.out), len(tokens))
Expand Down Expand Up @@ -616,7 +615,7 @@ func TestLexerRIDLTokens(t *testing.T) {
}

for _, input := range inputs {
tokens, err := tokenize(strings.NewReader(input.in))
tokens, err := tokenize([]byte(input.in))
assert.NoError(t, err)

for i, tok := range tokens {
Expand Down
11 changes: 0 additions & 11 deletions schema/ridl/mock.go

This file was deleted.

5 changes: 2 additions & 3 deletions schema/ridl/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ridl
import (
"errors"
"fmt"
"io"
"log"
"reflect"
"runtime"
Expand Down Expand Up @@ -43,8 +42,8 @@ type parser struct {
root RootNode
}

func newParser(r io.Reader) (*parser, error) {
tokens, err := tokenize(r)
func newParser(src []byte) (*parser, error) {
tokens, err := tokenize(src)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 4fed3b0

Please sign in to comment.