ParseKeysFromText parses a string of keys from various formats into a string slice. This function is exported to be shared with the handler layer.
(text string)
| 160 | // ParseKeysFromText parses a string of keys from various formats into a string slice. |
| 161 | // This function is exported to be shared with the handler layer. |
| 162 | func (s *KeyService) ParseKeysFromText(text string) []string { |
| 163 | var keys []string |
| 164 | |
| 165 | // First, try to parse as a JSON array of strings |
| 166 | if json.Unmarshal([]byte(text), &keys) == nil && len(keys) > 0 { |
| 167 | return s.filterValidKeys(keys) |
| 168 | } |
| 169 | |
| 170 | // 通用解析:通过分隔符分割文本,不使用复杂的正则表达式 |
| 171 | delimiters := regexp.MustCompile(`[\s,;\n\r\t]+`) |
| 172 | splitKeys := delimiters.Split(strings.TrimSpace(text), -1) |
| 173 | |
| 174 | for _, key := range splitKeys { |
| 175 | key = strings.TrimSpace(key) |
| 176 | if key != "" { |
| 177 | keys = append(keys, key) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return s.filterValidKeys(keys) |
| 182 | } |
| 183 | |
| 184 | // filterValidKeys validates and filters potential API keys |
| 185 | func (s *KeyService) filterValidKeys(keys []string) []string { |
no test coverage detected