Skip to content

Commit

Permalink
(feature): use tidwall/gjson for json parsing (#25)
Browse files Browse the repository at this point in the history
* (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
everettraven committed Nov 15, 2023
1 parent 1e1ea17 commit 512d6d9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 42 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ kind create cluster

The `test.json` file contains samples for each of the different panel types that `buoy` currently supports. As this is a hobby project very early in the development cycle there are some limitations and things are bound to not work as expected.

`buoy` uses https://github.com/tidwall/gjson for the path evaluation and extracting of values from resources. Please consult their documentation for valid path syntax

## Controls
- `ctrl+c`, `q`, `esc` will quit the program and exit the tui
- `tab` will switch the active tab to the one to the right of the currently active tab
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ require (
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/lipgloss v0.7.1
github.com/spf13/cobra v1.7.0
github.com/tidwall/gjson v1.17.0
k8s.io/api v0.28.1
k8s.io/apimachinery v0.28.1
k8s.io/client-go v0.28.1
sigs.k8s.io/controller-runtime v0.16.2
sigs.k8s.io/yaml v1.3.0
)

require github.com/dlclark/regexp2 v1.4.0 // indirect
require (
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)

require (
github.com/alecthomas/chroma v0.10.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down
14 changes: 7 additions & 7 deletions pkg/paneler/helpers.go
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
}
29 changes: 3 additions & 26 deletions pkg/paneler/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,8 @@ func (t *Table) runInformerForTable(tablePanel buoytypes.Table, tw *panels.Table
tw.SetError(err)
break
}
switch val := val.(type) {
case string:
row = append(row, val)
case map[string]interface{}:
data, err := json.Marshal(val)
if err != nil {
tw.SetError(err)
break
}
row = append(row, string(data))
default:
row = append(row, fmt.Sprint(val))
}

row = append(row, fmt.Sprint(val))
}

tw.AddOrUpdateRow(u.GetUID(), row)
Expand All @@ -122,19 +111,7 @@ func (t *Table) runInformerForTable(tablePanel buoytypes.Table, tw *panels.Table
tw.SetError(err)
break
}
switch val := val.(type) {
case string:
row = append(row, val)
case map[string]interface{}:
data, err := json.Marshal(val)
if err != nil {
tw.SetError(err)
break
}
row = append(row, string(data))
default:
row = append(row, fmt.Sprint(val))
}
row = append(row, fmt.Sprint(val))
}

tw.AddOrUpdateRow(u.GetUID(), row)
Expand Down
16 changes: 8 additions & 8 deletions test.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
"path": "metadata.name"
},
{
"header": "Phase",
"path": "status.phase"
"header": "Labels",
"path": "metadata.labels"
},
{
"header": "PodIP",
"path": "status.podIP"
"header": "Ready Condition",
"path": "status.conditions.#(type==Ready)"
},
{
"header": "Start Time",
"path": "status.startTime"
"header": "Phase",
"path": "status.phase"
},
{
"header": "UID",
"path": "metadata.uid"
"header": "Containers",
"path": "spec.containers.#.name"
}
]
},
Expand Down

0 comments on commit 512d6d9

Please sign in to comment.