casing

package module
v1.173.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

  • Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved.
  • SPDX-License-Identifier: Apache-2.0

Package casing rewrites identifier-like strings according to declarative token replacement rules.

"Identifier-like strings" here means names that are structured as code or configuration identifiers rather than arbitrary prose, for example:

  • camelCase names such as `apiClient` or `sourceName`
  • PascalCase names such as `ApiClient` or `SourceName`
  • mixed acronym-containing identifiers such as `HttpAPIClient`

It is intended for cases where callers need to preserve the overall casing shape of such identifiers while forcing specific words to use a canonical replacement and exempting other whole words through an exception list.

This differs from simpler alternatives such as:

  • `strings.Replacer`, which is useful for literal substring replacement but does not understand identifier word boundaries or case reconstruction
  • regexp-based rewriting, which can express some token patterns but becomes awkward when replacement depends on identifier-style word splitting, acronym policies, and explicit exception lists

The package therefore combines identifier splitting with rule-based word replacement, rather than treating the input as an arbitrary free-form string.

Unlike libraries that automatically apply a built-in Go initialism list, this package only applies that behaviour when explicitly requested. Callers can opt into it by creating a replacer from InitialismRules or by reusing the ready-made InitialismReplacer. This keeps the package predictable for domain-specific casing policies while still providing built-in optional Go initialism support when wanted.

Typical uses include:

  • normalising known acronyms in generated identifiers
  • preserving exception words that should not be rewritten
  • applying a shared replacement policy across tools or generators

Related references:

The case-conversion helpers in this package accept `...*Replacer` so callers can use one common helper shape whether they want replacement or not. Only the first replacer is used; passing more than one replacer is not supported.

  • Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved.

  • SPDX-License-Identifier: Apache-2.0

  • Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved.

  • SPDX-License-Identifier: Apache-2.0

  • Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved.

  • SPDX-License-Identifier: Apache-2.0

  • Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved.

  • SPDX-License-Identifier: Apache-2.0

  • Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved.

  • SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

View Source
var InitialismReplacer = mustNewReplacer(InitialismRules...)

InitialismReplacer is a ready-to-use replacer built from InitialismRules.

This variable provides built-in optional Go initialism support without requiring callers to construct a replacer explicitly.

View Source
var InitialismRules = []Rule{
	{Token: "Acl", Replacement: "ACL"},
	{Token: "Api", Replacement: "API"},
	{Token: "Ascii", Replacement: "ASCII"},
	{Token: "Cpu", Replacement: "CPU"},
	{Token: "Css", Replacement: "CSS"},
	{Token: "Dns", Replacement: "DNS"},
	{Token: "Eof", Replacement: "EOF"},
	{Token: "Guid", Replacement: "GUID"},
	{Token: "Html", Replacement: "HTML"},
	{Token: "Http", Replacement: "HTTP"},
	{Token: "Https", Replacement: "HTTPS"},
	{Token: "Id", Replacement: "ID"},
	{Token: "Ip", Replacement: "IP"},
	{Token: "Json", Replacement: "JSON"},
	{Token: "Lhs", Replacement: "LHS"},
	{Token: "Qps", Replacement: "QPS"},
	{Token: "Ram", Replacement: "RAM"},
	{Token: "Rhs", Replacement: "RHS"},
	{Token: "Rpc", Replacement: "RPC"},
	{Token: "Sla", Replacement: "SLA"},
	{Token: "Smtp", Replacement: "SMTP"},
	{Token: "Sql", Replacement: "SQL"},
	{Token: "Ssh", Replacement: "SSH"},
	{Token: "Tcp", Replacement: "TCP"},
	{Token: "Tls", Replacement: "TLS"},
	{Token: "Ttl", Replacement: "TTL"},
	{Token: "Udp", Replacement: "UDP"},
	{Token: "Ui", Replacement: "UI"},
	{Token: "Uid", Replacement: "UID"},
	{Token: "Uuid", Replacement: "UUID"},
	{Token: "Uri", Replacement: "URI"},
	{Token: "Url", Replacement: "URL"},
	{Token: "Utf8", Replacement: "UTF8"},
	{Token: "Vm", Replacement: "VM"},
	{Token: "Xml", Replacement: "XML"},
	{Token: "Xmpp", Replacement: "XMPP"},
	{Token: "Xsrf", Replacement: "XSRF"},
	{Token: "Xss", Replacement: "XSS"},
}

