-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix/array-parameter-handling-in-generator-commands (#22)
* Removes direct file access from the NewTemplateModelBuilder method; requires an AstFileAccessor function instead. Adds AstFromFile and AstFromSource accessor functions. * Fixes handling of array types * Updates the init command to add a reference to the latest version of Parsley
- Loading branch information
1 parent
8533007
commit ca31b47
Showing
9 changed files
with
158 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package generator | ||
|
||
import ( | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
) | ||
|
||
type AstFileAccessor func() (*ast.File, error) | ||
|
||
// AstFromFile Creates an AstFileAccessor object for the given Golang source file. | ||
func AstFromFile(sourceFilePath string) AstFileAccessor { | ||
return func() (*ast.File, error) { | ||
fileSet := token.NewFileSet() | ||
return parser.ParseFile(fileSet, sourceFilePath, nil, parser.ParseComments) | ||
} | ||
} | ||
|
||
// AstFromSource Creates an AstFileAccessor object for the given source code. | ||
func AstFromSource(code []byte) AstFileAccessor { | ||
return func() (*ast.File, error) { | ||
fileSet := token.NewFileSet() | ||
return parser.ParseFile(fileSet, "", code, parser.ParseComments) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package generator | ||
|
||
import ( | ||
"github.com/matzefriedrich/parsley/internal/generator" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_AstFileAccessor_create_accessor_from_source(t *testing.T) { | ||
|
||
// Arrange | ||
source := []byte("package types\n" + | ||
"\n" + | ||
"type Service0 interface {\n" + | ||
" Method0()\n" + | ||
"}\n" + | ||
"\n" + | ||
"type Service1 interface {\n" + | ||
" Method1() string\n" + | ||
" Method3() (string, error)\n" + | ||
"}\n") | ||
|
||
accessor := generator.AstFromSource(source) | ||
|
||
// Act | ||
actual, err := accessor() | ||
|
||
// Assert | ||
assert.NoError(t, err) | ||
assert.NotNil(t, actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package generator | ||
|
||
import ( | ||
"github.com/matzefriedrich/parsley/internal/generator" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_NewTemplateModelBuilder_from_empty_source_file_returns_error(t *testing.T) { | ||
|
||
// Arrange | ||
source := []byte("") | ||
|
||
// Act | ||
sut, err := generator.NewTemplateModelBuilder(generator.AstFromSource(source)) | ||
|
||
// Assert | ||
assert.Error(t, err) | ||
assert.Nil(t, sut) | ||
} | ||
|
||
func Test_NewTemplateModelBuilder_from_minimal_source_file(t *testing.T) { | ||
|
||
// Arrange | ||
source := []byte("package main") | ||
|
||
// Act | ||
sut, err := generator.NewTemplateModelBuilder(generator.AstFromSource(source)) | ||
|
||
// Assert | ||
assert.NoError(t, err) | ||
assert.NotNil(t, sut) | ||
} | ||
|
||
func Test_NewTemplateModelBuilder_Build_multiple_interface_definitions(t *testing.T) { | ||
|
||
// Arrange | ||
source := []byte("package types\n" + "\n" + | ||
"type Service0 interface {\n" + " Method0(s string)\n" + "}\n" + "\n" + | ||
"type Service1 interface {\n" + " Method1() string\n" + " Method2(data []bytes) (string, error)\n" + | ||
"}\n") | ||
|
||
accessor := generator.AstFromSource(source) | ||
sut, _ := generator.NewTemplateModelBuilder(accessor) | ||
|
||
// Act | ||
actual, err := sut.Build() | ||
|
||
// Assert | ||
assert.NoError(t, err) | ||
assert.NotNil(t, actual) | ||
|
||
serviceInterface0 := actual.Interfaces[0] | ||
assert.Equal(t, "Service0", serviceInterface0.Name) | ||
|
||
serviceInterface1 := actual.Interfaces[1] | ||
assert.Equal(t, "Service1", serviceInterface1.Name) | ||
|
||
method1 := serviceInterface1.Methods[0] | ||
assert.Equal(t, "Method1", method1.Name) | ||
|
||
method2 := serviceInterface1.Methods[1] | ||
assert.Equal(t, "Method2", method2.Name) | ||
} |