From 2c89ce4d42af0cf2c3728414fd15a148e16c4930 Mon Sep 17 00:00:00 2001 From: Alex Dunmow Date: Sat, 2 May 2026 11:03:09 +0800 Subject: [PATCH] feat: add BadgeRefresher interface and badge types --- badges/badges.go | 42 ++++++++++++++++++++++++++++++++++++++++++ plugin/deps.go | 8 ++++++++ 2 files changed, 50 insertions(+) create mode 100644 badges/badges.go diff --git a/badges/badges.go b/badges/badges.go new file mode 100644 index 0000000..fced045 --- /dev/null +++ b/badges/badges.go @@ -0,0 +1,42 @@ +package badges + +import "encoding/json" + +// BadgeRule defines a condition for automatic badge assignment. +type BadgeRule struct { + Field string `json:"field"` + Operator string `json:"operator"` // eq, neq, gt, gte, lt, lte, contains, exists, true, false + Value any `json:"value"` +} + +// BadgeDefinition defines a badge with auto-computation rules. +type BadgeDefinition struct { + Key string `json:"key"` + Label string `json:"label"` + Description string `json:"description"` + Icon string `json:"icon"` + Rules []BadgeRule `json:"rules"` + RulesMode string `json:"rules_mode"` // "and" or "or" + ManualOnly bool `json:"manual_only"` +} + +// ParseDefinitions extracts badge definitions from a data table schema JSON. +func ParseDefinitions(schemaJSON []byte) []BadgeDefinition { + var schema map[string]any + if err := json.Unmarshal(schemaJSON, &schema); err != nil { + return nil + } + badgesRaw, ok := schema["badges"] + if !ok { + return nil + } + raw, err := json.Marshal(badgesRaw) + if err != nil { + return nil + } + var definitions []BadgeDefinition + if err := json.Unmarshal(raw, &definitions); err != nil { + return nil + } + return definitions +} diff --git a/plugin/deps.go b/plugin/deps.go index 20d8719..7d190b7 100644 --- a/plugin/deps.go +++ b/plugin/deps.go @@ -49,6 +49,7 @@ type ServiceDeps struct { // Core RPC services — pre-built bindings for CMS-provided services CoreServiceBindings CoreServiceBindings ReviewSubmitter ReviewSubmitter + BadgeRefresher BadgeRefresher // Extension points — typed as narrow interfaces where possible JobRunner JobRunner @@ -85,3 +86,10 @@ type RAGResult struct { Score float64 Metadata map[string]string } + +// BadgeRefresher recomputes badges for a data table row. +// The CMS handles loading the table schema, aggregating ratings, +// evaluating badge rules, and persisting the updated badge list. +type BadgeRefresher interface { + RefreshBadges(ctx context.Context, tableID, rowID uuid.UUID) error +}