naming

package
v0.6.7 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package naming converts between the three casings rig deals with: the snake_case of Postgres, the PascalCase of Go and TypeScript identifiers, and the camelCase of JSON keys.

The conversions are deliberately asymmetric. A Go identifier uppercases known initialisms, so fixture_id becomes FixtureID; a JSON key does not, so the same column becomes fixtureId. Applying Go's initialism convention to wire formats produces keys like fixtureID that no other language's client would generate.

Index

Constants

This section is empty.

Variables

View Source
var DefaultInitialisms = []string{
	"ACL", "API", "ASCII", "CIDR", "CPU", "CSS", "CSV", "DB", "DNS", "EOF",
	"GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "JWT", "LHS", "MIME",
	"QPS", "RAM", "RHS", "RPC", "SLA", "SMTP", "SQL", "SSH", "SSL", "TCP",
	"TLS", "TTL", "UDP", "UI", "UID", "URI", "URL", "UTF8", "UUID", "VM",
	"XML", "XSS", "YAML",
}

DefaultInitialisms are the acronyms that stay fully uppercase in Go identifiers. A project adds its own through Config.

Functions

func Kebab

func Kebab(s string) string

Kebab converts an identifier to kebab-case, used for URL path segments.

func Snake

func Snake(s string) string

Snake converts an identifier to snake_case.

func Words

func Words(s string) []string

Words splits an identifier into its component words, accepting snake_case, kebab-case, PascalCase, camelCase, or any mixture. Runs of capitals are kept together up to the last one that starts a new word, so "APIKey" splits into "API" and "Key" rather than into single letters.

Types

type Case

type Case string

Case is the shape of a JSON key.

const (
	CaseCamel  Case = "camel"
	CasePascal Case = "pascal"
	CaseSnake  Case = "snake"
)

type Config

type Config struct {
	// Initialisms replaces [DefaultInitialisms] when non-empty.
	Initialisms []string
	// ExtraInitialisms are added to whichever base list is in use. This is the
	// usual knob: a project adds its own acronyms without restating Go's.
	ExtraInitialisms []string
	// Plurals overrides the derived plural for specific singular names, keyed
	// by the singular form as written in the database.
	Plurals map[string]string
	// JSONCase is the shape of generated JSON keys. Defaults to camel.
	JSONCase Case
}

Config configures a Namer.

type Namer

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

Namer performs the conversions. The zero value is not usable; call New.

func New

func New(cfg Config) *Namer

New builds a namer from a configuration.

func (*Namer) Go

func (n *Namer) Go(s string) string

Go converts an identifier to an exported Go name, uppercasing initialisms.

email_address -> EmailAddress
fixture_id    -> FixtureID
api_key       -> APIKey

func (*Namer) GoUnexported

func (n *Namer) GoUnexported(s string) string

GoUnexported is Namer.Go with a lowercase first word, for local variables and unexported fields. An initialism at the start is lowercased whole, so "id_token" becomes "idToken" rather than "iDToken".

func (*Namer) JSON

func (n *Namer) JSON(s string) string

JSON converts an identifier to a JSON key in the configured case. Initialisms are not uppercased: fixture_id becomes fixtureId, because that is what a client generator in any other language would produce.

func (*Namer) PathSegment

func (n *Namer) PathSegment(plural string) string

PathSegment converts a plural resource name into a URL path segment.

LessonTimes -> lesson-times

func (*Namer) Plural

func (n *Namer) Plural(s string) string

Plural returns the plural of a singular name. The override map wins; failing that, a handful of English rules apply. The rules are deliberately shallow — they get the common cases right and the override map exists for the rest.

Jump to

Keyboard shortcuts

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