Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jamietanna committed Jun 16, 2024
1 parent 44ecf65 commit c8bc0bb
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# yaml-language-server: $schema=../../../../configuration-schema.json
package: allowlowercasefields
generate:
models: true
output: types.gen.go
output-options:
skip-prune: true
allow-unexported-fields: true
# TODO: SchemaNameToTypeName
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package allowlowercasefields

//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml spec.yaml
11 changes: 11 additions & 0 deletions internal/test/outputoptions/allow_lowercase_fields/spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
components:
schemas:
my_item:
type: object
properties:
name:
type: string
age:
type: integer
# NOTE that this is an unexported field
x-go-name: age
10 changes: 10 additions & 0 deletions internal/test/outputoptions/allow_lowercase_fields/types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions internal/test/outputoptions/allow_lowercase_fields/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package allowlowercasefields

import "testing"

func TestMyItemCompiles(t *testing.T) {
_ = MyItem{
Name: "a string",
age: 1_000,
}
}
2 changes: 2 additions & 0 deletions pkg/codegen/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ type OutputOptions struct {

// NameNormalizer is the method used to normalize Go names and types, for instance converting the text `MyApi` to `MyAPI`. Corresponds with the constants defined for `codegen.NameNormalizerFunction`
NameNormalizer string `yaml:"name-normalizer,omitempty"`

AllowUnexportedFields bool `yaml:"allow-unexported-fields"`
}

// UpdateDefaults sets reasonable default values for unset fields in Configuration
Expand Down
3 changes: 3 additions & 0 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ func (p Property) GoFieldName() string {
}
}

// return goFieldName

return SchemaNameToTypeName(goFieldName)
}

Expand Down Expand Up @@ -664,6 +666,7 @@ func GenFieldsFromProperties(props []Property) []string {

goFieldName := p.GoFieldName()

fmt.Printf("goFieldName: %v\n", goFieldName)
// Add a comment to a field in case we have one, otherwise skip.
if p.Description != "" {
// Separate the comment from a previous-defined, unrelated field.
Expand Down
4 changes: 4 additions & 0 deletions pkg/codegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ func ToCamelCase(str string) string {

n := ""
capNext := true
// if globalState.options.OutputOptions.AllowUnexportedFields {
// capNext = false
// }

for _, v := range s {
if unicode.IsUpper(v) {
n += string(v)
Expand Down

0 comments on commit c8bc0bb

Please sign in to comment.