live

package module
v0.0.0-...-c7b8ccc Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2022 License: BSD-3-Clause Imports: 8 Imported by: 26

README

Overview

live.Data is a handy general-purpose data wrapper, which solves the problem that the same data type in different versions of a plugin are actually not the same at runtime. It is designed to work with hotswap, which provides a solution for reloading your go code without restarting your server, interrupting or blocking any ongoing procedure.

Getting Started

go get -u github.com/edwingeng/live

Usage

type questInfo1 struct {
    ID   int64
    Name string
    Done bool
}

q1 := questInfo1{
    ID:   5848,
    Name: "Of Love and Family",
}

liveObj := WrapObject(&q1)

// 2000 years later...

type questInfo2 struct {
    ID   int64
    Name string
    Desc string
    Done bool
}

var q2 questInfo2
q2.Desc = "<>"
liveObj.MustUnwrapObject(&q2)
fmt.Printf("ID: %v\n", q2.ID)
fmt.Printf("Name: %v\n", q2.Name)
fmt.Printf("Desc: %v\n", q2.Desc)
fmt.Printf("Done: %v\n", q2.Done)

// Output:
// ID: 5848
// Name: Of Love and Family
// Desc: <>
// Done: false

Unsupported Types

  • uintptr
  • func
  • interface{}, as known as any
  • unsafe.Pointer

FAQ

  • Is it allowed for a struct to have a field of an unsupported type?

Yes. The field tag, live:"true", is designed for that.

type example struct {
    x func() `live:"true"`
}

Documentation

Overview

Package live provides some types and functions designed for plugin hot reload, which solves the problem that the same type in different versions of a plugin are actually not the same at runtime.

Example
type questInfo1 struct {
	ID   int64
	Name string
	Done bool
}

q1 := questInfo1{
	ID:   5848,
	Name: "Of Love and Family",
}

liveObj := MustWrapObject(&q1)

// 2000 years later...

type questInfo2 struct {
	ID   int64
	Name string
	Desc string
	Done bool
}

var q2 questInfo2
q2.Desc = "<>"
liveObj.MustUnwrapObject(&q2)
fmt.Printf("ID: %v\n", q2.ID)
fmt.Printf("Name: %v\n", q2.Name)
fmt.Printf("Desc: %v\n", q2.Desc)
fmt.Printf("Done: %v\n", q2.Done)
Output:

ID: 5848
Name: Of Love and Family
Desc: <>
Done: false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Blacklist     blacklist
	SkipTypeCheck bool
}

Config has a blacklist, with which it prevents any object of a type defined in a plugin from being converted to a live data by WrapValueDirect. Config also checks whether an object refers to, directly or indirectly, an unsupported type. The unsupported types are: func, uintptr, interface{}, any, and unsafe.Pointer. It panics if any of these types is found.

To force a struct field to bypass the check, give it the following tag: `live:"true"`.

func NewConfig

func NewConfig(pkgBlacklist []string) Config

type Data

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

Data is a handy general-purpose data wrapper.

var (
	Nil Data
)

func FromProtorBytes

func FromProtorBytes(dAtA []byte) (Data, error)

func MustWrapObject

func MustWrapObject(obj interface{}) Data

func MustWrapProtobufObject

func MustWrapProtobufObject(obj ProtobufMarshaler) Data

func WrapBool

func WrapBool(v bool) Data

func WrapBytes

func WrapBytes(v []byte) Data

func WrapComplex128

func WrapComplex128(v complex128) Data

func WrapComplex64

func WrapComplex64(v complex64) Data

func WrapFloat32

func WrapFloat32(v float32) Data

func WrapFloat64

func WrapFloat64(v float64) Data

func WrapInt

func WrapInt(v int) Data

func WrapInt16

func WrapInt16(v int16) Data

func WrapInt32

func WrapInt32(v int32) Data

func WrapInt64

func WrapInt64(v int64) Data

func WrapInt8

func WrapInt8(v int8) Data

func WrapObject

func WrapObject(obj interface{}) (Data, error)

func WrapProtobufObject

func WrapProtobufObject(obj ProtobufMarshaler) (Data, error)

func WrapString

func WrapString(v string) Data

func WrapUint

func WrapUint(v uint) Data

func WrapUint16

func WrapUint16(v uint16) Data

func WrapUint32

func WrapUint32(v uint32) Data

func WrapUint64

func WrapUint64(v uint64) Data

func WrapUint8

func WrapUint8(v uint8) Data

func WrapValueDirect

func WrapValueDirect(v interface{}, cfg Config) Data

WrapValueDirect converts almost anything into a live data, which is not marshallable.

func (Data) Bool

func (d Data) Bool() bool

func (Data) Bytes

func (d Data) Bytes() []byte

func (Data) Complex128

func (d Data) Complex128() complex128

func (Data) Complex64

func (d Data) Complex64() complex64

func (Data) Float32

func (d Data) Float32() float32

func (Data) Float64

func (d Data) Float64() float64

func (Data) Int

func (d Data) Int() int

func (Data) Int16

func (d Data) Int16() int16

func (Data) Int32

func (d Data) Int32() int32

func (Data) Int64

func (d Data) Int64() int64

func (Data) Int8

func (d Data) Int8() int8

func (Data) MarshalEasyJSON

func (d Data) MarshalEasyJSON(w *jwriter.Writer)

func (Data) MarshalJSON

func (d Data) MarshalJSON() ([]byte, error)

func (Data) Marshallable

func (d Data) Marshallable() bool

func (Data) MustUnwrapObject

func (d Data) MustUnwrapObject(out interface{})

func (Data) MustUnwrapProtobufObject

func (d Data) MustUnwrapProtobufObject(out ProtobufUnmarshaler)

func (Data) String

func (d Data) String() string

func (Data) Uint

func (d Data) Uint() uint

func (Data) Uint16

func (d Data) Uint16() uint16

func (Data) Uint32

func (d Data) Uint32() uint32

func (Data) Uint64

func (d Data) Uint64() uint64

func (Data) Uint8

func (d Data) Uint8() uint8

func (*Data) UnmarshalEasyJSON

func (d *Data) UnmarshalEasyJSON(l *jlexer.Lexer)

func (*Data) UnmarshalJSON

func (d *Data) UnmarshalJSON(dAtA []byte) error

func (Data) UnwrapObject

func (d Data) UnwrapObject(out interface{}) error

func (Data) UnwrapProtobufObject

func (d Data) UnwrapProtobufObject(out ProtobufUnmarshaler) error

func (Data) Value

func (d Data) Value() interface{}

type ProtobufMarshaler

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

type ProtobufUnmarshaler

type ProtobufUnmarshaler interface {
	Unmarshal([]byte) error
}

type Protor

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

func NewProtor

func NewProtor(d Data) Protor

func (Protor) Marshal

func (p Protor) Marshal() (dAtA []byte, err error)

func (Protor) MarshalTo

func (p Protor) MarshalTo(dAtA []byte) (int, error)

func (Protor) MarshalToSizedBuffer

func (p Protor) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (Protor) Size

func (p Protor) Size() (n int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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