naming

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 6 Imported by: 0

README

openapi-go-naming

Go Reference CI

Fast, deterministic conversion of OpenAPI identifiers into idiomatic, collision-safe Go names.

Zero dependencies. Built entirely on Go's standard library. Requires Go 1.25 or newer.

import naming "github.com/giraffesyo/openapi-go-naming"

typeName := naming.Exported("user_profile")   // UserProfile
fieldName := naming.Exported("account_id")    // AccountID
paramName := naming.Unexported("account_id")  // accountID

Every conversion returns a valid Go identifier. Exported names are guaranteed to be exported, unexported names avoid Go keywords, Unicode is supported, and identifiers beginning with digits receive a stable prefix.

Input Exported Unexported
user_id UserID userID
HTTPServerURL HTTPServerURL httpServerURL
some.dotted-name SomeDottedName someDottedName
123-response N123Response n123Response
type Type type_
用户_id X用户ID 用户ID

Initialisms

The default converter recognizes conventional Go initialisms such as API, HTTP, ID, JSON, URL, and UUID. Add project-specific initialisms with an immutable converter:

converter := naming.NewConverter("GPU")

converter.Exported("gpu_limit")   // GPULimit
converter.Unexported("gpu_limit") // gpuLimit

Converters are safe for concurrent use.

Collision handling

Scope allocates unique names without changing the first occurrence:

scope := naming.NewScope("User2")

scope.Unique("User") // User
scope.Unique("User") // User3
scope.Unique("User") // User4

The zero value is ready to use, and all operations are safe for concurrent use.

Performance

Conversion uses a single-pass tokenizer and writes directly into one output buffer. Common ASCII conversions allocate only the returned string. Run the included benchmarks with:

go test -bench . -benchmem -run '^$' ./...

License

MIT.

Documentation

Overview

Package naming converts external identifiers into idiomatic Go identifiers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exported

func Exported(input string) string

Exported converts input to an exported Go identifier in PascalCase.

func Unexported

func Unexported(input string) string

Unexported converts input to an unexported Go identifier in camelCase.

Types

type Converter

type Converter struct {
	// contains filtered or unexported fields
}

Converter converts identifiers using an immutable set of initialisms. It is safe for concurrent use.

func NewConverter

func NewConverter(initialisms ...string) *Converter

NewConverter returns a converter containing the standard Go initialisms plus each additional canonical spelling. Matching is case-insensitive.

For example, NewConverter("GPU") converts "gpu_limit" to "GPULimit".

func (*Converter) Exported

func (c *Converter) Exported(input string) string

Exported converts input to an exported Go identifier in PascalCase.

func (*Converter) Unexported

func (c *Converter) Unexported(input string) string

Unexported converts input to an unexported Go identifier in camelCase.

type Scope

type Scope struct {
	// contains filtered or unexported fields
}

Scope allocates unique identifiers within a namespace. Its zero value is ready to use, and its methods are safe for concurrent use. A Scope must not be copied after first use.

func NewScope

func NewScope(reserved ...string) *Scope

NewScope returns a scope with reserved identifiers already marked as used.

func (*Scope) Reserve

func (s *Scope) Reserve(identifiers ...string)

Reserve marks identifiers as unavailable. Reserving an identifier more than once has no effect.

func (*Scope) Unique

func (s *Scope) Unique(identifier string) string

Unique returns identifier when it is available. Otherwise, it appends the lowest available decimal suffix beginning with 2.

Jump to

Keyboard shortcuts

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