quip

package
v0.7.1-0...-2a1f27b Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2021 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

quip is a package of quick ipld patterns.

Most quip functions take a pointer to an error as their first argument. This has two purposes: if there's an error there, the quip function will do nothing; and if the quip function does something and creates an error, it puts it there. The effect of this is that most logic can be written very linearly.

quip functions can be used to increase brevity without worrying about performance costs. None of the quip functions cause additional allocations in the course of their work. Benchmarks indicate no measurable speed penalties versus longhand manual error checking.

Several functions perform comparable operations but with different arguments, and so these function names follow a pattern:

  • `Build*` functions take a NodePrototype and return a Node.
  • `Assemble*` functions take a NodeAssembler and feed data into it.
  • There is no analog of `NodeAssembler.Begin*` functions (we simply always use callbacks for structuring, because this is reasonably optimal).
  • `Assign*` functions handle values of the scalar kinds (these of course also never need callbacks, since there's no possible recursion).

The `Assemble*` functions are used recursively. The `Build*` functions can be used instead of `Assemble*` at the top of a tree in order to save on writing a few additional lines of NodeBuilder setup and usage. (The `Assemble*` functions can also be used at the top of a tree if you wish to control the NodeBuilder yourself. This may be desirable for being able to reset and reuse the NodeBuilder when performance is critical, for example.)

The usual IPLD NodeAssembler, MapAssembler, and ListAssembler interfaces are still available while using quip functions, should you wish to interact with them directly, or compose the use of quip functions with other styles of data manipulation.

Example
var err error
n := quip.BuildMap(&err, basicnode.Prototype.Any, 4, func(ma ipld.MapAssembler) {
	quip.AssignMapEntryString(&err, ma, "some key", "some value")
	quip.AssignMapEntryString(&err, ma, "another key", "another value")
	quip.AssembleMapEntry(&err, ma, "nested map", func(na ipld.NodeAssembler) {
		quip.AssembleMap(&err, na, 2, func(ma ipld.MapAssembler) {
			quip.AssignMapEntryString(&err, ma, "deeper entries", "deeper values")
			quip.AssignMapEntryString(&err, ma, "more deeper entries", "more deeper values")
		})
	})
	quip.AssembleMapEntry(&err, ma, "nested list", func(na ipld.NodeAssembler) {
		quip.AssembleList(&err, na, 2, func(la ipld.ListAssembler) {
			quip.AssignListEntryInt(&err, la, 1)
			quip.AssignListEntryInt(&err, la, 2)
		})
	})
})
if err != nil {
	panic(err)
}
dagjson.Encoder(n, os.Stdout)
Output:

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsorbError

func AbsorbError(e *error, err error)

func AssembleList

func AssembleList(e *error, na ipld.NodeAssembler, sizeHint int64, fn func(la ipld.ListAssembler))

func AssembleListEntry

func AssembleListEntry(e *error, la ipld.ListAssembler, fn func(va ipld.NodeAssembler))

func AssembleMap

func AssembleMap(e *error, na ipld.NodeAssembler, sizeHint int64, fn func(ma ipld.MapAssembler))

func AssembleMapEntry

func AssembleMapEntry(e *error, ma ipld.MapAssembler, k string, fn func(va ipld.NodeAssembler))

func Assign

func Assign(e *error, na ipld.NodeAssembler, x interface{})

Assign takes any value and attempts to turn it into something we can reparse as Node-like, using the same logic as fluent.Reflect. It's not particularly performant, so use it only when convenience matters more than performance.

func AssignBool

func AssignBool(e *error, na ipld.NodeAssembler, x bool)

func AssignBytes

func AssignBytes(e *error, na ipld.NodeAssembler, x []byte)

func AssignFloat

func AssignFloat(e *error, na ipld.NodeAssembler, x float64)

func AssignInt

func AssignInt(e *error, na ipld.NodeAssembler, x int64)
func AssignLink(e *error, na ipld.NodeAssembler, x ipld.Link)

func AssignListEntry

func AssignListEntry(e *error, la ipld.ListAssembler, x interface{})

AssignListEntry takes any value and attempts to turn it into something we can reparse as Node-like, using the same logic as fluent.Reflect. It's not particularly performant, so use it only when convenience matters more than performance.

func AssignListEntryBool

func AssignListEntryBool(e *error, la ipld.ListAssembler, v bool)

func AssignListEntryBytes

func AssignListEntryBytes(e *error, la ipld.ListAssembler, v []byte)

func AssignListEntryFloat

func AssignListEntryFloat(e *error, la ipld.ListAssembler, v float64)

func AssignListEntryInt

func AssignListEntryInt(e *error, la ipld.ListAssembler, v int64)
func AssignListEntryLink(e *error, la ipld.ListAssembler, v ipld.Link)

func AssignListEntryNode

func AssignListEntryNode(e *error, la ipld.ListAssembler, v ipld.Node)

func AssignListEntryNull

func AssignListEntryNull(e *error, la ipld.ListAssembler)

func AssignListEntryString

func AssignListEntryString(e *error, la ipld.ListAssembler, v string)

func AssignMapEntry

func AssignMapEntry(e *error, ma ipld.MapAssembler, k string, x interface{})

AssignMapEntry takes any value and attempts to turn it into something we can reparse as Node-like, using the same logic as fluent.Reflect. It's not particularly performant, so use it only when convenience matters more than performance.

func AssignMapEntryBool

func AssignMapEntryBool(e *error, ma ipld.MapAssembler, k string, v bool)

func AssignMapEntryBytes

func AssignMapEntryBytes(e *error, ma ipld.MapAssembler, k string, v []byte)

func AssignMapEntryFloat

func AssignMapEntryFloat(e *error, ma ipld.MapAssembler, k string, v float64)

func AssignMapEntryInt

func AssignMapEntryInt(e *error, ma ipld.MapAssembler, k string, v int64)
func AssignMapEntryLink(e *error, ma ipld.MapAssembler, k string, v ipld.Link)

func AssignMapEntryNode

func AssignMapEntryNode(e *error, ma ipld.MapAssembler, k string, v ipld.Node)

func AssignMapEntryNull

func AssignMapEntryNull(e *error, ma ipld.MapAssembler, k string)

func AssignMapEntryString

func AssignMapEntryString(e *error, ma ipld.MapAssembler, k string, v string)

func AssignNode

func AssignNode(e *error, na ipld.NodeAssembler, x ipld.Node)

func AssignNull

func AssignNull(e *error, na ipld.NodeAssembler)

func AssignString

func AssignString(e *error, na ipld.NodeAssembler, x string)

func BuildList

func BuildList(e *error, np ipld.NodePrototype, sizeHint int64, fn func(la ipld.ListAssembler)) ipld.Node

func BuildMap

func BuildMap(e *error, np ipld.NodePrototype, sizeHint int64, fn func(ma ipld.MapAssembler)) ipld.Node

func CopyRange

func CopyRange(e *error, la ipld.ListAssembler, src ipld.Node, start, end int64)

Types

This section is empty.

Jump to

Keyboard shortcuts

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