Usage from Go

Introduction

The dasel CLI is a basic package that uses the dasel external API to query and modify data.

Your application can do the same.

External API

Ensure you have imported the appropriate parsing packages. See parsing.

Dasel exposes a simple API that can be consumed by your go packages/modules.

There are three main funcs of interest:

  • Select - Query some data and receive the results in Go types.

  • Query - Query some data and receive the results in Dasel model.Value types.

    • model.Value is more verbose to work with, but maintains ordering and retains metadata.

    • See the model package for implementation details.

  • Modify - Perform a query that will modify the given data in-place.

Read api.go for more details information.

Processing byte data

Dasel queries generally expect model.Value input data. These can be constructed manually by you, or you can use dasel parsers to read structured data from JSON, YAML, etc.

Or you can use an in-memory value:

It's worth noting that any data passed to the external API are converted to a model.Value internally using model.NewValue.

You can convert dasel values back to bytes with parsing.Format("json").NewWriter or back to a standard go data type with myValue.GoValue().

Parsing formats

Please note that you will have to import any dasel parser that you wish to use in your application. The Dasel CLI does this in cmd/dasel/main.go. E.g.

Dasel project structure

Dasel has the following main packages:

  • dasel - The external API.

  • model - A wrapper around reflection types. This is what Dasel uses to access and modify data internally.

  • parsing - Parsing implementations for each of the supported languages (e.g. JSON). Each subdirectory contains a read and writer implementation.

  • execution - The real code implementation of all dasel features.

  • selector - Parse dasel queries and returns an AST which can be used by the execution package.

Examples

Up to date examples are maintained within the GitHub repository under api_example_test.go.

Selecting basic data from an in-memory map

Last updated