Documentation
¶
Overview ¶
Package product is the catalog layer over OpenJD templates: it parses product definition files, holds the embedded built-ins, validates inline templates, and overlays read-only built-ins on the store via Catalog.
Index ¶
- Constants
- Variables
- func Builtins() []store.Product
- func ParseDefinition(data []byte, opts ValidateOptions) (store.Product, error)
- func ValidateMetadata(p store.Product) error
- func ValidateTemplate(rawTemplate string, format store.TemplateFormat, opts ValidateOptions) error
- type Catalog
- func (c *Catalog) Create(ctx context.Context, p store.Product) (store.Product, error)
- func (c *Catalog) Delete(ctx context.Context, name string) error
- func (c *Catalog) GetByName(ctx context.Context, name string) (store.Product, error)
- func (c *Catalog) Install(ctx context.Context, def store.Product, ref, fingerprint string) (store.Product, bool, error)
- func (c *Catalog) List(ctx context.Context) ([]store.Product, error)
- func (c *Catalog) Update(ctx context.Context, p store.Product) (store.Product, error)
- type ValidateOptions
Constants ¶
const ( MaxNameLen = 128 MaxTitleLen = 200 MaxDescriptionLen = 500 MaxReadmeLen = 8000 MaxCategoryLen = 64 MaxVersionLen = 32 )
Metadata length limits, in RUNES rather than bytes: 500 bytes of CJK is ~166 characters, and a Japanese-language description must not be silently third-class.
The two interesting caps differ in kind. MaxDescriptionLen is a design constraint -- description is rendered into an unclamped picker card and a native Blender EnumProperty tooltip, and 500 is 1.5x the longest description that survived commit a1e529e's hand-trim (329). It would have REJECTED the 940-, 629- and 617-rune descriptions that forced that commit, which is the test a cap should pass; a 1000 cap would have permitted all three. MaxReadmeLen is only an abuse guard -- readme is detail-page-only, so nothing downstream breaks; it simply should not be a novel.
The rest have far more headroom than the shipped presets need (their maxima are name 35, title 37, category 11). They exist because capping description while leaving its neighbors unbounded would be incoherent once the helper exists.
Variables ¶
var ErrReadOnly = errors.New("product: read-only")
ErrReadOnly is returned when a mutation targets a read-only product (built-in or installed preset).
Functions ¶
func Builtins ¶
Builtins returns the embedded built-in products, sorted by name. The returned slice is a copy; callers may not mutate the shared definitions.
func ParseDefinition ¶
func ParseDefinition(data []byte, opts ValidateOptions) (store.Product, error)
ParseDefinition parses a YAML product definition file (metadata + inline template) into a store.Product. Source is left empty for the caller to set. The inline template is re-serialized to YAML and validated under opts; a malformed template is an error, and so is a breach of opts.Deadline (wrapping [expr.ErrDeadlineExceeded] -- see ValidateTemplate for why the two must be told apart).
opts is a REQUIRED argument rather than an implicit default because the callers differ in what they can and must supply, and an implicit default is how the preset routes silently kept validating on openjd.DefaultExprLimits() after the create/update route was fixed:
- [LoadBuiltins], from package init, has no configuration to offer and passes only EnforceLimits. That one is a genuine exemption.
- internal/presetlib, reached from GET /api/v1/presets/{name} and POST /api/v1/presets/{name}/install, passes the operator's limits and a per-request deadline.
func ValidateMetadata ¶ added in v0.3.0
ValidateMetadata enforces the length limits on a product's metadata fields.
It is exported and deliberately called from BOTH doors into product data -- ParseDefinition and the REST create/update handler. Those are separate entry points to the same data, and the comment on ValidateOptions records this exact trap biting once already: the preset routes silently kept validating on DefaultExprLimits() after the create/update route was fixed.
It checks LENGTH only. The slug pattern stays in validateName on the definition path, because applying it to the REST route would tighten acceptance and strand products already stored under a pattern-invalid name.
func ValidateTemplate ¶
func ValidateTemplate(rawTemplate string, format store.TemplateFormat, opts ValidateOptions) error
ValidateTemplate parses rawTemplate as OpenJD in the given format and runs the standard validation under opts. It returns nil when the template is valid, an error carrying the OpenJD ValidationErrors when it is not, or an error wrapping [expr.ErrDeadlineExceeded] when opts.Deadline passed mid-validation.
Those last two are different claims and callers must keep them apart: the ValidationErrors say the template is wrong, and a client that retries it will get the same answer forever; a deadline says THIS SERVER gave up on a template that might well validate on an idle machine. internal/api answers 400 for the first and 503 for the second. Match the deadline structurally with errors.Is(err, expr.ErrDeadlineExceeded), never by reading the message.
Types ¶
type Catalog ¶
type Catalog struct {
// contains filtered or unexported fields
}
Catalog overlays the embedded built-ins on a ProductStore and enforces the read-only / name-shadowing rules. It is the only component aware of built-ins.
func NewCatalog ¶
func NewCatalog(st store.ProductStore) *Catalog
NewCatalog returns a Catalog backed by st.
func (*Catalog) Create ¶
Create stores a new custom/installed product. It rejects names that shadow a built-in. An empty Source defaults to SourceCustom; an empty ID is assigned.
func (*Catalog) GetByName ¶
GetByName returns a built-in (preferred) or stored product, or ErrNotFound.
func (*Catalog) Install ¶
func (c *Catalog) Install(ctx context.Context, def store.Product, ref, fingerprint string) (store.Product, bool, error)
Install stores a preset fetched from the library as an installed product. It create-or-overwrites by name: a new name is created; an existing installed product of the same name is overwritten (the update path); a name shadowing a built-in or colliding with a custom product returns store.ErrConflict. The returned bool is true when a new product was created, false on overwrite.
type ValidateOptions ¶ added in v0.3.0
type ValidateOptions struct {
// EnforceLimits gates OpenJD's quantitative limit checks; see
// [openjd.ValidateOptions].
EnforceLimits bool
// ExprLimits is the operator's configured bound on what this template's
// EXPR expression walk may spend (sub-project E4d).
//
// The zero value means "use the defaults", which is what every caller with
// no operator configuration to offer gets. Before EXPR sub-project H1 that
// was the ONLY thing this path could get, so an operator who tightened the
// four knobs found product template validation still running on the
// defaults -- invisible while the walk is gated on EXPR being
// StatusSupported, and live the moment H2 flips it.
//
// EVERY ROUTE THAT REACHES THIS PACKAGE FROM AN HTTP REQUEST MUST SET IT.
// H1's own whole-branch review found the preset install path still on the
// defaults after the create/update route was fixed -- two routes behind the
// same permission (policy.ProductsManage), both ending in a catalog write,
// behaving differently. The limits question is not about who supplies the
// content: an operator who tightened a knob asked for it to be enforced
// wherever validation happens.
ExprLimits openjd.ExprLimits
// Deadline, when non-zero, is an absolute wall-clock instant after which
// validation stops and [ValidateTemplate] returns an error wrapping
// [expr.ErrDeadlineExceeded] -- H1's backstop.
//
// PER REQUEST, never stored: it is computed from a configured duration at
// the top of one HTTP request. See [openjd.ExprLimits]' Deadline field for
// what storing one on a long-lived value does.
Deadline time.Time
}
ValidateOptions bounds one ValidateTemplate or ParseDefinition call.
It exists because a product template is validated from places with very different trust and configuration properties. POST /api/v1/products and PUT /api/v1/products/{name} accept an ARBITRARY client-supplied body, anonymously when auth is off (the default), and need both the operator's configured EXPR limits and a wall-clock backstop. The preset routes (GET /api/v1/presets/{name}, POST /api/v1/presets/{name}/install) validate a sha256-pinned body from an operator-configured index -- not client-chosen content, but still one anonymous, repeatable request per validation, so they take both too. The built-in loader runs from package init, where no configuration exists at all, and can take neither.
The zero value reproduces the pre-H1 behavior exactly: limit enforcement off, default EXPR limits, no deadline.