AddDef adds an assertion to the model.
(sec string, key string, value string)
| 68 | |
| 69 | // AddDef adds an assertion to the model. |
| 70 | func (model Model) AddDef(sec string, key string, value string) bool { |
| 71 | if value == "" { |
| 72 | return false |
| 73 | } |
| 74 | |
| 75 | ast := Assertion{} |
| 76 | ast.Key = key |
| 77 | ast.Value = value |
| 78 | ast.PolicyMap = make(map[string]int) |
| 79 | ast.FieldIndexMap = make(map[string]int) |
| 80 | |
| 81 | if sec == "r" || sec == "p" { |
| 82 | ast.Tokens = strings.Split(ast.Value, ",") |
| 83 | for i := range ast.Tokens { |
| 84 | ast.Tokens[i] = key + "_" + strings.TrimSpace(ast.Tokens[i]) |
| 85 | } |
| 86 | } else if sec == "g" { |
| 87 | ast.ParamsTokens = getParamsToken(ast.Value) |
| 88 | ast.Tokens = strings.Split(ast.Value, ",") |
| 89 | ast.Tokens = ast.Tokens[:len(ast.Tokens)-len(ast.ParamsTokens)] |
| 90 | } else { |
| 91 | ast.Value = util.RemoveComments(util.EscapeAssertion(ast.Value)) |
| 92 | } |
| 93 | |
| 94 | if sec == "m" { |
| 95 | // Escape backslashes in string literals to match CSV parsing behavior |
| 96 | ast.Value = util.EscapeStringLiterals(ast.Value) |
| 97 | |
| 98 | if strings.Contains(ast.Value, "in") { |
| 99 | ast.Value = strings.Replace(strings.Replace(ast.Value, "[", "(", -1), "]", ")", -1) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | _, ok := model[sec] |
| 104 | if !ok { |
| 105 | model[sec] = make(AssertionMap) |
| 106 | } |
| 107 | |
| 108 | model[sec][key] = &ast |
| 109 | return true |
| 110 | } |
| 111 | |
| 112 | func getKeySuffix(i int) string { |
| 113 | if i == 1 { |