Documentation
¶
Overview ¶
Package naming provides shared naming utilities for oastools packages: case conversion, and the charset rule OAS 3.x imposes on component names.
This internal package contains common string transformation functions used by multiple oastools packages including builder and joiner. Functions include ToPascalCase, ToCamelCase, ToSnakeCase, and ToKebabCase.
These functions are used for:
- Builder package: Schema and operation naming from titles
- Joiner package: Template functions for operation-aware schema renaming
- Fixer package: Operation ID naming
Component Names ¶
ComponentNamePattern and IsValidComponentName state, once, the charset OAS 3.x requires of every key of the Components Object. The validator asks whether a document's names are legal and the fixer asks which characters it may keep when building a replacement; both consume the same definition so neither can drift from the spec or from the other. SanitizeComponentName (with FoldToASCII) reduces an illegal name to a legal one, and the fixer and the converter's 2.0 to 3.x rename share it.
As an internal package, these functions are not part of the public API and may change without notice.
Package naming provides shared string case conversion utilities.
Index ¶
- Constants
- func FoldToASCII(name string) string
- func IsComponentNameChar(r rune) bool
- func IsValidComponentName(name string) bool
- func SanitizeComponentName(name string) string
- func ToCamelCase(s string) string
- func ToKebabCase(s string) string
- func ToPascalCase(s string) string
- func ToSnakeCase(s string) string
Examples ¶
Constants ¶
const ComponentNamePattern = `^[a-zA-Z0-9._-]+$`
ComponentNamePattern is the charset OAS 3.x requires of every fixed field of the Components Object: "All the fixed fields declared above are objects that MUST use keys that match the regular expression: ^[a-zA-Z0-9\.\-_]+$".
See: https://spec.openapis.org/oas/v3.0.0.html#components-object
The spec writes "." and "-" escaped inside the character class, where neither needs escaping; this is the same expression written plainly. It is a string rather than a compiled regexp.Regexp because nothing needs to run it — IsValidComponentName decides membership directly, and this exists so an error message can quote the rule it enforces.
OAS 2.0 has no counterpart, deliberately. It places no charset constraint on the keys of the root-level parameters, definitions, and responses objects, so a name containing "/" or "~" is legitimate there and is escaped per RFC 6901 when referenced. Applying this pattern to an OAS 2.0 document would reject valid specs.
Variables ¶
This section is empty.
Functions ¶
func FoldToASCII ¶ added in v1.70.1
FoldToASCII rewrites accented letters as their base letter, so "Pét" becomes "Pet" rather than "P_t".
It decomposes each rune into a base plus its combining marks, drops the marks, and recomposes. That covers the Latin-alphabet names code generators actually produce; a script with no ASCII base form, such as "宠物", is returned unchanged and the caller's sanitizer replaces it.
The transformer is built per call because it carries state and callers may run concurrently. The ASCII fast path keeps that off the common route.
func IsComponentNameChar ¶ added in v1.58.0
IsComponentNameChar reports whether r may appear in an OAS 3.x component name.
Spelled out rather than deferring to unicode.IsLetter or unicode.IsDigit: ComponentNamePattern is ASCII only, so "é" and "宠" are letters it nonetheless rejects, and "٣" is a digit it rejects.
func IsValidComponentName ¶ added in v1.58.0
IsValidComponentName reports whether name satisfies ComponentNamePattern.
This is the single implementation of that rule. Validation asks whether a document's names are legal and the fixer asks which characters it may keep when building a replacement, so both consume this rather than restating the charset; a hand-maintained copy on either side would be free to drift from the spec and from the other.
The empty string is not valid: the pattern's "+" requires at least one character. Callers that want to report an empty name differently should test for it before calling.
Example ¶
fmt.Println(IsValidComponentName("pkg.v1-Pet"))
fmt.Println(IsValidComponentName("pkg/Pet"))
Output: true false
func SanitizeComponentName ¶ added in v1.70.1
SanitizeComponentName reduces name to one that satisfies ComponentNamePattern: accents are folded, every other disallowed rune becomes "_", runs of "_" collapse and the ends are trimmed. "*Pet" becomes "Pet" and "map[string]interface {}" becomes "map_string_interface".
The result is empty when nothing of name survives (a name made only of punctuation, or of a script with no ASCII form). Two different names can sanitize to the same result, so a caller that renames must resolve the collision itself.
func ToCamelCase ¶
ToCamelCase converts a string to camelCase. Like PascalCase but with the first letter lowercase. Example: "user_profile" -> "userProfile" Example: "UserProfile" -> "userProfile"
func ToKebabCase ¶
ToKebabCase converts a string to kebab-case. Like snake_case but with hyphens instead of underscores. Example: "UserProfile" -> "user-profile"
func ToPascalCase ¶
ToPascalCase converts a string to PascalCase. Separators (underscore, hyphen, dot, slash) trigger capitalization of the next letter. Example: "user_profile" -> "UserProfile" Example: "api-client" -> "ApiClient"
func ToSnakeCase ¶
ToSnakeCase converts a string to snake_case. Uppercase letters are prefixed with underscore and lowercased. Existing separators (hyphen, dot, slash) are converted to underscores. Example: "UserProfile" -> "user_profile" Example: "APIClient" -> "api_client"
Types ¶
This section is empty.