settings

package
v0.1.60 Latest Latest
Warning

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

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

Documentation

Overview

Package settings provides typed access to protobuf-backed settings.

Product code works exclusively with generated protobuf messages and typed Field values. JSON is an implementation detail of the persistence boundary.

Index

Constants

View Source
const DefaultMaximumJSONBytes = 128 * 1024

Variables

View Source
var ErrNilMessage = errors.New("settings message is nil")

Functions

This section is empty.

Types

type Field

type Field[M proto.Message, T any] struct {
	// contains filtered or unexported fields
}

Field is a typed path from a generated protobuf settings message M to a scalar value T. Paths are resolved and validated once when the field catalog is initialized. Set materializes missing parent messages automatically.

func MustBool

func MustBool[M proto.Message](prototype M, path string, defaultValue bool) Field[M, bool]

MustBool defines an optional bool settings field.

func MustEnum

func MustEnum[M proto.Message, E ~int32](prototype M, path string, defaultValue E) Field[M, E]

MustEnum defines an optional enum settings field. E is the generated enum type (whose underlying type is int32). Set rejects values unknown to the field's enum descriptor.

func MustInt32

func MustInt32[M proto.Message](prototype M, path string, defaultValue int32) Field[M, int32]

MustInt32 defines an optional int32 settings field.

func MustInt64

func MustInt64[M proto.Message](prototype M, path string, defaultValue int64) Field[M, int64]

MustInt64 defines an optional int64 settings field.

func MustString

func MustString[M proto.Message](prototype M, path, defaultValue string) Field[M, string]

MustString defines an optional string settings field and panics when the path is invalid. Field catalogs should be package-level values so schema mistakes fail during process initialization rather than on a request.

func (Field[M, T]) ApplyDefault added in v0.1.60

func (field Field[M, T]) ApplyDefault(message M) (bool, error)

ApplyDefault writes the configured default only when the field is absent. It returns true when the message changed. Existing values, including explicit false, empty string, zero, or the zero enum, are never overwritten.

func (Field[M, T]) Clear

func (field Field[M, T]) Clear(message M) error

Clear removes a value. Empty parent messages created solely for the cleared path are pruned, so ProtoJSON does not persist meaningless {"parent": {}}.

func (Field[M, T]) Default added in v0.1.60

func (field Field[M, T]) Default() T

Default returns the field's configured fallback value.

func (Field[M, T]) Get

func (field Field[M, T]) Get(message M) (T, error)

Get returns the explicitly stored value, or the field's configured default when the field or any of its parent messages is absent.

func (Field[M, T]) Has

func (field Field[M, T]) Has(message M) (bool, error)

Has reports whether the complete path is explicitly present.

func (Field[M, T]) Lookup

func (field Field[M, T]) Lookup(message M) (T, bool, error)

Lookup returns the value and its protobuf presence. An explicit scalar zero (false, "", 0, or the zero enum) remains distinguishable from an unset field when the schema declares presence, as settings protos should.

func (Field[M, T]) Path

func (field Field[M, T]) Path() string

Path returns the stable protobuf field path, using proto field names.

func (Field[M, T]) Set

func (field Field[M, T]) Set(message M, value T) error

Set writes a typed value and materializes every missing parent message.

type JSONCodec

type JSONCodec[M proto.Message] struct {
	// contains filtered or unexported fields
}

JSONCodec is the only conversion boundary between typed protobuf settings and their JSONB representation. UseProtoNames keeps persisted keys stable across Go and TypeScript, while DiscardUnknown allows safe rolling schema removal without deleting older keys from the database.

func MustJSONCodec

func MustJSONCodec[M proto.Message](newMessage func() M, maximumBytes int) *JSONCodec[M]

func NewJSONCodec

func NewJSONCodec[M proto.Message](newMessage func() M, maximumBytes int) (*JSONCodec[M], error)

func (*JSONCodec[M]) Marshal

func (codec *JSONCodec[M]) Marshal(message M) ([]byte, error)

func (*JSONCodec[M]) Unmarshal

func (codec *JSONCodec[M]) Unmarshal(encoded []byte) (M, error)

Jump to

Keyboard shortcuts

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