https://github.com/kompitech/rmap
Rmap is better map[string]interface{}
https://github.com/kompitech/rmap
Last synced: 6 months ago
JSON representation
Rmap is better map[string]interface{}
- Host: GitHub
- URL: https://github.com/kompitech/rmap
- Owner: KompiTech
- Created: 2019-10-18T10:59:05.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-17T11:53:05.000Z (almost 3 years ago)
- Last Synced: 2025-04-02T06:35:32.599Z (10 months ago)
- Language: Go
- Size: 90.8 KB
- Stars: 3
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Rmap (recursive map) is wrapper around Go type ```map[string]interface{}``` which usually represents JSON data. Compared to pure Go, error handling code is greatly reduced.
All relevant methods also have Must variant which doesn't return error and immediately panics.
# Getters
## Get{Type}
Gets key with given type. Error is returned if key does not exist, or the type is invalid.
Example:
```
m := map[string]interface{}{
"stringValue": "hello world",
"intValue": 42,
}
r := rmap.NewFromMap(m)
val, err := r.GetString("stringValue")
// val is "hello world"
// err is nil
val2, err := r.GetString("intValue")
// val2 is ""
// err string is: intValue is not of type: STRING in object: {"intValue":42,"stringValue":"hello world"}, but: int
```
## GetJPtr{Type}
Get key with given type by using JSONPointer selector. This is very useful when working with nested objects. Error is returned if key does not exist, type is invalid or JSONPointer is not correct.
Example:
```
m := map[string]interface{}{
"nestedArray": []interface{}{
map[string]interface{}{
"nestedObj": map[string]interface{}{
"stringValue": "hello world",
},
},
},
}
r := NewFromMap(m)
val, err := r.GetJPtrString("/nestedArray/0/nestedObj/stringValue")
// val is "hello world"
// err is nil
val, err := r.GetJPtrString("/nestedArray/1/nestedObj/stringValue")
// val is ""
// err string is : r.GetJPtr() failed: ptr.Get() failed: Out of bound array[0,1] index '1'
```
## Get(JPtr)Iterable
Gets key that must be iterable. Error is returned if key does not exist or value is not iterable.
Example:
```
m := map[string]interface{}{
"array": []interface{}{
map[string]interface{}{
"position": "first",
},
map[string]interface{}{
"position": "second",
},
},
}
r := NewFromMap(m)
val, err := r.GetIterable("array")
// val is []interface{}
// err is nil
for _, objI := range val {
obj, err := NewFromInterface(objI)
// obj is next array element in every iteration
// err is nil
}
```
## And many more (check the code!)
# Constructors
JSON content is assumed in String/Bytes, unless YAML is specified. YAML must be able to be converted to JSON.
- NewEmpty
- NewFromInterface
- NewFromMap
- NewFromBytes
- NewFromString
- NewFromReader
- NewFromYAMLBytes
- NewFromYAMLFile