tscommon

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TSString  = "string"
	TSNumber  = "number"
	TSBoolean = "boolean"
)

Variables

This section is empty.

Functions

func BuildOneofFieldSet

func BuildOneofFieldSet(discriminatedOneofs []*annotations.OneofDiscriminatorInfo) map[*protogen.Field]bool

BuildOneofFieldSet returns a set of fields that belong to discriminated oneofs.

func GenerateEnumType

func GenerateEnumType(p Printer, enum *protogen.Enum)

GenerateEnumType writes a TypeScript string union type for a protobuf enum. Uses custom enum_value annotations if present, otherwise uses proto names.

func GenerateFieldDeclaration

func GenerateFieldDeclaration(p Printer, field *protogen.Field)

GenerateFieldDeclaration generates a single TypeScript field declaration line.

func GenerateFlattenedFields

func GenerateFlattenedFields(p Printer, childMsg *protogen.Message, prefix string)

GenerateFlattenedFields inlines child message fields at the parent level with optional prefix.

func GenerateFlattenedOneofInterface

func GenerateFlattenedOneofInterface(
	p Printer,
	msg *protogen.Message,
	name string,
	discriminatedOneofs []*annotations.OneofDiscriminatorInfo,
)

GenerateFlattenedOneofInterface generates a type alias with intersection for messages with flattened discriminated oneofs.

func GenerateInterface

func GenerateInterface(p Printer, msg *protogen.Message)

GenerateInterface writes a TypeScript interface for a protobuf message. If the message has discriminated oneofs, it generates appropriate union types.

func GenerateOneofDiscriminatedUnionType

func GenerateOneofDiscriminatedUnionType(p Printer, msgName string, info *annotations.OneofDiscriminatorInfo)

GenerateOneofDiscriminatedUnionType generates a TypeScript discriminated union type for a oneof.

func GenerateStandardInterface

func GenerateStandardInterface(
	p Printer,
	msg *protogen.Message,
	name string,
	discriminatedOneofs []*annotations.OneofDiscriminatorInfo,
)

GenerateStandardInterface generates a standard interface, handling non-flattened discriminated oneofs as optional union properties.

func HeaderNameToPropertyName

func HeaderNameToPropertyName(headerName string) string

HeaderNameToPropertyName converts "X-API-Key" to "apiKey".

func IsOptionalField

func IsOptionalField(field *protogen.Field) bool

IsOptionalField returns true if the field should be optional in TypeScript.

func RootUnwrapTSType

func RootUnwrapTSType(msg *protogen.Message) string

RootUnwrapTSType returns the TypeScript type for a root-unwrapped message.

func SnakeToLowerCamel

func SnakeToLowerCamel(s string) string

SnakeToLowerCamel converts "user_id" to "userId".

func SnakeToUpperCamel

func SnakeToUpperCamel(s string) string

SnakeToUpperCamel converts snake_case to UpperCamelCase.

func TSElementType

func TSElementType(field *protogen.Field) string

TSElementType returns the TypeScript type for the element of a repeated field.

func TSEnumUnspecifiedValue added in v0.8.0

func TSEnumUnspecifiedValue(field *protogen.Field) string

TSEnumUnspecifiedValue returns the first enum value (UNSPECIFIED variant) as a quoted string. For enum fields, this is the zero-value equivalent used in TS zero checks.

func TSFieldType

func TSFieldType(field *protogen.Field) string

TSFieldType returns the TypeScript type string for a protobuf field.

func TSScalarType

func TSScalarType(kind protoreflect.Kind) string

TSScalarType returns the TypeScript type for a protobuf scalar kind. This is the base helper that uses only kind information (no field context). For int64/uint64 fields, callers should use TSScalarTypeForField to check encoding annotations.

func TSScalarTypeForField

func TSScalarTypeForField(field *protogen.Field) string

TSScalarTypeForField returns the TypeScript type for a protobuf field, checking encoding annotations for int64/uint64 fields.

func TSTimestampType

func TSTimestampType(field *protogen.Field) string

TSTimestampType returns the TypeScript type for a google.protobuf.Timestamp field based on its timestamp_format annotation. UNIX_SECONDS and UNIX_MILLIS serialize as integers -> number RFC3339, DATE, and default serialize as strings -> string.

func TSZeroCheck

func TSZeroCheck(fieldKind string) string

TSZeroCheck returns the TypeScript zero-value check expression for a query param. Uses the proto field kind (not TS type) to determine the appropriate check. For int64/uint64 fields, this returns the STRING encoding check; use TSZeroCheckForField when the full field context is available to check encoding annotations.

func TSZeroCheckForField

func TSZeroCheckForField(field *protogen.Field) string

TSZeroCheckForField returns the TypeScript zero-value check expression for a field, checking encoding annotations for int64/uint64 fields.

func WriteErrorTypes

func WriteErrorTypes(p Printer)

WriteErrorTypes writes the shared error types (FieldViolation, ValidationError, ApiError).

Types

type MessageSet

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

MessageSet tracks collected messages by full name to deduplicate.

func CollectServiceMessages

func CollectServiceMessages(file *protogen.File) *MessageSet

CollectServiceMessages collects all messages transitively referenced by services in a file. It also includes messages whose names end with "Error" (convention for proto-defined custom errors).

func NewMessageSet

func NewMessageSet() *MessageSet

NewMessageSet creates a new MessageSet.

func (*MessageSet) AddEnum

func (ms *MessageSet) AddEnum(enum *protogen.Enum)

AddEnum adds an enum to the collection.

func (*MessageSet) AddMessage

func (ms *MessageSet) AddMessage(msg *protogen.Message)

AddMessage adds a message and recursively adds all referenced messages.

func (*MessageSet) OrderedEnums

func (ms *MessageSet) OrderedEnums() []*protogen.Enum

OrderedEnums returns enums sorted by full name.

func (*MessageSet) OrderedMessages

func (ms *MessageSet) OrderedMessages() []*protogen.Message

OrderedMessages returns messages in discovery order.

type Printer

type Printer func(format string, args ...interface{})

Printer is a function that prints a formatted line.

Jump to

Keyboard shortcuts

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