ParseCommentFlags processes the comments provided with queries to determine the metadata params, flags and rules to skip. All flags in query comments are prefixed with `@`, e.g. @param, @@sqlc-vet-disable.
(comments []string)
| 121 | // ParseCommentFlags processes the comments provided with queries to determine the metadata params, flags and rules to skip. |
| 122 | // All flags in query comments are prefixed with `@`, e.g. @param, @@sqlc-vet-disable. |
| 123 | func ParseCommentFlags(comments []string) (map[string]string, map[string]bool, map[string]struct{}, error) { |
| 124 | params := make(map[string]string) |
| 125 | flags := make(map[string]bool) |
| 126 | ruleSkiplist := make(map[string]struct{}) |
| 127 | |
| 128 | for _, line := range comments { |
| 129 | s := bufio.NewScanner(strings.NewReader(line)) |
| 130 | s.Split(bufio.ScanWords) |
| 131 | |
| 132 | s.Scan() |
| 133 | token := s.Text() |
| 134 | |
| 135 | if !strings.HasPrefix(token, "@") { |
| 136 | continue |
| 137 | } |
| 138 | |
| 139 | switch token { |
| 140 | case constants.QueryFlagParam: |
| 141 | s.Scan() |
| 142 | name := s.Text() |
| 143 | var rest []string |
| 144 | for s.Scan() { |
| 145 | paramToken := s.Text() |
| 146 | rest = append(rest, paramToken) |
| 147 | } |
| 148 | params[name] = strings.Join(rest, " ") |
| 149 | |
| 150 | case constants.QueryFlagSqlcVetDisable: |
| 151 | flags[token] = true |
| 152 | |
| 153 | // Vet rules can all be disabled in the same line or split across lines .i.e. |
| 154 | // /* @sqlc-vet-disable sqlc/db-prepare delete-without-where */ |
| 155 | // is equivalent to: |
| 156 | // /* @sqlc-vet-disable sqlc/db-prepare */ |
| 157 | // /* @sqlc-vet-disable delete-without-where */ |
| 158 | for s.Scan() { |
| 159 | ruleSkiplist[s.Text()] = struct{}{} |
| 160 | } |
| 161 | |
| 162 | default: |
| 163 | flags[token] = true |
| 164 | } |
| 165 | |
| 166 | if s.Err() != nil { |
| 167 | return params, flags, ruleSkiplist, s.Err() |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return params, flags, ruleSkiplist, nil |
| 172 | } |