Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally provide SchemaIDNameFormatter for $id field #82

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ type Reflector struct {
// include a schema ID.
BaseSchemaID ID

// UseCamelCaseForIDs determines if missing IDs will use CamelCase (e.g. `TestUser`)
// or the default snake-case (e.g. `test-user`) when creating the BaseSchemaID.
UseCamelCaseForIDs bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than a specific case, It'd probably be better to use a specific method, which is consistent with some of the other text modifiers.

Something like:

SchemaIDNameFormatter func(id string) string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, is this more like what you are thinking of, @samlown ?


// Anonymous when true will hide the auto-generated Schema ID and provide what is
// known as an "anonymous schema". As a rule, this is not recommended.
Anonymous bool
Expand Down Expand Up @@ -260,7 +264,11 @@ func (r *Reflector) ReflectFromType(t reflect.Type) *Schema {
}
}
if baseSchemaID != EmptyID {
s.ID = baseSchemaID.Add(ToSnakeCase(name))
if r.UseCamelCaseForIDs {
s.ID = baseSchemaID.Add(name)
} else {
s.ID = baseSchemaID.Add(ToSnakeCase(name))
}
}
}

Expand Down