facet

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package facet defines the category-specific ad attributes (price, mileage, hours, year, condition, ...) in one typed registry. Each facet's kind, unit handling, allowed values, validation, and display formatting live in a single Def so that adding a facet is a one-line registry entry rather than a schema/SQL/UI change.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatDateRange

func FormatDateRange(start, end string) string

FormatDateRange renders a compact date or date range for listing cards.

func SaleDateDetailDisplays

func SaleDateDetailDisplays(start, end string) []string

SaleDateDetailDisplays returns bullet labels for sale dates on the ad detail page.

func SaleWeekRange

func SaleWeekRange(preset string, now time.Time) (min, max time.Time, ok bool)

SaleWeekRange maps a when-filter preset to an inclusive sale_start_date range.

Types

type Def

type Def struct {
	Key        string
	Label      string
	Kind       Kind
	Form       FormWidget
	Filter     FilterWidget
	Units      []string // Int-only: allowed units stored in Value.Text; empty = no unit
	Suffix     string   // Int-only: constant suffix when Units is empty (e.g. "hrs")
	Compact    bool     // Int-only: render large values as e.g. "45K" on cards
	Enum       []string // enum options for FormSelect and FormRadio
	SelectMin  int      // FormSelect lower bound when Kind == Int (inclusive)
	SelectMax  int      // FormSelect upper bound when Kind == Int; 0 = current calendar year + 1
	Filterable bool     // show in the search filter panel
	Required   bool     // required on the new-ad form
}

Def is the definition of a single facet.

func All

func All() []Def

All returns every registered facet definition.

func Get

func Get(key string) (Def, bool)

Get returns the definition for a facet key.

func (Def) CardLabel

func (d Def) CardLabel() bool

CardLabel reports whether this facet appears after the title on listing cards.

func (Def) EmbeddingSnippet

func (d Def) EmbeddingSnippet(v Value) string

EmbeddingSnippet returns a prompt line for vector encoding.

func (Def) FormDefaultCurrency

func (d Def) FormDefaultCurrency(defaults FormDefaults) string

FormDefaultCurrency returns the selected currency for a money facet.

func (Def) FormDefaultUnit

func (d Def) FormDefaultUnit(defaults FormDefaults) string

FormDefaultUnit returns the selected unit for a facet with Units.

func (Def) FormOptions

func (d Def) FormOptions() []string

FormOptions returns dropdown or checkbox choices for form widgets.

func (Def) FormatCompact

func (d Def) FormatCompact(v Value) string

FormatCompact renders a value for dense contexts (listing cards).

func (Def) FormatFull

func (d Def) FormatFull(v Value) string

FormatFull renders a value with full precision (ad detail page).

func (Def) LocationPlaceholder

func (d Def) LocationPlaceholder() string

LocationPlaceholder returns the form placeholder for a Location facet.

func (Def) NormalizeUnit

func (d Def) NormalizeUnit(unit string) string

NormalizeUnit returns unit if allowed, otherwise the first allowed unit.

func (Def) SelectBounds

func (d Def) SelectBounds() (min, max int)

SelectBounds returns inclusive min/max for FormSelect on Kind Int.

func (Def) SupportedCurrencies

func (d Def) SupportedCurrencies() []string

SupportedCurrencies returns currency dropdown options for money facets.

func (Def) ValidFilterValue

func (d Def) ValidFilterValue(val string) bool

ValidFilterValue reports whether val is an allowed search filter option.

func (Def) ValidUnit

func (d Def) ValidUnit(unit string) bool

ValidUnit reports whether unit is allowed for this facet.

func (Def) Validate

func (d Def) Validate(v Value) error

Validate checks a value against the definition. Optional facets may be absent; Required facets must be present and pass kind-specific rules.

func (Def) VectorMetadataValue

func (d Def) VectorMetadataValue(v Value) (any, bool)

VectorMetadataValue returns the JSONB value for search filters.

type Filter

type Filter struct {
	Min     *int     `json:"min,omitempty"`
	Max     *int     `json:"max,omitempty"`
	TextMin *string  `json:"text_min,omitempty"`
	TextMax *string  `json:"text_max,omitempty"`
	Value   *string  `json:"value,omitempty"`
	Values  []string `json:"values,omitempty"`
}

Filter is a search constraint. Min/Max apply to Int/Money; TextMin/TextMax to Date; Value to a single Enum; Values to multiple Enum selections (OR within the facet).

func ResolveFilterForSearch

func ResolveFilterForSearch(key string, f Filter, now time.Time) Filter

ResolveFilterForSearch converts stored filter presets into query constraints.

func (Filter) Active

func (f Filter) Active() bool

type FilterWidget

type FilterWidget int

FilterWidget controls the search filter panel for a facet.

const (
	FilterRange      FilterWidget = iota // min/max inputs
	FilterExact                          // single-select (Enum dropdown)
	FilterCheckboxes                     // multi-select (Enum checkboxes)
	FilterFlag                           // single opt-in checkbox (Flag)
)

type FormDefaults

type FormDefaults struct {
	Currency string // default selected currency for money facets
	Unit     string // preferred unit for facets with Units (e.g. mi/km)
}

FormDefaults holds per-request values for new-ad form widgets.

type FormWidget

type FormWidget int

FormWidget controls the new-ad form control for a facet.

const (
	FormNumber     FormWidget = iota // plain number input (+ unit select when Units set)
	FormSelect                       // <select>; options from Enum (Kind Enum) or SelectMin..SelectMax (Kind Int)
	FormRadio                        // exclusive choices for Kind Enum
	FormMoney                        // amount + currency + FREE
	FormDate                         // date input (Kind Date)
	FormCheckboxes                   // multi-select checkboxes (Kind MultiEnum)
	FormLocation                     // location text input (Kind Location)
	FormFlag                         // single opt-in checkbox (Kind Flag)
)

type Kind

type Kind int
const (
	Int       Kind = iota // numeric, range-filterable
	Money                 // amount + currency (Value.Text), FREE when amount == 0
	Enum                  // fixed string set, exact-match filter
	Date                  // ISO date YYYY-MM-DD in Value.Text
	MultiEnum             // multiple enum selections, JSON array in Value.Text
	Location              // user location text; resolves to ads.location_id
	Flag                  // optional opt-in; Num == 1 when set
)

type Value

type Value struct {
	Num    *int
	Text   *string
	Values []string
}

Value is a stored facet value for one ad. Num holds Int/Money amounts; Text holds currency (Money), unit (Int with Units), selection (Enum/Date), or a JSON array (MultiEnum). Values holds decoded MultiEnum selections in memory.

func EncodeFlag

func EncodeFlag(checked bool) Value

EncodeFlag builds a Flag value when checked.

func EncodeMultiEnum

func EncodeMultiEnum(selected []string) Value

EncodeMultiEnum builds a MultiEnum value from selected options.

func ParseDateValue

func ParseDateValue(raw string) (Value, error)

ParseDateValue parses an ISO date string into a Date facet value.

func (Value) DateString

func (v Value) DateString() string

DateString returns the ISO date from a Date facet value.

func (Value) FlagSet

func (v Value) FlagSet() bool

FlagSet reports whether a Flag facet value is checked.

func (Value) MultiEnumValues

func (v Value) MultiEnumValues() []string

MultiEnumValues decodes MultiEnum selections from memory or stored JSON.

func (Value) Present

func (v Value) Present() bool

Jump to

Keyboard shortcuts

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