Product
Introducing Ruby Support in Socket
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
github.com/icza/dyno
Package dyno is a utility to work with dynamic objects at ease.
Primary goal is to easily handle dynamic objects and arrays (and a mixture of these)
that are the result of unmarshaling a JSON or YAML text into an interface{}
for example. When unmarshaling into interface{}
, libraries usually choose
either map[string]interface{}
or map[interface{}]interface{}
to represent objects,
and []interface{}
to represent arrays. Package dyno supports a mixture of
these in any depth and combination.
When operating on a dynamic object, you designate a value you're interested
in by specifying a path. A path is a navigation; it is a series of map keys
and int
slice indices that tells how to get to the value.
Should you need to marshal a dynamic object to JSON which contains maps with
interface{}
key type (which is not supported by encoding/json
), you may use
the ConvertMapI2MapS
converter function.
The implementation does not use reflection at all, so performance is rather good.
Get a (typed) value denoted by a path: Get, GetInt, GetString, GetSlice, GetMapI, GetMapS
Get a typed value with built-in conversion / parsing: GetInteger, GetFloating, GetBoolean
Specialized get for maps with string
keys: SGet
Set a value denoted by a path: Set
Specialized set for maps with string
keys: SSet
Append value(s) to a slice denoted by a path: Append, AppendMore
Delete a key from a map or an element from a slice denoted by a path: Delete
Convert maps with interface{}
keys to maps with string
keys: ConvertMapI2MapS
Let's see a simple example editing a JSON text to mask out a password. This is
a simplified version of the Example_jsonEdit
example function:
src := `{"login":{"password":"secret","user":"bob"},"name":"cmpA"}`
var v interface{}
if err := json.Unmarshal([]byte(src), &v); err != nil {
panic(err)
}
// Edit (mask out) password:
if err = dyno.Set(v, "xxx", "login", "password"); err != nil {
fmt.Printf("Failed to set password: %v\n", err)
}
edited, err := json.Marshal(v)
fmt.Printf("Edited JSON: %s, error: %v\n", edited, err)
Output will be:
Edited JSON: {"login":{"password":"xxx","user":"bob"},"name":"cmpA"}, error: <nil>
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.