pamburus / valf

Typified values with snapshotting capabilities

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

valf

GoDoc Build Status Go Report Status Coverage Status

This package provides means for dealing with typified values with snapshotting capabilities uniformly and a way to get its type and value back using visitor pattern.

This package was originally a part of github.com/ssgreg/logf package.

It has become a separate package to get richer flexibility and make this technology available for any other purpose which may not be related to logging.

Example

The following example creates a new valf value and gets its type back using visitor pattern.

package main

import (
	"fmt"

	"github.com/pamburus/valf"
)

type testVisitor struct {
	valf.IgnoringVisitor
}

func (v testVisitor) VisitString(value string) {
	fmt.Printf("string: %#v\n", value)
}

func (v testVisitor) VisitBytes(value []byte) {
	fmt.Printf("bytes: %q\n", string(value))
}

func main() {
	s := valf.String("some string value")
	s.AcceptVisitor(testVisitor{})

	bv := []byte("some bytes value")
	b := valf.Bytes(bv)
	b.AcceptVisitor(testVisitor{})

	bv[1] = 'a'
	b.AcceptVisitor(testVisitor{})

	bs := b.Snapshot()
	bv[1] = 'o'
	bs.AcceptVisitor(testVisitor{})
}

The output is the following:

string: "some string value"
bytes: "some bytes value"
bytes: "same bytes value"
bytes: "same bytes value"

About

Typified values with snapshotting capabilities

License:MIT License


Languages

Language:Go 100.0%