encoder

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2022 License: MIT Imports: 7 Imported by: 0

README

encoder

create multiple encoder object for encode and decode data and an example for register pattern.

install

go get -u github.com/Ja7ad/encoder

usage

example usage for encoder :

package main

import (
	"fmt"
	"log"
	"github.com/Ja7ad/encoder"
)

type Person struct {
	Name    string `json:"name" bson:"name"`
	Age     int    `json:"age" bson:"age"`
	Address string `json:"address" bson:"address"`
}

func main() {
	enc := New()
	enc.RegisterEncoder(JSON, &JsonEncoder{})
	enc.RegisterEncoder(GOB, &GobEncoder{})
	enc.RegisterEncoder(BSON, &BsonEncoder{})
	enc.RegisterEncoder(PROTO, &ProtoEncoder{})

	p := &Person{}

	jsonEnc, err := enc.GetJsonEncoder()
	if err != nil {
		log.Fatal(err)
	}

	b, err := jsonEnc.Encode(&Person{
		Name:    "Javad",
		Age:     30,
		Address: "example address 1",
	})

	if err != nil {
		log.Fatal(err)
	}

	if err = jsonEnc.Decode(b, p); err != nil {
		log.Fatal(err)
	}

	fmt.Println(p)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BsonEncoder

type BsonEncoder struct{}

func (*BsonEncoder) Decode

func (*BsonEncoder) Decode(data []byte, vPtr interface{}) error

func (*BsonEncoder) Encode

func (*BsonEncoder) Encode(vPtr interface{}) ([]byte, error)

type Encode

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

func New

func New() *Encode

New create encode object

func (*Encode) GetBsonEncoder

func (e *Encode) GetBsonEncoder() (Encoder, error)

func (*Encode) GetGobEncoder

func (e *Encode) GetGobEncoder() (Encoder, error)

func (*Encode) GetJsonEncoder

func (e *Encode) GetJsonEncoder() (Encoder, error)

func (*Encode) GetProtoEncoder

func (e *Encode) GetProtoEncoder() (Encoder, error)

func (*Encode) RegisterEncoder

func (e *Encode) RegisterEncoder(encoderType EncodeType, encoder Encoder)

RegisterEncoder register encode base on EncodeType and Encoder interface

type EncodeType

type EncodeType int
const (
	JSON  EncodeType = iota // JSON is encoder and decoder for json
	GOB                     // GOB is encoder and decoder for gob
	BSON                    // BSON is encoder and decoder for mongodb bson
	PROTO                   // PROTO is encoder and decoder for proto reflect
)

type Encoder

type Encoder interface {
	Encode(data interface{}) ([]byte, error)
	Decode(data []byte, vPtr interface{}) error
}

type GobEncoder

type GobEncoder struct{}

func (*GobEncoder) Decode

func (*GobEncoder) Decode(data []byte, vPtr interface{}) error

func (*GobEncoder) Encode

func (*GobEncoder) Encode(vPtr interface{}) ([]byte, error)

type JsonEncoder

type JsonEncoder struct{}

func (*JsonEncoder) Decode

func (*JsonEncoder) Decode(data []byte, vPtr interface{}) error

func (*JsonEncoder) Encode

func (*JsonEncoder) Encode(vPtr interface{}) ([]byte, error)

type ProtoEncoder

type ProtoEncoder struct{}

func (*ProtoEncoder) Decode

func (*ProtoEncoder) Decode(data []byte, vPtr interface{}) error

func (*ProtoEncoder) Encode

func (*ProtoEncoder) Encode(vPtr interface{}) ([]byte, error)

Jump to

Keyboard shortcuts

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