Skip to content

Commit 2ada750

Browse files
Fix: processing JSON path (#289)
Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
1 parent ad842f2 commit 2ada750

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

plugin/plaintext/common_in.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,37 @@ func (t *TextIn) scanFileForJSONIn(reader io.Reader, entry *lib.Entry) error {
209209
path = strings.TrimSpace(path)
210210

211211
result := gjson.GetBytes(data, path)
212-
for _, cidr := range result.Array() {
213-
if err := entry.AddPrefix(cidr.String()); err != nil {
212+
if err := t.processJSONResult(result, entry); err != nil {
213+
return fmt.Errorf("❌ [type %s | action %s] failed to process JSON: %v", t.Type, t.Action, err)
214+
}
215+
}
216+
217+
return nil
218+
}
219+
220+
func (t *TextIn) processJSONResult(result gjson.Result, entry *lib.Entry) error {
221+
switch {
222+
case !result.Exists():
223+
return fmt.Errorf("invaild IP address or CIDR (value not exist), please check your specified JSON path or JSON source")
224+
225+
case result.Type == gjson.String:
226+
cidr := strings.TrimSpace(result.String())
227+
if cidr == "" {
228+
return fmt.Errorf("empty string, please check your specified JSON path or JSON source")
229+
}
230+
if err := entry.AddPrefix(cidr); err != nil {
231+
return err
232+
}
233+
234+
case result.IsArray():
235+
for _, item := range result.Array() {
236+
if err := t.processJSONResult(item, entry); err != nil {
214237
return err
215238
}
216239
}
240+
241+
default:
242+
return fmt.Errorf("invaild IP address or CIDR, please check your specified JSON path or JSON source")
217243
}
218244

219245
return nil

0 commit comments

Comments
 (0)