serialize

package
v0.0.0-...-73e4950 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	ErrUnsupportedUseRead erro.String = "Serialize() method unsupported, use the Read() method instead"
	ErrUnsupported        erro.State  = "method: unsupported"
)

Variables

View Source
var (
	AnyParam  = _anyWrap{Any(nil)}
	BinParam  = _binaryWrap{Binary(nil)}
	ErrParam  = _errorWrap{Error(nil)}
	F64Param  = _float64Param{Float64(0)}
	IntParam  = _intParam{Integer(0)}
	MapParam  = _mapParam{Map(nil)}
	StrParam  = _stringParam{String("")}
	UintParam = _uintParam{Uinteger(0)}
)

Functions

func Any

func Any(v interface{}) *_any

func Binary

func Binary(v io.Reader) *_binary

func Error

func Error(v error) *_error

func Float64

func Float64(v float64) *_float64

func Integer

func Integer(v int) *_int

func Map

func Map(m map[string]interface{}) _map

func String

func String(v string) *_string

func Uinteger

func Uinteger(v uint) *_uint

Types

type Convert

type Convert []Serializable

func (Convert) ToInterface

func (in Convert) ToInterface() []interface{}

type Serializable

type Serializable interface {
	Serialize() (string, error)
	Unserialize(string) error
}
Example (Assignment)
// Serialized values can be assigned to a variable before being used

var e = Error(fmt.Errorf("some bad thing"))
var f = Float64(1.4)
var i = Integer(10)
var s = String("string")
var u = Uinteger(11)

fmt.Printf("Error: %v\nFloat: %f\nInt: %d\nString: %q\nUint: %d\n", e, f.Interface(), i.Interface(), s, u.Interface())
Output:

Error: some bad thing
Float: 1.400000
Int: 10
String: "string"
Uint: 11
Example (Binary)
r := strings.NewReader("This is streamed data")

// binary doesn't serialize to a string, but it
// provides a io.Reader from the Interface() method
b := Binary(r)

io.Copy(os.Stdout, b.Interface().(io.Reader))
b.Interface().(io.Seeker).Seek(0, 0)
fmt.Println("\n----")
// There is also the hidden (io.Reader) interface

io.Copy(os.Stdout, b)
Output:

This is streamed data
----
This is streamed data
Example (Custom)
pepperoni := &pizza{crust: "thin", toppings: []string{"cheese", "pepperoni"}}
fmt.Println(pepperoni.Serialize())

cheese := &pizza{}
cheese.Unserialize("crust:cheese top:mozzarella,gorgonzola,goat,parmesan")
fmt.Println(cheese)
Output:

crust:thin top:cheese,pepperoni <nil>
&{cheese [mozzarella gorgonzola goat parmesan]}
Example (Interface)
var e = Error(nil)
var f = Float64(0)
var i = Integer(0)
var s = String("")
var u = Uinteger(0)

e.Unserialize("something bad happened")
f.Unserialize("3.14")
i.Unserialize("10")
s.Unserialize("pass")
u.Unserialize("20")

fmt.Printf("%v\n%f\n%d\n%q\n%d", e.Interface(), f.Interface(), i.Interface(), s.Interface(), u.Interface())
Output:

something bad happened
3.140000
10
"pass"
20
Example (Params)
var e = ErrParam
var f = F64Param
var i = IntParam
var s = StrParam
var u = UintParam

e.Unserialize("something bad happened")
f.Unserialize("3.14")
i.Unserialize("10")
s.Unserialize("pass")
u.Unserialize("20")

fmt.Printf("%q\n%q\n%q\n%q\n%q", e, f, i, s, u)
Output:

""
""
""
""
""
Example (Short_func)
// Assign the serialize function to a short function name for later usage

var er = Error
var fl = Float64
var in = Integer
var st = String
var ui = Uinteger

var a, b, c = er(fmt.Errorf("error A")), er(fmt.Errorf("error B")), er(fmt.Errorf("error C"))
var d, e, f = fl(1.5), fl(2.6), fl(3.7)
var g, h, i = in(100), in(-100), in(1000000)
var j, k, l = st("The"), st("Quick"), st("Brown")
var m, n, o = ui(400), ui(800), ui(9223372036854775808)

fmt.Printf("%v\n", a)
fmt.Printf("%v\n", b)
fmt.Printf("%v\n", c)
fmt.Printf("%f\n", *d)
fmt.Printf("%f\n", *e)
fmt.Printf("%f\n", *f)
fmt.Printf("%d\n", *g)
fmt.Printf("%d\n", *h)
fmt.Printf("%d\n", *i)
fmt.Printf("%s\n", j)
fmt.Printf("%s\n", k)
fmt.Printf("%s\n", l)
fmt.Printf("%d\n", *m)
fmt.Printf("%d\n", *n)
fmt.Printf("%d\n", *o)
Output:

error A
error B
error C
1.500000
2.600000
3.700000
100
-100
1000000
The
Quick
Brown
400
800
9223372036854775808
Example (String)
var e = Error(nil)
var f = Float64(0)
var i = Integer(0)
var s = String("")
var u = Uinteger(0)

e.Unserialize("something bad happened")
f.Unserialize("3.14")
i.Unserialize("10")
s.Unserialize("pass")
u.Unserialize("20")

fmt.Printf("%s\n%s\n%s\n%s\n%s", e, f, i, s, u)
Output:

something bad happened
3.14
10
pass
20
Example (Unserialize)
var e = Error(nil)
var f = Float64(0)
var i = Integer(0)
var s = String("")
var u = Uinteger(0)

e.Unserialize("something bad happened")
f.Unserialize("3.14")
i.Unserialize("10")
s.Unserialize("pass")
u.Unserialize("20")

es, _ := e.Serialize()
fs, _ := f.Serialize()
is, _ := i.Serialize()
ss, _ := s.Serialize()
us, _ := u.Serialize()

fmt.Printf("%s\n%s\n%s\n%s\n%s", es, fs, is, ss, us)
Output:

something bad happened
3.14
10
pass
20

type SerializableParam

type SerializableParam interface {
	Serializable
	Interface() interface{}
	Param() Serializable
}

https://github.com/socketio/socket.io/tree/master/examples/custom-parsers

type SerializableWrap

type SerializableWrap interface {
	Serializable
	Interface() interface{}
}

Jump to

Keyboard shortcuts

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