structpbconv

package module
v0.0.0-...-7d1a317 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2019 License: MIT Imports: 4 Imported by: 1

README

structpbconv

MIT License

Converts structpb.Struct to another structure.

Usage

For example, let us assume to convert Payload of a logging.Entry to an ActivityPayload, which is a basic log format in Google Compute Engine.

The type of Payload of logging.Entry is interface{} but it can be casted to *structpb.Struct in most cases.

The following code converts the instance of *structpb.Struct to an instance of ActivityPayload, which is also defined in that code.

Note that to specify a field name, use structpb tag.

import (
	"github.com/golang/protobuf/ptypes/struct"
	"github.com/jkawamoto/structpbconv"
)


type ActivityPayload struct {
	EventTimestampUs string `structpb:"event_timestamp_us"`
	EventType        string `structpb:"event_type"`
	TraceID          string `structpb:"trace_id"`
	Actor            struct {
		User string
	}
	Resource struct {
		Zone string
		Type string
		ID   string
		Name string
	}
	Version      string
	EventSubtype string `structpb:"event_subtype"`
	Operation    struct {
		Zone string
		Type string
		ID   string
		Name string
	}
}


func NewActivityPayload(payload *structpb.Struct) *ActivityPayload {
	var res ActivityPayload
	structpbconv.Convert(payload, &res)
	return &res
}

License

This software is released under the MIT License, see LICENSE.

Documentation

Overview

Package structpbconv provides a method which converts a structpb.Struct instance to another given structure instance.

For example, let us assume to convert Payload of a logging.Entry to an ActivityPayload, which is a basic log format in Google Compute Engine.

The type of Payload of logging.Entry is `interface{}` but it can be casted to `*structpb.Struct` in most cases.

The following example converts the instance of `*structpb.Struct` to an instance of ActivityPayload, which is also defined in that code.

Note that to specify a field name, use `structpb` tag.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(src *structpb.Struct, dst interface{}) error

Convert converts a structpb.Struct object to a concrete object.

Example
package main

import (
	structpb "github.com/golang/protobuf/ptypes/struct"

	"github.com/jkawamoto/structpbconv"
)

type ActivityPayload struct {
	EventTimestampUs string `structpb:"event_timestamp_us"`
	EventType        string `structpb:"vent_type"`
	TraceID          string `structpb:"trace_id"`
	Actor            struct {
		User string
	}
	Resource struct {
		Zone string
		Type string
		ID   string
		Name string
	}
	Version      string
	EventSubtype string `structpb:"event_subtype"`
	Operation    struct {
		Zone string
		Type string
		ID   string
		Name string
	}
	Items []string
	Map   map[string]string
}

func main() {

	var payload interface{}
	var res *ActivityPayload

	switch s := payload.(type) {
	case *structpb.Struct:
		res = &ActivityPayload{}
		structpbconv.Convert(s, res)
	default:
		// Error: Given payload is not an instance of *structpb.Struct.
	}

}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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