-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feature): use tidwall/gjson for json parsing (#25)
* (feature): use tidwall/gjson for json parsing Signed-off-by: Bryce Palmer <everettraven@gmail.com> * add snippet to readme Signed-off-by: Bryce Palmer <everettraven@gmail.com> --------- Signed-off-by: Bryce Palmer <everettraven@gmail.com>
- Loading branch information
1 parent
1e1ea17
commit 512d6d9
Showing
6 changed files
with
32 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
package paneler | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
func getDotNotationValue(item map[string]interface{}, dotPath string) (interface{}, error) { | ||
keys := strings.Split(dotPath, ".") | ||
val, exist, err := unstructured.NestedFieldNoCopy(item, keys...) | ||
jsonBytes, err := json.Marshal(item) | ||
if err != nil { | ||
return nil, fmt.Errorf("error fetching nested field %q: %w", dotPath, err) | ||
return nil, fmt.Errorf("error marshalling item to json: %w", err) | ||
} | ||
if !exist { | ||
res := gjson.Get(string(jsonBytes), dotPath) | ||
if !res.Exists() { | ||
return nil, fmt.Errorf("nested field %q not found", dotPath) | ||
} | ||
return val, nil | ||
return res.Value(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters