yamf

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2022 License: MIT Imports: 10 Imported by: 0

README

yamf

GoDoc

Yet-Another-Multi-Format implementation in Go.

Warning

This library is a work in progress and does not strive to have an A+ score at the moment. Although, v0.1.0 should be simple, usable, and self-explanatory enough with fair unit test coverage.

Example
Encode hash to JSON
package main

import (
	"encoding/json"
	"os"

	"github.com/ksinica/yamf"
	"github.com/ksinica/yamf/hash/blake2b"
)

func main() {
	hash, _ := blake2b.New()
	hash.Write([]byte("Hello world!"))

	json.NewEncoder(os.Stdout).Encode(struct {
		Hash *yamf.TypeValue `json:"hash"`
	}{
		Hash: yamf.HashToTypeValue(hash),
	})
	// {"hash":"0008239dad53cf69f358"}
}
Decode hash from JSON
package main

import (
	"encoding/json"

	"github.com/ksinica/yamf"
	_ "github.com/ksinica/yamf/hash/blake2b"
)

func main() {
	var val struct {
		Hash *yamf.TypeValue `json:"hash"`
	}

	json.Unmarshal([]byte(`{"hash":"0008239dad53cf69f358"}`), &val)

	hash, _ := yamf.TypeValueToHash(*val.Hash)
	hash.Write([]byte("Hello world!"))

	if !yamf.HashEqual(hash, *val.Hash) {
		panic("oh no!")
	}
}

License

Source code is available under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrHashNotFound = errors.New("hash type not found")
)
View Source
var (
	// ErrValueTooBig is returned when value byte size exceeds limit
	// set by MaxValueSize.
	ErrValueTooBig = errors.New("yamf: value is too big")
)
View Source
var (
	// MaxValueSize sets the maximal value byte size for read operations.
	// Less than zero means no limit.
	MaxValueSize = 1024
)

Functions

func HashEqual

func HashEqual(a Hash, b TypeValue) bool

HashEqual is a handy function to compare a Hash object's sum to a sum stored in TypeValue.

func RegisterHash

func RegisterHash(id uint64, f func() (Hash, error))

RegisterHash associates factory function with an hash type identifier. If a given type id exists, the function will be overwritten.

RegisterHash can be called concurrently.

Types

type Hash

type Hash interface {
	hash.Hash
	HashType() uint64
}

func CreateHash

func CreateHash(id uint64) (Hash, error)

CreateHash creates hash object associated with given type id and an error, if any.

CreateHash can be called concurrently.

func TypeValueToHash

func TypeValueToHash(val TypeValue) (Hash, error)

type TypeValue

TypeValue encapsulates type and value pair and allows to encode and decode them as per Simple Type-Length-Value specification:

https://github.com/AljoschaMeyer/stlv

func HashToTypeValue

func HashToTypeValue(hash Hash) *TypeValue

func (TypeValue) Equal

func (t TypeValue) Equal(other TypeValue) bool

func (*TypeValue) MarshalBinary

func (s *TypeValue) MarshalBinary() ([]byte, error)

MarshalBinary encodes receiver into a byte slice and returns an error, if any.

func (*TypeValue) MarshalText

func (s *TypeValue) MarshalText() ([]byte, error)

MarshalText encodes receiver into a textual representation and returns an error, if any.

func (*TypeValue) ReadFrom

func (t *TypeValue) ReadFrom(r io.Reader) (n int64, err error)

MarshalBinary encodes receiver into a byte slice and returns an error, if any.

varu64.ErrNonCanonical may be returned in the case when non-canonical encoding was encountered during the process.

func (TypeValue) String

func (t TypeValue) String() string

func (*TypeValue) UnmarshalBinary

func (s *TypeValue) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes data into the receiver and returns an error, if any.

func (*TypeValue) UnmarshalText

func (s *TypeValue) UnmarshalText(text []byte) error

UnmarshalText decodes textual representation into the receiver and returns an error, if any.

func (*TypeValue) WriteTo

func (t *TypeValue) WriteTo(w io.Writer) (n int64, err error)

WriteTo encodes receiver to w. It returns the number of bytes written and an error, if any.

Directories

Path Synopsis
hash

Jump to

Keyboard shortcuts

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