qp

package
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2022 License: MIT Imports: 2 Imported by: 24

Documentation

Overview

qp helps to quickly build IPLD nodes.

It contains top-level Build funcs, such as BuildMap and BuildList, which return the final node as well as an error.

Underneath, one can use a number of Assemble functions to construct basic nodes, such as String or Int.

Finally, functions like MapEntry and ListEntry allow inserting into maps and lists.

These all use the same IPLD datamodel interfaces such as NodePrototype and NodeAssembler, but with some magic to reduce verbosity.

Example
package main

import (
	"os"

	"github.com/ipld/go-ipld-prime/codec/dagjson"
	"github.com/ipld/go-ipld-prime/datamodel"
	"github.com/ipld/go-ipld-prime/fluent/qp"
	"github.com/ipld/go-ipld-prime/node/basicnode"
)

func main() {
	n, err := qp.BuildMap(basicnode.Prototype.Any, 4, func(ma datamodel.MapAssembler) {
		qp.MapEntry(ma, "some key", qp.String("some value"))
		qp.MapEntry(ma, "another key", qp.String("another value"))
		qp.MapEntry(ma, "nested map", qp.Map(2, func(ma datamodel.MapAssembler) {
			qp.MapEntry(ma, "deeper entries", qp.String("deeper values"))
			qp.MapEntry(ma, "more deeper entries", qp.String("more deeper values"))
		}))
		qp.MapEntry(ma, "nested list", qp.List(2, func(la datamodel.ListAssembler) {
			qp.ListEntry(la, qp.Int(1))
			qp.ListEntry(la, qp.Int(2))
		}))
	})
	if err != nil {
		panic(err)
	}
	dagjson.Encode(n, os.Stdout)

}
Output:

{"another key":"another value","nested list":[1,2],"nested map":{"deeper entries":"deeper values","more deeper entries":"more deeper values"},"some key":"some value"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildList

func BuildList(np datamodel.NodePrototype, sizeHint int64, fn func(datamodel.ListAssembler)) (_ datamodel.Node, err error)

func BuildMap

func BuildMap(np datamodel.NodePrototype, sizeHint int64, fn func(datamodel.MapAssembler)) (_ datamodel.Node, err error)

func ListEntry

func ListEntry(la datamodel.ListAssembler, fn Assemble)

func MapEntry

func MapEntry(ma datamodel.MapAssembler, k string, fn Assemble)

Types

type Assemble

type Assemble = func(datamodel.NodeAssembler)

func Bool

func Bool(b bool) Assemble

func Bytes

func Bytes(p []byte) Assemble

func Float

func Float(f float64) Assemble

func Int

func Int(i int64) Assemble
func Link(l datamodel.Link) Assemble

func List

func List(sizeHint int64, fn func(datamodel.ListAssembler)) Assemble

func Map

func Map(sizeHint int64, fn func(datamodel.MapAssembler)) Assemble

func Node

func Node(n datamodel.Node) Assemble

func Null

func Null() Assemble

func String

func String(s string) Assemble

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL