package seatdef import ( "bytes" "encoding/json" "fmt" ) // 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 }