package seatdef import ( "bytes" "encoding/json" "errors" "fmt" "strings" ) // Callers says who may call a verb or send an accepted kind. It is read as text (the form every definition on main // has) or as a list (the form design 47 G §336 writes); the reader release carries both so that a definition in // either form is read (design 47 G §340), and G's own reader refuses the text. type Callers struct { // Text is the callers in words, when the definition gave them so. Text string // List is the callers as a list of entries, when the definition gave them so. List []string } // IsText says whether the definition gave its callers in words. func (c Callers) IsText() bool { return c.List == nil } // UnmarshalJSON reads a string or a list of strings. func (c *Callers) UnmarshalJSON(raw []byte) error { raw = bytes.TrimSpace(raw) switch { case len(raw) > 0 && raw[0] == '"': return json.Unmarshal(raw, &c.Text) case len(raw) > 0 && raw[0] == '[': c.List = []string{} return json.Unmarshal(raw, &c.List) } return errors.New("callers is a text or a list of entries") } // MarshalJSON writes the form the definition was read in. func (c Callers) MarshalJSON() ([]byte, error) { if c.IsText() { return json.Marshal(c.Text) } return json.Marshal(c.List) } func checkCallers(where string, c *Callers) Problems { if c == nil { return nil } if c.IsText() { if c.Text == "" { return Problems{{where, "is empty"}} } return nil } var ps Problems for i, e := range c.List { if why := CallerEntryProblem(e); why != "" { ps = append(ps, Problem{fmt.Sprintf("%s[%d]", where, i), why}) } } return ps } // The entries of a list of callers that are not a name (design 47 G §336). const ( CallerOperator = "operator" CallerHealers = "healers" CallerPersons = "persons" CallerSessions = "sessions" CallerInvokers = "invokers" ) // The prefixes of the entries that name a seat's holder or a module (design 47 G §392, §336). const ( CallerSeatPrefix = "seat:" CallerModulePrefix = "module:" CallerHolderPrefix = "holder:" CallerSameNode = "@same-node" ) // CallerEntryProblem says why an entry of a list of callers is none of the forms of design 47 G §336, §392; empty // when it is one: `operator`, `healers`, `persons`, `sessions`, `invokers`, `seat:`, `module:` and // `holder:@same-node`. A bare name is refused, since a name such as `mesh-grants` is both a seat and a module. func CallerEntryProblem(e string) string { switch e { case CallerOperator, CallerHealers, CallerPersons, CallerSessions, CallerInvokers: return "" } if n, ok := strings.CutPrefix(e, CallerSeatPrefix); ok && wordName.MatchString(n) { return "" } if n, ok := strings.CutPrefix(e, CallerModulePrefix); ok && wordName.MatchString(n) { return "" } if n, ok := strings.CutPrefix(e, CallerHolderPrefix); ok { if seat, ok := strings.CutSuffix(n, CallerSameNode); ok && wordName.MatchString(seat) { return "" } } return fmt.Sprintf("%q is none of operator, healers, persons, sessions, invokers, seat:, module: and "+ "holder:@same-node", e) } // DefaultCallers are the callers a verb that names none takes by its class (design 47 G §610, amending §391): a read takes // invokers, persons and sessions; an act, a warrant and an accepted kind (`accept`) the operator, sessions and invokers. A // verb that names no class is a read. func DefaultCallers(class string) []string { switch class { case ClassAct, ClassWarrant, "accept": return []string{CallerOperator, CallerSessions, CallerInvokers} } return []string{CallerInvokers, CallerPersons, CallerSessions} } // EffectiveCallers are the callers the verb is open to: the list it names, or the default of its class. A verb whose // callers are text, as the definitions before G wrote them, names no list and takes the default. func (v Verb) EffectiveCallers() []string { if v.Callers != nil && !v.Callers.IsText() { return v.Callers.List } return DefaultCallers(v.Class) } // The classes of a verb (design 47 G §391). const ( ClassRead = "read" ClassAct = "act" ClassWarrant = "warrant" ) // Classes are the classes a verb's `class` may name. var Classes = []string{ClassRead, ClassAct, ClassWarrant} // Keep is an event's `keep`: the string of today's definitions (KeepLastPerSubject or KeepStream, with the message's // max-age-days and max-mib beside it) or the object of design 47 G §396 (`max-age-days`, `max-mib`, // `last-per-subject`). The reader release reads both (design 47 G §340, §409). type Keep struct { // Mode is the string form; empty when the object form was given. Mode string // MaxAgeDays, MaxMiB and LastPerSubject are the object form's fields; MaxAgeDays and MaxMiB are nil when unsaid. MaxAgeDays *int MaxMiB *int LastPerSubject bool // Object says the object form was given. Object bool } type keepObject struct { MaxAgeDays *int `json:"max-age-days"` MaxMiB *int `json:"max-mib"` LastPerSubject bool `json:"last-per-subject"` } // UnmarshalJSON reads the string or the object, the object strictly. func (k *Keep) UnmarshalJSON(raw []byte) error { raw = bytes.TrimSpace(raw) if len(raw) > 0 && raw[0] == '"' { return json.Unmarshal(raw, &k.Mode) } var o keepObject dec := json.NewDecoder(bytes.NewReader(raw)) dec.DisallowUnknownFields() if err := dec.Decode(&o); err != nil { return fmt.Errorf("keep is a string or an object of max-age-days, max-mib and last-per-subject: %w", err) } *k = Keep{MaxAgeDays: o.MaxAgeDays, MaxMiB: o.MaxMiB, LastPerSubject: o.LastPerSubject, Object: true} return nil } // MarshalJSON writes the form the definition was read in. func (k Keep) MarshalJSON() ([]byte, error) { if !k.Object { return json.Marshal(k.Mode) } return json.Marshal(keepObject{k.MaxAgeDays, k.MaxMiB, k.LastPerSubject}) } func checkKeepObject(where string, k Keep) Problems { var ps Problems if k.MaxAgeDays != nil && *k.MaxAgeDays < 0 { ps = append(ps, Problem{where + ".max-age-days", "is negative (0 is no age limit)"}) } if k.MaxMiB != nil && *k.MaxMiB <= 0 { ps = append(ps, Problem{where + ".max-mib", "is not above zero"}) } return ps }