From 8c29ab1da7bb4d396cababa23c430492523a6479 Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Wed, 19 Jun 2024 11:40:25 +0700 Subject: [PATCH] cmd: add --package-types argument (#118) --- cmd/hasura-ndc-go/connector.go | 2 +- cmd/hasura-ndc-go/connector_test.go | 35 +++++++++++++++++-- cmd/hasura-ndc-go/main.go | 3 ++ cmd/hasura-ndc-go/schema.go | 35 +++++++++++++++++-- .../templates/connector/connector.go.tmpl | 1 - .../subdir/expected/connector.go.tmpl | 2 +- .../source/{ => connector}/types/connector.go | 0 7 files changed, 71 insertions(+), 7 deletions(-) rename cmd/hasura-ndc-go/testdata/subdir/source/{ => connector}/types/connector.go (100%) diff --git a/cmd/hasura-ndc-go/connector.go b/cmd/hasura-ndc-go/connector.go index e062836..bb57c4b 100644 --- a/cmd/hasura-ndc-go/connector.go +++ b/cmd/hasura-ndc-go/connector.go @@ -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 diff --git a/cmd/hasura-ndc-go/connector_test.go b/cmd/hasura-ndc-go/connector_test.go index 27d82d6..f6e82c0 100644 --- a/cmd/hasura-ndc-go/connector_test.go +++ b/cmd/hasura-ndc-go/connector_test.go @@ -31,6 +31,8 @@ func TestConnectorGeneration(t *testing.T) { ConnectorDir string Directories []string ModuleName string + PackageTypes string + errorMsg string }{ { Name: "basic", @@ -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() @@ -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)) diff --git a/cmd/hasura-ndc-go/main.go b/cmd/hasura-ndc-go/main.go index 3a906cf..e59ee20 100644 --- a/cmd/hasura-ndc-go/main.go +++ b/cmd/hasura-ndc-go/main.go @@ -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."` } @@ -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...") diff --git a/cmd/hasura-ndc-go/schema.go b/cmd/hasura-ndc-go/schema.go index 0f1f158..0f366ca 100644 --- a/cmd/hasura-ndc-go/schema.go +++ b/cmd/hasura-ndc-go/schema.go @@ -9,6 +9,7 @@ import ( "go/token" "go/types" "path" + "path/filepath" "regexp" "runtime/trace" "strings" @@ -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) @@ -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 { diff --git a/cmd/hasura-ndc-go/templates/connector/connector.go.tmpl b/cmd/hasura-ndc-go/templates/connector/connector.go.tmpl index 8804a28..d034bf9 100644 --- a/cmd/hasura-ndc-go/templates/connector/connector.go.tmpl +++ b/cmd/hasura-ndc-go/templates/connector/connector.go.tmpl @@ -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" diff --git a/cmd/hasura-ndc-go/testdata/subdir/expected/connector.go.tmpl b/cmd/hasura-ndc-go/testdata/subdir/expected/connector.go.tmpl index bfa8352..70cce02 100644 --- a/cmd/hasura-ndc-go/testdata/subdir/expected/connector.go.tmpl +++ b/cmd/hasura-ndc-go/testdata/subdir/expected/connector.go.tmpl @@ -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" diff --git a/cmd/hasura-ndc-go/testdata/subdir/source/types/connector.go b/cmd/hasura-ndc-go/testdata/subdir/source/connector/types/connector.go similarity index 100% rename from cmd/hasura-ndc-go/testdata/subdir/source/types/connector.go rename to cmd/hasura-ndc-go/testdata/subdir/source/connector/types/connector.go