Comments (6)
Hey @s3rj1k, I'd need some more details about that, but, skimming through this article, it sounds like you want to create a gabs container from a weakly-decoded JSON. Is that correct? If yes, try this:
package main
import (
"fmt"
"github.com/Jeffail/gabs/v2"
)
func main() {
newEnvs := []interface{}{
map[string]interface{}{
"name": "NAMESPACE",
"valueFrom": map[string]interface{}{
"fieldRef": map[string]interface{}{
"fieldPath": "metadata.namespace",
},
},
},
map[string]interface{}{
"name": "POD_UID",
"valueFrom": map[string]interface{}{
"fieldRef": map[string]interface{}{
"fieldPath": "metadata.uid",
},
},
},
}
obj := gabs.Wrap(newEnvs)
fmt.Println(obj.Path("*.valueFrom.fieldRef.fieldPath").String())
}
from gabs.
@mihaitodor hey, thanks for quick response, I just want a helper to convert gabs <-> unstructured
Basically unstructured has embedded Object map[string]interface{}
where gabs container has interface{}
.
The flow would be something like this:
- unstructured -> gabs.container
- some gabs magic manipulating data
- gabs.container -> unstructured
(hopefully with minimal memory allocation)
from gabs.
@s3rj1k Sure, have you tried the .Data()
method?
package main
import (
"fmt"
"github.com/Jeffail/gabs/v2"
"github.com/davecgh/go-spew/spew"
)
func main() {
newEnvs := []interface{}{
map[string]interface{}{
"name": "NAMESPACE",
"valueFrom": map[string]interface{}{
"fieldRef": map[string]interface{}{
"fieldPath": "metadata.namespace",
},
},
},
map[string]interface{}{
"name": "POD_UID",
"valueFrom": map[string]interface{}{
"fieldRef": map[string]interface{}{
"fieldPath": "metadata.uid",
},
},
},
}
obj := gabs.Wrap(newEnvs)
fmt.Println(obj.Path("*.valueFrom.fieldRef.fieldPath").String())
spew.Dump(obj.Data())
obj.SetP(666, "0.valueFrom.fieldRef.fieldPath")
spew.Dump(obj.Data())
}
PS: You should be able to use type assertions to get the actual type from the interface{}
returned by Data()
(in the example above, that would be obj.Data().([]interface{})
).
from gabs.
Flow is a bit different, reversed
package main
import (
"log"
"github.com/Jeffail/gabs/v2"
"github.com/davecgh/go-spew/spew"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
)
func main() {
b := []byte(`
apiVersion: v1
kind: Pod
metadata:
name: my-pod
namespace: default
spec:
containers:
- name: nginx
image: nginx:latest
`)
obj := new(unstructured.Unstructured)
err := yaml.Unmarshal(b, obj)
if err != nil {
log.Fatalf("Failed to unmarshal YAML: %v", err)
}
container := gabs.Wrap(obj.Object)
objNew := new(unstructured.Unstructured)
objNew.SetUnstructuredContent(container.Data().(map[string]interface{}))
spew.Dump(obj.Object)
spew.Dump(objNew.Object)
}
so what I am after here is basically
container.Data().(map[string]interface{})
a more streamlined version of this ^^^
from gabs.
Sure, but that's specific to your use case. Nothing wrong with publishing your own gabs wrapper which exposes a method that returns container.Data().(map[string]interface{})
, even if it's just some boilerplate.
from gabs.
Sure, but that's specific to your use case. Nothing wrong with publishing your own gabs wrapper which exposes a method that returns
container.Data().(map[string]interface{})
, even if it's just some boilerplate.
Yea, I was hoping for method similar to Data
but that will return map[string]interface{}
by type casting it internally and panic when error occurs.
from gabs.
Related Issues (20)
- Iterate over json root keys HOT 2
- Not getting value from key HOT 2
- Generate Array of Struct HOT 2
- add new json item not sync in memory HOT 2
- Selecting only inner fields HOT 1
- gabs.Container.Path() returns null, even the value exists HOT 2
- Container.Set does not take effect on parent node if param "hierarchy" is empty HOT 5
- Merge fails on "pure" arrays HOT 2
- Iterate through every key, value; even inner childs. Get full path of a key HOT 5
- Is there any way we can find the key in an object in a JSON file HOT 2
- Finding whether the keys of the input json is a subset of a array of string values. HOT 1
- How to create a JSON array HOT 3
- Path/Search does not work with arrays HOT 2
- How to create additional keys and arrays HOT 1
- how to Preserving key order by json parse ? HOT 1
- to byte[] diff error? HOT 4
- Lists not working? HOT 1
- Feature request: use -1 index to get last item in array.
- Inconsistency Between .Wrap() and .New() Containers Causes Delete() to fail
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gabs.