Skip to content

Commit

Permalink
cmd: add --package-types argument (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac authored Jun 19, 2024
1 parent 573cbb7 commit 8c29ab1
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/hasura-ndc-go/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func parseAndGenerateConnector(args *GenerateArguments, moduleName string) error
}

parseCtx, parseTask := trace.NewTask(context.TODO(), "parse")
sm, err := parseRawConnectorSchemaFromGoCode(parseCtx, moduleName, ".", args.Directories)
sm, err := parseRawConnectorSchemaFromGoCode(parseCtx, moduleName, ".", args)
if err != nil {
parseTask.End()
return err
Expand Down
35 changes: 33 additions & 2 deletions cmd/hasura-ndc-go/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func TestConnectorGeneration(t *testing.T) {
ConnectorDir string
Directories []string
ModuleName string
PackageTypes string
errorMsg string
}{
{
Name: "basic",
Expand All @@ -51,6 +53,29 @@ func TestConnectorGeneration(t *testing.T) {
ModuleName: "github.com/hasura/ndc-codegen-subdir-test",
Directories: []string{"connector/functions"},
},
{
Name: "subdir_package_types",
BasePath: "./testdata/subdir",
ConnectorDir: "connector",
ModuleName: "github.com/hasura/ndc-codegen-subdir-test",
PackageTypes: "connector/types",
Directories: []string{"connector/functions"},
},
{
Name: "subdir_package_types_absolute",
BasePath: "./testdata/subdir",
ConnectorDir: "connector",
ModuleName: "github.com/hasura/ndc-codegen-subdir-test",
PackageTypes: "github.com/hasura/ndc-codegen-subdir-test/connector/types",
Directories: []string{"connector/functions"},
},
{
Name: "invalid_types_package",
BasePath: "./testdata/subdir",
ModuleName: "github.com/hasura/ndc-codegen-subdir-test",
Directories: []string{"connector/functions"},
errorMsg: "the `types` package where the State struct is in must be placed in root or connector directory",
},
}

rootDir, err := os.Getwd()
Expand All @@ -67,12 +92,18 @@ func TestConnectorGeneration(t *testing.T) {
srcDir := path.Join(tc.BasePath, "source")
assert.NoError(t, os.Chdir(srcDir))

assert.NoError(t, parseAndGenerateConnector(&GenerateArguments{
err = parseAndGenerateConnector(&GenerateArguments{
Path: srcDir,
ConnectorDir: tc.ConnectorDir,
PackageTypes: tc.PackageTypes,
Directories: tc.Directories,
}, tc.ModuleName))
}, tc.ModuleName)
if tc.errorMsg != "" {
assert.ErrorContains(t, err, tc.errorMsg)
return
}

assert.NoError(t, err)
var expectedSchema schema.SchemaResponse
assert.NoError(t, json.Unmarshal(expectedSchemaBytes, &expectedSchema))

Expand Down
3 changes: 3 additions & 0 deletions cmd/hasura-ndc-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type GenerateArguments struct {
Path string `help:"The path of the root directory where the go.mod file is present" short:"p" default:"."`
ConnectorDir string `help:"The directory where the connector.go file is placed" default:"."`
PackageTypes string `help:"The name of types package where the State struct is in"`
Directories []string `help:"Folders contain NDC operation functions" short:"d" default:"functions,types"`
Trace string `help:"Enable tracing and write to target file path."`
}
Expand Down Expand Up @@ -63,6 +64,8 @@ func main() {
case "generate":
log.Info().
Str("path", cli.Generate.Path).
Str("connector_dir", cli.Generate.ConnectorDir).
Str("package_types", cli.Generate.PackageTypes).
Interface("directories", cli.Generate.Directories).
Msg("generating connector schema...")

Expand Down
35 changes: 33 additions & 2 deletions cmd/hasura-ndc-go/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go/token"
"go/types"
"path"
"path/filepath"
"regexp"
"runtime/trace"
"strings"
Expand Down Expand Up @@ -337,10 +338,17 @@ type SchemaParser struct {
pkg *packages.Package
}

func parseRawConnectorSchemaFromGoCode(ctx context.Context, moduleName string, filePath string, folders []string) (*RawConnectorSchema, error) {
func parseRawConnectorSchemaFromGoCode(ctx context.Context, moduleName string, filePath string, args *GenerateArguments) (*RawConnectorSchema, error) {
rawSchema := NewRawConnectorSchema()

pkgTypes, err := evalPackageTypesLocation(args.PackageTypes, moduleName, filePath, args.ConnectorDir)
if err != nil {
return nil, err
}
rawSchema.Imports[pkgTypes] = true

fset := token.NewFileSet()
for _, folder := range folders {
for _, folder := range args.Directories {
_, parseCodeTask := trace.NewTask(ctx, fmt.Sprintf("parse_%s_code", folder))
folderPath := path.Join(filePath, folder)

Expand Down Expand Up @@ -374,6 +382,29 @@ func parseRawConnectorSchemaFromGoCode(ctx context.Context, moduleName string, f
return rawSchema, nil
}

func evalPackageTypesLocation(name string, moduleName string, filePath string, connectorDir string) (string, error) {
if name != "" {
// assume that the absolute package name should have domain, e.g. github.com/...
if strings.Contains(name, ".") {
return name, nil
}
return fmt.Sprintf("%s/%s", moduleName, name), nil
}

matches, err := filepath.Glob(path.Join(filePath, "types", "*.go"))
if err == nil && len(matches) > 0 {
return fmt.Sprintf("%s/types", moduleName), nil
}

if connectorDir != "" && !strings.HasPrefix(".", connectorDir) {
matches, err = filepath.Glob(path.Join(filePath, connectorDir, "types", "*.go"))
if err == nil && len(matches) > 0 {
return fmt.Sprintf("%s/%s/types", moduleName, connectorDir), nil
}
}
return "", fmt.Errorf("the `types` package where the State struct is in must be placed in root or connector directory, %s", err)
}

// parse raw connector schema from Go code
func (sp *SchemaParser) parseRawConnectorSchema(rawSchema *RawConnectorSchema, pkg *types.Package) error {

Expand Down
1 change: 0 additions & 1 deletion cmd/hasura-ndc-go/templates/connector/connector.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"

{{.Imports}}
"{{.Module}}/types"
"github.com/hasura/ndc-sdk-go/connector"
"github.com/hasura/ndc-sdk-go/schema"
"github.com/hasura/ndc-sdk-go/utils"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"

"github.com/hasura/ndc-codegen-subdir-test/connector/functions"
"github.com/hasura/ndc-codegen-subdir-test/types"
"github.com/hasura/ndc-codegen-subdir-test/connector/types"
"github.com/hasura/ndc-sdk-go/connector"
"github.com/hasura/ndc-sdk-go/schema"
"github.com/hasura/ndc-sdk-go/utils"
Expand Down

0 comments on commit 8c29ab1

Please sign in to comment.