dataeq

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2020 License: MIT Imports: 2 Imported by: 8

Documentation

Overview

Package dataeq allows to create API to compare two values as the data format such as JSON and YAML. dataeq compare two values by the followin way.

1. convert value to a byte string by Marshal 2. unmarshal a byte string to `interface{}` by Unmarshal 3. compare two values by reflect.DeepEqual

So dataeq requires two API for the data format, Marhsal and Unmarshal.

Example
package main

import (
	"fmt"
	"log"

	"github.com/suzuki-shunsuke/go-dataeq/dataeq"
)

type (
	Foo struct {
		Foo string `json:"foo"`
	}
)

func checkResult(b bool, err error) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(b)
}

func main() {
	b, err := dataeq.JSON.Equal(
		Foo{Foo: "bar"},
		map[string]string{"foo": "bar"},
	)
	checkResult(b, err)

	// when a type of value is []byte, it is treated as JSON string
	b, err = dataeq.JSON.Equal(
		Foo{Foo: "bar"},
		[]byte(`{"foo": "bar"}`),
	)
	checkResult(b, err)

}
Output:

true
true

Index

Examples

Constants

This section is empty.

Variables

View Source
var JSON = New(json.Marshal, json.Unmarshal) //nolint:gochecknoglobals

Functions

This section is empty.

Types

type DataFormat

type DataFormat struct {
	// contains filtered or unexported fields
}

DataFormat allows to compare values as the data format. DataFormat must be created by the function `New`.

func New

func New(marshal Marshal, unmarshal Unmarshal) DataFormat

New creates DataFormat by Marshal and Unmarshal. DataFormat must be created by this function.

func (DataFormat) Convert

func (df DataFormat) Convert(x interface{}) (interface{}, error)

Convert converts value to byte string and unmarshals the byte string to `interface{}`. Convert can be used to normalize the value to compare with the other value.

func (DataFormat) ConvertByte

func (df DataFormat) ConvertByte(b []byte) (interface{}, error)

ConvertByte unmarshals a byte string to `interface{}`.

func (DataFormat) Equal

func (df DataFormat) Equal(x, y interface{}) (bool, error)

Equal returns true if two arguments are equal.

type Marshal

type Marshal func(interface{}) ([]byte, error)

Marshal converts data to a byte string.

type Unmarshal

type Unmarshal func([]byte, interface{}) error

Unmarshal parses the encoded byte string and stores the result in the value pointed to by 2th argument. The 2th argument must be a pointer.

Jump to

Keyboard shortcuts

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