internal

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 7 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 added in v0.7.0

func EscapeLike(s string) string

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

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 added in v0.7.0

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 added in v0.8.0

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 Encoder added in v0.7.0

type Encoder struct{}

Encoder provides JSON encoding/decoding for document storage.

func (*Encoder) Decode added in v0.7.0

func (e *Encoder) Decode(data []byte, v any) error

func (*Encoder) Encode added in v0.7.0

func (e *Encoder) Encode(v any) ([]byte, error)

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

	// 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