43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
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
|
|
}
|