hessian

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2019 License: MIT Imports: 8 Imported by: 0

README

Golang Hessian

Build Status codecov Go Report Card License GoDoc Release

Golang Hessian can use hessian proxy to connect to hessian service.

Reference:

How to use ?

No argument method call
package main

import (
    "log"
    "time"

    hessian "github.com/ggwhite/go-hessian"
)

func main() {
    var addr = "http://localhost:8080/simple"
    proxy, err := hessian.NewProxy(&hessian.ProxyConfig{
        Version: hessian.V1,
        URL:     addr,
    })
    if err != nil {
        panic(err)
    }
    
    args, err := proxy.Invoke("str")
    log.Println(args, err)
}

Result:

2019/04/18 16:20:52 [Hello] <nil>

Create a proxy to your hessian service, invoke from given method name, it return a slice of interface and error.

With argument method call
package main

import (
    "log"
    "time"

    hessian "github.com/ggwhite/go-hessian"
)

func main() {
    var addr = "http://localhost:8080/simple"
    proxy, err := hessian.NewProxy(&hessian.ProxyConfig{
        Version: hessian.V1,
        URL:     addr,
    })
    if err != nil {
        panic(err)
    }
    
    log.Println(proxy.Invoke("strI2", "ggwhite", "this is message"))
    log.Println(args, err)
}

Result:

2019/04/18 16:43:18 [Hello[ggwhite], this is message] <nil>
With struct argument method call
package main

import (
    "log"
    "time"

    hessian "github.com/ggwhite/go-hessian"
)

type User struct {
    hessian.Package `hessian:"lab.ggw.shs.service.User"`
    Name            string      `hessian:"name"`
    Email           interface{} `hessian:"email"`
    Father          *User       `hessian:"father"`
}

func main() {
    var addr = "http://localhost:8080/simple"
    proxy, err := hessian.NewProxy(&hessian.ProxyConfig{
        Version: hessian.V1,
        URL:     addr,
    })
    if err != nil {
        panic(err)
    }
    proxy.RegisterType(reflect.TypeOf(User{}))
    
    ans, err := proxy.Invoke("obj")
    log.Println(ans[0], err)
    
    log.Println(proxy.Invoke("objI", &User{
        Name:  "ggwhite",
        Email: "ggw.chang@gmail.com",
    }))
}

Result:

2019/04/18 16:46:13 &{ ggwhite ggw.chang@gmail.com <nil>} <nil>
2019/04/18 16:46:13 [ggwhite] <nil>

Give hessian.Package to your struct and add tag hessian to let proxy know what package(ClassName) of your POJO.

Mapping type can be a type of struct or a pointer of the struct.

Supported

Type Serialize Deserialize
null Y Y
boolean Y Y
int Y Y
long Y Y
double Y Y
date N N
string Y Y
xml N N
binary Y Y
list Y Y
ref N N
remote N N

Documentation

Index

Constants

View Source
const (
	V1 version = iota
	V2
)

Versions

Variables

This section is empty.

Functions

This section is empty.

Types

type Deserializer

type Deserializer interface {
	// Read parse input (io.Reader) to return value
	Read() ([]interface{}, error)

	// Reset
	Reset(io.Reader)

	// SetTypeMap
	SetTypeMap(map[string]reflect.Type)
}

Deserializer input stream for hessian response

type DeserializerV1

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

DeserializerV1 input stream for hessian 1.0 response

func NewDeserializerV1

func NewDeserializerV1() *DeserializerV1

NewDeserializerV1 create DeserializerV1

func (*DeserializerV1) BuildObject

func (i *DeserializerV1) BuildObject(pkg string, data map[interface{}]interface{}) (interface{}, error)

BuildObject build a struct or ptr(of struct) from given package and data

func (*DeserializerV1) Read

func (i *DeserializerV1) Read() ([]interface{}, error)

Read parse input (io.Reader) to return value

func (*DeserializerV1) ReadArrayAt

func (i *DeserializerV1) ReadArrayAt(p []byte, begin int) ([]interface{}, int, error)

ReadArrayAt Read string from given bytes and begin index.

After 'Vt <type> <size>' chart, find <value> + <value> ...

func (*DeserializerV1) ReadAt

func (i *DeserializerV1) ReadAt(p []byte, begin int) ([]interface{}, int, error)

ReadAt Read object from given bytes and begin index.

func (*DeserializerV1) ReadBytesAt

func (i *DeserializerV1) ReadBytesAt(p []byte, begin int) ([]byte, int, error)

ReadBytesAt Read bytes from given bytes and begin index.

After 'B' chart, find b16 b8 <bytes-value>

func (*DeserializerV1) ReadFloat64At

func (i *DeserializerV1) ReadFloat64At(p []byte, begin int) (float64, int, error)

ReadFloat64At Read string from given bytes and begin index.

After 'D' chart, find b64 b56 b48 b40 b32 b24 b16 b8

func (*DeserializerV1) ReadInt32At

func (i *DeserializerV1) ReadInt32At(p []byte, begin int) (int32, int, error)

ReadInt32At Read string from given bytes and begin index.

After 'I' chart, find b32 b24 b16 b8

func (*DeserializerV1) ReadInt64At

func (i *DeserializerV1) ReadInt64At(p []byte, begin int) (int64, int, error)

ReadInt64At Read string from given bytes and begin index.

After 'L' chart, find b64 b56 b48 b40 b32 b24 b16 b8

func (*DeserializerV1) ReadMapAt

