-
Notifications
You must be signed in to change notification settings - Fork 0
/
modify.go
48 lines (41 loc) · 1.02 KB
/
modify.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
package caddyslack
import (
"bytes"
"io"
"github.com/Jeffail/gabs"
)
func deleteJSONFromReader(readerIn io.Reader, pointsToDelete []string) (readerOut io.Reader, err error) {
if len(pointsToDelete) <= 0 {
return readerIn, nil
}
jsonParsed, err := gabs.ParseJSONBuffer(readerIn)
if err != nil {
return readerOut, err
}
for _, pointToDelete := range pointsToDelete {
err := jsonParsed.DeleteP(pointToDelete)
if err != nil {
return nil, err
}
}
return bytes.NewReader(jsonParsed.Bytes()), nil
}
func onlyJSONFromReader(readerIn io.Reader, pointsOnly []string) (readerOut io.Reader, err error) {
if pointsOnly == nil {
return readerIn, nil
}
jsonParsed, err := gabs.ParseJSONBuffer(readerIn)
if err != nil {
return readerOut, err
}
outContainer := gabs.New()
for _, point := range pointsOnly {
if jsonParsed.ExistsP(point) {
_, err = outContainer.SetP(jsonParsed.Path(point).Data(), point)
if err != nil {
return readerOut, err
}
}
}
return bytes.NewReader(outContainer.Bytes()), nil
}