naming

package
v1.58.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 2 Imported by: 0

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, ToKebabCase, and ToTitleCase.

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.

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

Examples

Constants

View Source
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 IsComponentNameChar added in v1.58.0

func IsComponentNameChar(r rune) bool

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

func IsValidComponentName(name string) bool

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 ToCamelCase

func ToCamelCase(s string) string

ToCamelCase converts a string to camelCase. Like PascalCase but with the first letter lowercase. Example: "user_profile" -> "userProfile" Example: "UserProfile" -> "userProfile"

func ToKebabCase

func ToKebabCase(s string) string

ToKebabCase converts a string to kebab-case. Like snake_case but with hyphens instead of underscores. Example: "UserProfile" -> "user-profile"

func ToPascalCase

func ToPascalCase(s string) string

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

func ToSnakeCase(s string) string

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"

func ToTitleCase

func ToTitleCase(s string) string

ToTitleCase converts the first letter to uppercase. Example: "hello" -> "Hello"

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL