datas

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package datas provides a collection of data marshaling and unmarshaling interfaces and functions. These interfaces and functions provide a flexible and extensible way to work with structured data in Go, whether it is being serialized to a byte slice, written to an IO stream, or both. In addition to these interfaces, this package also provides a number of useful functions for working with data in Go, including functions for encoding and decoding data using common serialization formats like JSON and XML.

Example
package main

import (
	"fmt"

	"github.com/Prastiwar/Go-flow/datas"
)

func main() {
	var serializer datas.Marshaler = datas.Json()

	type Data struct {
		Foo string `json:"foo"`
	}

	testData := Data{Foo: "success"}

	b, err := serializer.Marshal(testData)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(b))

}
Output:

{"foo":"success"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteFormatter

type ByteFormatter interface {
	Marshaler
	Unmarshaler
}

ByteFormatter is an interface that combines Marshaler and Unmarshaler into a single interface for working with byte slices.

type ByteIOFormatter

type ByteIOFormatter interface {
	ByteFormatter
	IOFormatter
}

ByteIOFormatter is an interface that combines ByteFormatter and IOFormatter into a single interface for working with both byte slices and IO streams.

func Json

func Json() ByteIOFormatter

Json returns a ByteIOFormatter for encoding and decoding data in JSON format. The returned ByteIOFormatter is implemented using the encoding/json package from the Go standard library.

func Xml

func Xml() ByteIOFormatter

Xml returns a ByteIOFormatter for encoding and decoding data in XML format. The returned ByteIOFormatter is implemented using the encoding/xml package from the Go standard library.

type IOFormatter

type IOFormatter interface {
	WriterMarshaler
	ReaderUnmarshaler
}

IOFormatter is an interface that combines WriterMarshaler and ReaderUnmarshaler into a single interface for working with IO streams.

type Marshaler

type Marshaler interface {
	// Marshal returns a byte slice representation of the provided value.
	// An error is returned if the value cannot be marshaled.
	Marshal(v any) ([]byte, error)
}

Marshaler is an interface for types that can marshal any value to a byte slice.

type MarshalerFunc

type MarshalerFunc func(v any) ([]byte, error)

WriterMarshalerFunc is a function type that can be used as a Marshaler.

func (MarshalerFunc) Marshal

func (f MarshalerFunc) Marshal(v any) ([]byte, error)

type ReaderUnmarshaler

type ReaderUnmarshaler interface {
	// UnmarshalFrom reads data from the given io.Reader and populates the given value with it.
	// An error is returned if the data cannot be unmarshaled, or if reading from the io.Reader fails.
	UnmarshalFrom(r io.Reader, v any) error
}

ReaderUnmarshaler is an interface for types that can unmarshal data from an io.Reader into a value.

type ReaderUnmarshalerFunc

type ReaderUnmarshalerFunc func(r io.Reader, v any) error

ReaderUnmarshalerFunc is a function type that can be used as a ReaderUnmarshaler.

func (ReaderUnmarshalerFunc) UnmarshalFrom

func (f ReaderUnmarshalerFunc) UnmarshalFrom(r io.Reader, v any) error

type Unmarshaler

type Unmarshaler interface {
	// Unmarshal populates the given value with data from the byte slice.
	// An error is returned if the value cannot be unmarshaled.
	Unmarshal(data []byte, v any) error
}

Unmarshaler is an interface for types that can unmarshal a byte slice to any value.

type UnmarshalerFunc

type UnmarshalerFunc func(data []byte, v any) error

UnmarshalerFunc is a function type that can be used as a Unmarshaler.

func (UnmarshalerFunc) Unmarshal

func (f UnmarshalerFunc) Unmarshal(data []byte, v any) error

type WriterMarshaler

type WriterMarshaler interface {
	// MarshalTo writes a byte slice representation of the provided value to the given io.Writer.
	// An error is returned if the value cannot be marshaled, or if writing to the io.Writer fails.
	MarshalTo(w io.Writer, v any) error
}

WriterMarshaler is an interface for types that can marshal any value to an io.Writer.

type WriterMarshalerFunc

type WriterMarshalerFunc func(w io.Writer, v any) error

WriterMarshalerFunc is a function type that can be used as a WriterMarshaler.

func (WriterMarshalerFunc) MarshalTo

func (f WriterMarshalerFunc) MarshalTo(w io.Writer, v any) error

Jump to

Keyboard shortcuts

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