2024-01-17 14:49:03 +00:00
|
|
|
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 {
|
2024-05-13 10:54:37 +00:00
|
|
|
t ConditionKindType
|
2024-01-17 14:49:03 +00:00
|
|
|
str string
|
|
|
|
}{
|
2024-05-13 10:54:37 +00:00
|
|
|
{KindRequest, "Request"},
|
|
|
|
{KindResource, "Resource"},
|
2024-01-17 14:49:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-05-13 10:54:37 +00:00
|
|
|
func (ot ConditionKindType) MarshalEasyJSON(w *jwriter.Writer) {
|
2024-01-17 14:49:03 +00:00
|
|
|
for _, p := range objectTypeToJSONValue {
|
|
|
|
if p.t == ot {
|
|
|
|
w.String(p.str)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.String(strconv.FormatUint(uint64(ot), 10))
|
|
|
|
}
|
|
|
|
|
2024-05-13 10:54:37 +00:00
|
|
|
func (ot *ConditionKindType) UnmarshalEasyJSON(l *jlexer.Lexer) {
|
2024-01-17 14:49:03 +00:00
|
|
|
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
|
|
|
|
}
|
2024-05-13 10:54:37 +00:00
|
|
|
*ot = ConditionKindType(v)
|
2024-01-17 14:49:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|