Skip to content

Commit

Permalink
feat: support operation naming style snake_case (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac authored Sep 15, 2024
1 parent 1a1a126 commit d654cde
Show file tree
Hide file tree
Showing 20 changed files with 5,112 additions and 39 deletions.
6 changes: 4 additions & 2 deletions cmd/hasura-ndc-go/command/internal/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

"github.com/hasura/ndc-sdk-go/schema"
"github.com/iancoleman/strcase"
"github.com/rs/zerolog/log"
)

Expand All @@ -24,6 +25,7 @@ type ConnectorGenerationArguments struct {
PackageTypes string `help:"The name of types package where the State struct is in"`
Directories []string `help:"Folders contain NDC operation functions" short:"d"`
Trace string `help:"Enable tracing and write to target file path."`
Style string `help:"The naming style for functions and procedures. Accept: camel-case, snake-case" enum:"camel-case,snake-case" default:"camel-case"`
}

type connectorTypeBuilder struct {
Expand Down Expand Up @@ -500,10 +502,10 @@ func (j %s) ScalarName() string {
sb.imports["slices"] = ""

sb.builder.WriteString("const (\n")
pascalName := ToPascalCase(scalar.Name)
pascalName := strcase.ToCamel(scalar.Name)
enumConstants := make([]string, len(scalarRep.OneOf))
for i, enum := range scalarRep.OneOf {
enumConst := fmt.Sprintf("%s%s", pascalName, ToPascalCase(enum))
enumConst := fmt.Sprintf("%s%s", pascalName, strcase.ToCamel(enum))
enumConstants[i] = enumConst
sb.builder.WriteString(fmt.Sprintf(" %s %s = \"%s\"\n", enumConst, scalar.Name, enum))
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/hasura-ndc-go/command/internal/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestConnectorGeneration(t *testing.T) {
Directories []string
ModuleName string
PackageTypes string
NamingStyle string
errorMsg string
}{
{
Expand Down Expand Up @@ -75,6 +76,13 @@ func TestConnectorGeneration(t *testing.T) {
Directories: []string{"connector/functions"},
errorMsg: "the `types` package where the State struct is in must be placed in root or connector directory",
},
{
Name: "snake_case",
BasePath: "./testdata/snake_case",
ModuleName: "github.com/hasura/ndc-codegen-test-snake-case",
Directories: []string{"functions"},
NamingStyle: string(StyleSnakeCase),
},
}

rootDir, err := os.Getwd()
Expand All @@ -94,6 +102,7 @@ func TestConnectorGeneration(t *testing.T) {
ConnectorDir: tc.ConnectorDir,
PackageTypes: tc.PackageTypes,
Directories: tc.Directories,
Style: tc.NamingStyle,
}, tc.ModuleName)
if tc.errorMsg != "" {
assert.ErrorContains(t, err, tc.errorMsg)
Expand Down
6 changes: 6 additions & 0 deletions cmd/hasura-ndc-go/command/internal/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"text/template"

"github.com/hasura/ndc-sdk-go/schema"
"github.com/iancoleman/strcase"
)

const (
Expand All @@ -25,6 +26,11 @@ func init() {
if err != nil {
panic(fmt.Errorf("failed to parse connector template: %s", err))
}

strcase.ConfigureAcronym("API", "Api")
strcase.ConfigureAcronym("REST", "Rest")
strcase.ConfigureAcronym("HTTP", "Http")
strcase.ConfigureAcronym("SQL", "sql")
}

type ScalarName string
Expand Down
25 changes: 23 additions & 2 deletions cmd/hasura-ndc-go/command/internal/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/fatih/structtag"
"github.com/hasura/ndc-sdk-go/schema"
"github.com/iancoleman/strcase"
"github.com/rs/zerolog/log"
"golang.org/x/tools/go/packages"
)
Expand Down Expand Up @@ -213,6 +214,7 @@ type SchemaParser struct {
rawSchema *RawConnectorSchema
packages []*packages.Package
packageIndex int
namingStyle OperationNamingStyle
}

// GetCurrentPackage gets the current evaluating package
Expand All @@ -231,6 +233,14 @@ func (sp SchemaParser) FindPackageByPath(input string) *packages.Package {
}

func parseRawConnectorSchemaFromGoCode(ctx context.Context, moduleName string, filePath string, args *ConnectorGenerationArguments) (*RawConnectorSchema, error) {
var err error
namingStyle := StyleCamelCase
if args.Style != "" {
namingStyle, err = ParseOperationNamingStyle(args.Style)
if err != nil {
return nil, err
}
}
rawSchema := NewRawConnectorSchema()

pkgTypes, err := evalPackageTypesLocation(args.PackageTypes, moduleName, filePath, args.ConnectorDir)
Expand Down Expand Up @@ -312,6 +322,7 @@ func parseRawConnectorSchemaFromGoCode(ctx context.Context, moduleName string, f
packages: packageList,
packageIndex: i,
rawSchema: rawSchema,
namingStyle: namingStyle,
}

err = sp.parseRawConnectorSchema(packageList[i].Types)
Expand Down Expand Up @@ -846,6 +857,16 @@ func (sp *SchemaParser) parseTypeInfoFromComments(typeName string, packagePath s
return typeInfo, nil
}

// format operation name with style
func (sp SchemaParser) formatOperationName(name string) string {
switch sp.namingStyle {
case StyleSnakeCase:
return strcase.ToSnake(name)
default:
return strcase.ToLowerCamel(name)
}
}

func (sp *SchemaParser) parseOperationInfo(fn *types.Func) *OperationInfo {
functionName := fn.Name()
result := OperationInfo{
Expand Down Expand Up @@ -878,7 +899,7 @@ func (sp *SchemaParser) parseOperationInfo(fn *types.Func) *OperationInfo {
if matchesLen > 3 && strings.TrimSpace(matches[3]) != "" {
result.Name = strings.TrimSpace(matches[3])
} else {
result.Name = ToCamelCase(functionName)
result.Name = sp.formatOperationName(functionName)
}
} else {
descriptions = append(descriptions, text)
Expand All @@ -895,7 +916,7 @@ func (sp *SchemaParser) parseOperationInfo(fn *types.Func) *OperationInfo {
return nil
}
result.Kind = OperationKind(operationNameResults[1])
result.Name = ToCamelCase(operationNameResults[2])
result.Name = sp.formatOperationName(operationNameResults[2])
}

desc := strings.TrimSpace(strings.Join(descriptions, " "))
Expand Down
33 changes: 0 additions & 33 deletions cmd/hasura-ndc-go/command/internal/stringer.go

This file was deleted.

Loading

0 comments on commit d654cde

Please sign in to comment.