Coder Social home page Coder Social logo

uyaml's Introduction

uyaml

Package uyaml provides utilities for working with unstructured yaml documents. It allows structures to be queried, modified, and removed using a small query language that somewhat resembles CSS selectors (in some parallel dimension).

This package is considered of beta quality, and may behave oddly in some edge cases. In case this happens, please file an issue to the corresponding repository.

Querying Values

Let's suppose you have a extensive YAML document with a key nested under some complicated hierarchy. One option is to implement structures matching the document's, so yaml.Unmarshal can do its job. The other option is to unmarshal into a map of interfaces, or slice of interfaces. uyaml implements two methods to handle this scenario: DigItem and MustDigItem. For example, consider the following YAML document:

userCount: 2
users:
- name: josie
  roles:
  - bot
  - foo
  - bar
- name: lester
  roles:
  - dummy

In order to obtain roles for a user under the key josie, DigItem can be used:

doc, err := uyaml.Decode(...)
if err != nil {
    ...
}
ok, item, err := doc.DigItem("users.(name='josie').roles")
if err != nil {
	...
}
if !ok {
	...
}

str, ok := item.String()
if ok {
    fmt.Printf("%#v", item.)
}

MustDigItem works just like DigItem, except it only have a single return value, and panics in case the item cannot be found or the provided path cannot be parsed.

Removing Values

Removing values can be done with the Remove method, which takes a single path to be removed. In case the path does not exist, a noop happens and the same object is returned.

doc, err := uyaml.Decode(...)
if err != nil {
...
}
doc, err := doc.Remove("users.(name='josie')")

Setting Values

Set can be used to inject arbitrary values into the document's structure. For instance (errors checking omitted for brevity):

	val, err := data.Set("users.(name='dummy').test", true)
	yam, err := val.Encode()
	fmt.Println(yam)

Would print the following structure:

   userCount: 2
   users:
   - name: josie
     roles:
     - bot
     - foo
     - bar
   - name: lester
     roles:
     - dummy
   - name: dummy
     test: true

License

MIT License

Copyright (c) 2021 Victor Gama de Oliveira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

uyaml's People

Contributors

heyvito avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.