-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchreplacefuncs.go
67 lines (55 loc) · 1.63 KB
/
searchreplacefuncs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2021-2025 Dell Inc. or its subsidiaries. All rights reserved.
// Licensed to You under the Apache License, Version 2.0. See the LICENSE file for more details.
package jsonstream
import (
"sync"
)
type dropperType int
const (
dropperTypeFn dropperType = iota
dropperTypePropertyPathEqual
)
// Dropper is a SearchReplacer that searches for things and doesnt replace them with anything
type Dropper struct {
dType dropperType
searchFunc func(JSONPath) JSONPath
prefix JSONPath
}
//nolint:gochecknoglobals
var dropperPool = sync.Pool{
New: func() interface{} {
return &Dropper{}
},
}
func DropPropertyByMatchFunc(drop func(JSONPath) JSONPath) func(TokenIterator) TokenIterator {
dropper := dropperPool.Get().(*Dropper)
dropper.searchFunc = drop
dropper.dType = dropperTypeFn
return SearchReplace(dropper, true)
}
func DropPropertyByPathMatch(prefix JSONPath, iter TokenIterator) TokenIterator {
dropper := dropperPool.Get().(*Dropper)
dropper.dType = dropperTypePropertyPathEqual
dropper.prefix = prefix
return SearchReplaceWithIterator(dropper, true, iter)
}
func (bm *Dropper) Search(p JSONPath, _ []byte) JSONPath {
switch bm.dType {
case dropperTypePropertyPathEqual:
return bm.propertyPathEqual(p)
default:
return bm.searchFunc(p)
}
}
func (bm *Dropper) propertyPathEqual(tokPath JSONPath) JSONPath {
if ret := MatchPathPrefix(tokPath, bm.prefix); len(ret) == len(tokPath) {
return ret
}
return nil
}
func (bm *Dropper) Save(JSONPath, []byte, int) {}
func (bm *Dropper) GetReplaceIter() TokenIterator { return nil }
func (bm *Dropper) Close() error {
defer dropperPool.Put(bm)
return nil
}