encode

package
v1.90.1 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package encode provides serialization and deserialization functions to safely transmit, store and retrieve data between different systems (e.g., databases, queues, caches, etc.).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteDecode

func ByteDecode(msg []byte, data any) error

ByteDecode decodes a byte slice message encoded with the ByteEncode function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

func ByteDeserialize

func ByteDeserialize(msg []byte, data any) error

ByteDeserialize decodes a string message encoded with the Serialize function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

func ByteEncode

func ByteEncode(data any) ([]byte, error)

ByteEncode encodes the input data to gob+base64 byte slice.

func ByteSerialize

func ByteSerialize(data any) ([]byte, error)

ByteSerialize encodes the input data to JSON+base64 byte slice.

func Decode

func Decode(msg string, data any) error

Decode decodes a string message encoded with the Encode function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/encode"
)

func main() {
	type TestData struct {
		Alpha string
		Beta  int
	}

	var data TestData

	msg := "Kf+BAwEBCFRlc3REYXRhAf+CAAECAQVBbHBoYQEMAAEEQmV0YQEEAAAAD/+CAQZhYmMxMjMB/gLtAA=="

	err := encode.Decode(msg, &data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(data)

}
Output:

{abc123 -375}

func Deserialize

func Deserialize(msg string, data any) error

Deserialize decodes a byte slice message encoded with the Serialize function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/encode"
)

func main() {
	type TestData struct {
		Alpha string
		Beta  int
	}

	var data TestData

	msg := "eyJBbHBoYSI6ImFiYzEyMyIsIkJldGEiOi0zNzV9Cg=="

	err := encode.Deserialize(msg, &data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(data)

}
Output:

{abc123 -375}

func Encode

func Encode(data any) (string, error)

Encode encodes the input data to gob+base64 string.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/encode"
)

func main() {
	type TestData struct {
		Alpha string
		Beta  int
	}

	data := &TestData{Alpha: "test_string", Beta: -9876}

	v, err := encode.Encode(data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(v)
}
Output:

func Serialize

func Serialize(data any) (string, error)

Serialize encodes the input data to JSON+base64 string.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/encode"
)

func main() {
	type TestData struct {
		Alpha string
		Beta  int
	}

	data := &TestData{Alpha: "test_string", Beta: -9876}

	v, err := encode.Serialize(data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(v)

}
Output:

eyJBbHBoYSI6InRlc3Rfc3RyaW5nIiwiQmV0YSI6LTk4NzZ9Cg==

Types

This section is empty.

Jump to

Keyboard shortcuts

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