util

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidFieldName = errors.New("invalid field name")

ErrInvalidFieldName signals that a JSON field name does not match the allowed identifier pattern. Callers at the public API boundary should wrap this with den.ErrValidation so users can errors.Is against that sentinel. Kept separate here to avoid an import cycle with the root package.

Functions

func CollectionName

func CollectionName(typeName string) string

CollectionName derives the collection name from a Go type name. Simply lowercases the full name, no pluralization.

func EscapeLike

func EscapeLike(s string) string

EscapeLike escapes LIKE special characters (%, _, \) in a value.

func IdentifierSegment added in v0.14.0

func IdentifierSegment(jsonName string) string

IdentifierSegment turns a (possibly dotted) JSON field name into a fragment that's safe inside a bare SQL identifier. Dotted JSON paths (`profile.bio`) are correct in `json_extract` / `jsonb_extract_path` expressions, but break in identifier positions like CREATE INDEX names or FTS5 virtual-table column lists where the dot is parsed as a `table.column` qualifier. Callers keep the dotted form for the extraction expression and use this helper for the identifier.

func IsLinkShape added in v0.14.0

func IsLinkShape(t reflect.Type) bool

IsLinkShape reports whether t structurally matches the engine.Link[T] type — an ID/Value/Loaded triple with a string-typed ID.

Lives in internal/util so the schema walker (which cannot import engine without a cycle) can skip Link fields, and so the engine package has a single source of truth for the shape check.

func ParseJSONTagName

func ParseJSONTagName(tag string) string

ParseJSONTagName extracts the field name from a json struct tag. Returns "" if no json tag or if tagged with "-".

func SanitizeFieldName

func SanitizeFieldName(field string) string

SanitizeFieldName strips characters that are not safe for JSON path interpolation. Allows letters, digits, underscores, and dots (for nested paths).

func ValidateFieldName

func ValidateFieldName(name string) error

ValidateFieldName reports whether the given JSON field name is safe to interpolate into SQL and consistent with SQL identifier conventions. Returns an error wrapping ErrInvalidFieldName on rejection.

Types

type FieldInfo

type FieldInfo struct {
	JSONName  string
	GoName    string
	Type      reflect.Type
	Index     []int // reflect index path for nested access
	Options   TagOptions
	IsPointer bool
}

FieldInfo describes a single field in a document struct.

type StructInfo

type StructInfo struct {
	CollectionName string
	Fields         []FieldInfo

	GoType       reflect.Type
	HasDeletedAt bool

	// HasValidateTags is true when at least one field — including fields of
	// anonymous embedded structs — carries a non-empty `validate:` struct
	// tag. The write path uses this to skip the go-playground reflective
	// walk for types that have nothing to validate (the dominant case in
	// profiled workloads). Custom Validator.Validate(ctx) hooks are
	// unaffected; only the tag-driven walk is gated.
	HasValidateTags bool

	// Pre-resolved pointers to the base fields embedded by document.Base /
	// document.SoftDelete / document.Tracked. Populated once by
	// AnalyzeStruct so hot paths can skip per-op FieldByName lookups. Any
	// of these may be nil if the struct does not embed the corresponding
	// base field.
	BaseID        *FieldInfo
	BaseRev       *FieldInfo
	BaseCreatedAt *FieldInfo
	BaseUpdatedAt *FieldInfo
	BaseDeletedAt *FieldInfo
	// contains filtered or unexported fields
}

StructInfo holds analyzed metadata for a document struct.

func AnalyzeStruct

func AnalyzeStruct(t reflect.Type) (*StructInfo, error)

AnalyzeStruct analyzes a struct type and extracts field metadata from json and den struct tags. Embedded structs are flattened.

func (*StructInfo) FieldByName

func (s *StructInfo) FieldByName(jsonName string) *FieldInfo

FieldByName returns the FieldInfo for the given JSON field name, or nil.

func (*StructInfo) IndexedFields

func (s *StructInfo) IndexedFields() []FieldInfo

IndexedFields returns all fields with the index option set.

func (*StructInfo) UniqueFields

func (s *StructInfo) UniqueFields() []FieldInfo

UniqueFields returns all fields with the unique option set.

type TagOptions

type TagOptions struct {
	Index          bool
	Unique         bool
	FTS            bool
	OmitEmpty      bool
	Eager          bool   // valid on Link[T] / []Link[T] fields — auto-hydrate by default
	UniqueTogether string // group name for composite unique index
	IndexTogether  string // group name for composite non-unique index
}

TagOptions holds the parsed options from a den struct tag.

func ParseDenTag

func ParseDenTag(tag string) (TagOptions, error)

ParseDenTag parses a den struct tag for metadata options only. Format: "option1,option2,..." (no field name — that comes from json tag). Returns an error for unknown tag options to catch typos like "indx".

Jump to

Keyboard shortcuts

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