naming

package
v1.0.0 Latest Latest
Warning

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

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

Documentation

Overview

Package naming turns spec names into Go names and settles clashes between them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ImportName

func ImportName(path string) string

ImportName returns the name the package at an import path has by the usual layout of module paths: a major version suffix (v2) and a go- prefix are not part of the name, nor is anything after a dot (yaml.v3). It is empty when what is left is no Go name.

func Package

func Package(dir string) string

Package returns the Go package name for a folder: its name in lower case, letters and digits only, no leading digits. It is "api" when nothing is left or the name is a keyword.

Types

type Assignment

type Assignment struct {
	ID   string
	Name string
}

type Namer

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

Namer builds Go names. Suffix methods keep their first argument as is and convert the rest.

func New

func New(extra []string) *Namer

New returns a Namer that writes the default initialisms and extra in upper case.

func (*Namer) ArrayItem

func (n *Namer) ArrayItem(parent string) string

ArrayItem names the type of an inline array item: OrderItem.

func (*Namer) ClientOption

func (n *Namer) ClientOption(client string) string

ClientOption names the option type of a client: ClientOption.

func (*Namer) ClientRequestOptions

func (n *Namer) ClientRequestOptions(op string) string

ClientRequestOptions names what the client method of an operation sends.

func (*Namer) ClientResponse

func (n *Namer) ClientResponse(op string) string

ClientResponse names what the WithResponse client method of an operation returns.

func (*Namer) EnumConst

func (n *Namer) EnumConst(typ, value string) string

EnumConst names an enum constant prefixed with its type: StatusActive.

func (*Namer) EnumValues

func (n *Namer) EnumValues(typ string) string

EnumValues names the func that returns every value of an enum: StatusValues.

func (*Namer) Exported

func (n *Namer) Exported(parts ...string) string

Exported joins parts into one exported CamelCase identifier.

func (*Namer) InlineProperty

func (n *Namer) InlineProperty(parent, prop string) string

InlineProperty names the type of an inline schema under a property: OrderClientAddress.

func (*Namer) Interface

func (n *Namer) Interface(name string) string

Interface names the interface of a service or a client: ServiceInterface, ClientInterface.

func (*Namer) MapValue

func (n *Namer) MapValue(parent string) string

MapValue names the type of an inline additionalProperties schema: LabelsValue.

func (*Namer) MediaTag

func (n *Namer) MediaTag(contentType string) string

MediaTag shortens a media type: application/problem+json gives ProblemJSON.

func (*Namer) Params

func (n *Namer) Params(op, in string) string

Params names the struct for one parameter location: GetPetPathParams, GetPetQuery.

func (*Namer) RequestBody

func (n *Namer) RequestBody(op, contentType string, multiple bool) string

RequestBody adds the media type only when the operation has more than one body.

func (*Namer) Response

func (n *Namer) Response(op, status, contentType string, multiple bool) string

Response names an inline response: GetPetResponse200, GetPetResponse4XX, GetPetResponseDefault. The media type is added only when the response has more than one: GetPetJSONResponse200.

func (*Namer) ResponseConstructor

func (n *Namer) ResponseConstructor(op, status string, multiple bool) string

ResponseConstructor names the function that makes the response data of an operation from the body of one status: NewGetPetResponseData, or NewGetPetResponseData404 among several.

func (*Namer) ResponseData

func (n *Namer) ResponseData(op string) string

ResponseData names what the service method of an operation returns.

func (*Namer) ResponseHeaders

func (n *Namer) ResponseHeaders(op, status string) string

ResponseHeaders names the typed headers of a response: GetPetResponse200Headers.

func (*Namer) ResponseItem

func (n *Namer) ResponseItem(op string) string

ResponseItem names the inline schema of one frame of a streamed response: GetEventsResponseItem.

func (*Namer) ServiceRequestOptions

func (n *Namer) ServiceRequestOptions(op string) string

ServiceRequestOptions names what the service method of an operation receives.

func (*Namer) Snake

func (n *Namer) Snake(name string) string

Snake writes a Go name in snake case: GetPetByID gives get_pet_by_id, HTTP2Stats http2_stats.

func (*Namer) Status

func (n *Namer) Status(status string) string

Status writes a status as a name part: 200, 4XX, Default.

func (*Namer) ToolInput

func (n *Namer) ToolInput(op string) string

ToolInput names what the MCP tool of an operation receives.

func (*Namer) Unexported

func (n *Namer) Unexported(parts ...string) string

Unexported joins parts into one lowerCamel identifier. A keyword or predeclared name gets "Val".

func (*Namer) UnionVariant

func (n *Namer) UnionVariant(parent, title, discValue string, index int) string

UnionVariant uses the title, else the discriminator value, else Option and the position from 1.

type Rank

type Rank int

Rank orders requests for the same name: the higher rank keeps it.

const (
	RankInline Rank = iota
	RankOperation
	RankComponent
	RankComponentSchema
	RankGoName
)

type Rename

type Rename struct {
	ID        string
	From      string
	To        string
	Holder    string
	IsDerived bool
	Rank      Rank
	Origin    diag.Origin
}

Rename records a lost name. From is the name that was taken: Want, or Want plus one of its Derived suffixes. Holder is the ID that has From, empty when From is reserved; IsDerived says Holder built it from its own name.

func (Rename) Diagnostic

func (r Rename) Diagnostic() diag.Diagnostic

Diagnostic reports the rename; a lost x-go-name is a warning, anything else is info.

type Request

type Request struct {
	ID   string
	Want string
	// Fallback is tried before numbering: Fallback2, Fallback3, or Want2 without a Fallback.
	Fallback string
	// Derived are the suffixes of the names the generator builds from the name, such as methods:
	// the request holds its name plus each of them too, so a name is free only when all are.
	Derived []string
	Rank    Rank
	Order   int
	Origin  diag.Origin
}

Request asks for one name. ID is unique within a Resolve call, usually a JSON pointer.

type Result

type Result struct {
	Names   map[string]string
	Ordered []Assignment
	Renames []Rename
}

Result maps IDs to names. Range over Ordered, never over Names, to keep output deterministic.

func Resolve

func Resolve(reserved []string, reqs []Request) Result

Resolve compares names ignoring case and goes by rank, spec order and ID, never input order. A request whose Want is another one's Want plus one of its Derived suffixes goes after that one, so it is the one renamed.

Jump to

Keyboard shortcuts

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