func (i *DeserializerV1) ReadMapAt(p []byte, begin int) (map[interface{}]interface{}, int, error)

ReadMapAt Read string from given bytes and begin index.

After 'Mt' chart, find <key> + <value> ...

func (*DeserializerV1) ReadStringAt

func (i *DeserializerV1) ReadStringAt(p []byte, begin int) (string, int, error)

ReadStringAt Read string from given bytes and begin index.

After 'S' chart, find b16 b8 <string-value>

func (*DeserializerV1) Reset

func (i *DeserializerV1) Reset(r io.Reader)

Reset the io.Reader

func (*DeserializerV1) SetTypeMap

func (i *DeserializerV1) SetTypeMap(typeMap map[string]reflect.Type)

SetTypeMap set type map

type Package

type Package string

Package pojo class name, ex: lab.ggw.demo.User

type Proxy

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

Proxy hessian proxy

func NewProxy

func NewProxy(c *ProxyConfig) (*Proxy, error)

NewProxy create a proxy of the hessian service

func (*Proxy) Invoke

func (c *Proxy) Invoke(m string, args ...interface{}) ([]interface{}, error)

Invoke input method name and arguments, it will send request to server, and parse response to interface

func (*Proxy) RegisterType

func (c *Proxy) RegisterType(t reflect.Type) error

RegisterType input a type with Package field, register type mapping

type ProxyConfig

type ProxyConfig struct {
	Version version
	URL     string
	TypeMap map[string]reflect.Type
	Client  *http.Client
}

ProxyConfig config for NewProxy

func (*ProxyConfig) Validate

func (c *ProxyConfig) Validate() error

Validate Proxy Config

type Serializer

type Serializer interface {
	// Call Writes a complete method call.
	Call(string, ...interface{}) error

	// Writer get writer
	Writer() io.Writer

	// Reader get reader
	Reader() io.Reader

	// Flush clean writer
	Flush()

	// SetTypeMap
	SetTypeMap(map[string]reflect.Type)
}

Serializer output stream for hessian requests

type SerializerV1

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

SerializerV1 output stream for hessian 1.0 requests

func NewSerializerV1

func NewSerializerV1() *SerializerV1

NewSerializerV1 create SerializerV1

func (*SerializerV1) Call

func (o *SerializerV1) Call(m string, args ...interface{}) error

Call Writes a complete method call.

func (*SerializerV1) CompleteCall

func (o *SerializerV1) CompleteCall() error

CompleteCall Completes

func (*SerializerV1) Flush

func (o *SerializerV1) Flush()

Flush clean writer

func (*SerializerV1) Reader

func (o *SerializerV1) Reader() io.Reader

Reader get reader

func (*SerializerV1) SetTypeMap

func (o *SerializerV1) SetTypeMap(typeMap map[string]reflect.Type)

SetTypeMap set type map

func (*SerializerV1) StartCall

func (o *SerializerV1) StartCall() error

StartCall Starts the method call.

func (*SerializerV1) WriteArray

func (o *SerializerV1) WriteArray(arr interface{}) error

WriteArray Write an map value to the stream. The map will be written with the following syntax:

Vt b16 b8 <array-type> l b32 b24 b16 b8 <object> ... ... z

func (*SerializerV1) WriteBool

func (o *SerializerV1) WriteBool(b bool) error

WriteBool Writes a boolean value to the stream. The boolean will be written with the following syntax:

T or F

func (*SerializerV1) WriteBytes

func (o *SerializerV1) WriteBytes(b []byte) error

WriteBytes Writes a bytes value to the stream

func (*SerializerV1) WriteDouble

func (o *SerializerV1) WriteDouble(i float64) error

WriteDouble Writes an double value to the stream. The double will be written with the following syntax:

D b64 b56 b48 b40 b32 b24 b16 b8

func (*SerializerV1) WriteInt

func (o *SerializerV1) WriteInt(i int32) error

WriteInt Writes an integer value to the stream. The integer will be written with the following syntax:

I b32 b24 b16 b8

func (*SerializerV1) WriteLong

func (o *SerializerV1) WriteLong(i int64) error

WriteLong Writes an long value to the stream. The long will be written with the following syntax:

L b64 b56 b48 b40 b32 b24 b16 b8

func (*SerializerV1) WriteMap

func (o *SerializerV1) WriteMap(m interface{}) error

WriteMap Write an map value to the stream. The map will be written with the following syntax:

Mt b16 b8 (<key> <value>)z

func (*SerializerV1) WriteMethod

func (o *SerializerV1) WriteMethod(m string) error

WriteMethod Writes the method tag.

m b16 b8 method-name

func (*SerializerV1) WriteNull

func (o *SerializerV1) WriteNull() error

WriteNull Writes a null value to the stream.

func (*SerializerV1) WriteObject

func (o *SerializerV1) WriteObject(arg interface{}) error

WriteObject Writes any object to the output stream.

func (*SerializerV1) WritePtr

func (o *SerializerV1) WritePtr(p interface{}) error

WritePtr Writes an object value to the stream.

func (*SerializerV1) WriteString

func (o *SerializerV1) WriteString(s string) error

WriteString Writes a string value to the stream using UTF-8 encoding.

The string will be written with the following syntax:

S b16 b8 string-value

func (*SerializerV1) WriteStruct

func (o *SerializerV1) WriteStruct(s interface{}) error

WriteStruct Writes an object value to the stream.

func (*SerializerV1) Writer

func (o *SerializerV1) Writer() io.Writer

Writer get writer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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