package chain import ( "fmt" "strconv" jlexer "github.com/mailru/easyjson/jlexer" jwriter "github.com/mailru/easyjson/jwriter" ) // Run `make generate`` if types added or changed var matchTypeToJSONValue = []struct { mt MatchType str string }{ {MatchTypeDenyPriority, "DenyPriority"}, {MatchTypeFirstMatch, "FirstMatch"}, } var statusToJSONValue = []struct { s Status str string }{ {Allow, "Allow"}, {NoRuleFound, "NoRuleFound"}, {AccessDenied, "AccessDenied"}, {QuotaLimitReached, "QuotaLimitReached"}, } var objectTypeToJSONValue = []struct { t ObjectType str string }{ {ObjectRequest, "Request"}, {ObjectResource, "Resource"}, } func (mt MatchType) MarshalEasyJSON(w *jwriter.Writer) { for _, p := range matchTypeToJSONValue { if p.mt == mt { w.String(p.str) return } } w.String(strconv.FormatUint(uint64(mt), 10)) } func (mt *MatchType) UnmarshalEasyJSON(l *jlexer.Lexer) { str := l.String() for _, p := range matchTypeToJSONValue { if p.str == str { *mt = p.mt return } } v, err := strconv.ParseUint(str, 10, 8) if err != nil { l.AddError(fmt.Errorf("failed to parse match type: %w", err)) return } *mt = MatchType(v) } func (st Status) MarshalEasyJSON(w *jwriter.Writer) { for _, p := range statusToJSONValue { if p.s == st { w.String(p.str) return } } w.String(strconv.FormatUint(uint64(st), 10)) } func (st *Status) UnmarshalEasyJSON(l *jlexer.Lexer) { str := l.String() for _, p := range statusToJSONValue { if p.str == str { *st = p.s return } } v, err := strconv.ParseUint(str, 10, 8) if err != nil { l.AddError(fmt.Errorf("failed to parse status: %w", err)) return } *st = Status(v) } func (ot ObjectType) MarshalEasyJSON(w *jwriter.Writer) { for _, p := range objectTypeToJSONValue { if p.t == ot { w.String(p.str) return } } w.String(strconv.FormatUint(uint64(ot), 10)) } func (ot *ObjectType) UnmarshalEasyJSON(l *jlexer.Lexer) { str := l.String() for _, p := range objectTypeToJSONValue { if p.str == str { *ot = p.t return } } v, err := strconv.ParseUint(str, 10, 8) if err != nil { l.AddError(fmt.Errorf("failed to parse object type: %w", err)) return } *ot = ObjectType(v) } func (ct ConditionType) MarshalEasyJSON(w *jwriter.Writer) { for _, p := range condToStr { if p.ct == ct { w.String(p.str) return } } w.String(strconv.FormatUint(uint64(ct), 10)) } func (ct *ConditionType) UnmarshalEasyJSON(l *jlexer.Lexer) { str := l.String() for _, p := range condToStr { if p.str == str { *ct = p.ct return } } v, err := strconv.ParseUint(str, 10, 8) if err != nil { l.AddError(fmt.Errorf("failed to parse condition type: %w", err)) return } *ct = ConditionType(v) }