InitialismRules defines replacement rules for the common Go initialisms.

This variable can be passed directly to NewReplacer to opt into built-in Go-style initialism normalisation, for example:

r, err := NewReplacer(InitialismRules...)

The list is based on the standard Go initialism guidance: https://go.dev/wiki/CodeReviewComments#initialisms

Functions

func ToCamelCase

func ToCamelCase(value string, replacers ...*Replacer) string

ToCamelCase converts value to camelCase and optionally applies a replacer to the resulting identifier. Only the first replacer is used.

func ToKebabCase

func ToKebabCase(value string, replacers ...*Replacer) string

ToKebabCase converts value to kebab-case and optionally applies a replacer to the identifier before the final case conversion. Only the first replacer is used.

func ToPascalCase

func ToPascalCase(value string, replacers ...*Replacer) string

ToPascalCase converts value to PascalCase and optionally applies a replacer to the resulting identifier. Only the first replacer is used.

func ToSnakeCase

func ToSnakeCase(value string, replacers ...*Replacer) string

ToSnakeCase converts value to snake_case and optionally applies a replacer to the identifier before the final case conversion. Only the first replacer is used.

Types

type Replacer

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

Replacer applies token replacement rules to identifier-like strings.

A replacer works at identifier word boundaries rather than by performing raw substring replacement. In practice, this means it splits names such as `OpenAiApiKey`, `openAiApiKey`, or `HTTPApiToken` into logical words, looks up each word against the configured rules, and then rebuilds the identifier while preserving its overall case style.

If a word matches a configured rule, the rule's replacement is used unless that word is listed in the rule's exceptions. If a word does not match any rule, or it is exempted by an exception, it is reconstructed according to the identifier shape being processed:

  • all-uppercase words are preserved as-is
  • the first word of a camelCase identifier remains lower camel case
  • subsequent words are emitted in PascalCase

This makes Replacer useful for normalising acronyms and canonical tokens such as `API`, `HTTP`, `ID`, or `AI` across generated names, configuration keys, and code identifiers without losing the surrounding naming convention.

func NewReplacer

func NewReplacer(rules ...Rule) (*Replacer, error)

NewReplacer constructs a Replacer from the provided rules.

func (*Replacer) Replace

func (r *Replacer) Replace(value string) string

Replace rewrites an identifier-like string according to the configured rules while preserving camelCase or PascalCase shape.

func (*Replacer) WriteString

func (r *Replacer) WriteString(ctx context.Context, w io.Writer, s string) (n int, err error)

WriteString rewrites s with Replace and writes the result to w using the repository's context-aware safe I/O helpers.

type Rule

type Rule struct {
	Token       string
	Replacement string
	Exceptions  []string
}

Rule defines a token replacement rule for identifier-like strings.

func MergeRules

func MergeRules(rules ...Rule) []Rule

MergeRules merges compatible duplicate rules and keeps the last conflicting rule for a given token.

Compatible rules share the same token and replacement and have their exception lists combined. Conflicting rules do not cause an error; the later rule replaces the earlier one. The order of the returned rules is not guaranteed.

func (Rule) IsCompatible

func (r Rule) IsCompatible(other *Rule) bool

If other is empty, the rules are treated as incompatible and false is returned immediately.

func (Rule) Merge

func (r Rule) Merge(other *Rule) Rule

If other is empty, r is returned unchanged.

func (*Rule) Validate

func (r *Rule) Validate() (err error)

Validate checks that the rule contains the required values.

Jump to

Keyboard shortcuts

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