Skip to main content

Go

Go Reference

Stencil go client package provides a store to lookup protobuf descriptors and options to keep the protobuf descriptors upto date.

It has following features

  • Deserialize protobuf messages directly by specifying protobuf message name
  • Serialize data by specifying protobuf message name
  • Ability to refresh protobuf descriptors in specified intervals
  • Support to download descriptors from multiple urls

Requirements

  • go 1.16

Installation

Use go get

go get github.com/raystack/stencil/clients/go

Then import the stencil package into your own code as mentioned below

import stencil "github.com/raystack/stencil/clients/go"

Usage

Creating a client

import stencil "github.com/raystack/stencil/clients/go"

url := "http://localhost:8000/v1beta1/namespaces/{test-namespace}/schemas/{schema-name}"
client, err := stencil.NewClient([]string{url}, stencil.Options{})

Get Descriptor

import stencil "github.com/raystack/stencil/clients/go"

url := "http://localhost:8000/v1beta1/namespaces/{test-namespace}/schemas/{schema-name}"
client, err := stencil.NewClient([]string{url}, stencil.Options{})
if err != nil {
return
}
desc, err := client.GetDescriptor("google.protobuf.DescriptorProto")

Parse protobuf message.

import stencil "github.com/raystack/stencil/clients/go"

url := "http://localhost:8000/v1beta1/namespaces/{test-namespace}/schemas/{schema-name}"
client, err := stencil.NewClient([]string{url}, stencil.Options{})
if err != nil {
return
}
data := []byte("")
parsedMsg, err := client.Parse("google.protobuf.DescriptorProto", data)

Serialize data.

import stencil "github.com/raystack/stencil/clients/go"

url := "http://url/to/proto/descriptorset/file"
client, err := stencil.NewClient([]string{url}, stencil.Options{})
if err != nil {
return
}
data := map[string]interface{}{}
serializedMsg, err := client.Serialize("google.protobuf.DescriptorProto", data)

Enable auto refresh of schemas

import stencil "github.com/raystack/stencil/clients/go"

url := "http://localhost:8000/v1beta1/namespaces/{test-namespace}/schemas/{schema-name}"
// Configured to refresh schema every 12 hours
client, err := stencil.NewClient([]string{url}, stencil.Options{AutoRefresh: true, RefreshInterval: time.Hours * 12})
if err != nil {
return
}
desc, err := client.GetDescriptor("google.protobuf.DescriptorProto")

Using VersionBasedRefresh strategy

import stencil "github.com/raystack/stencil/clients/go"

url := "http://localhost:8000/v1beta1/namespaces/{test-namespace}/schemas/{schema-name}"
// Configured to refresh schema every 12 hours
client, err := stencil.NewClient([]string{url}, stencil.Options{AutoRefresh: true, RefreshInterval: time.Hours * 12, RefreshStrategy: stencil.VersionBasedRefresh})
if err != nil {
return
}
desc, err := client.GetDescriptor("google.protobuf.DescriptorProto")

Refer to go documentation for all available methods and options.