Skip to content

flowup-labs/grpc-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 

Repository files navigation

runtime

JSON marshal

JSON marshal is a combination of golang jsonbp and JSONBuiltin marshaler. This marshaler doesn't ignore json tags such as json:"myName" or json:"-"

⚠️ don't forget to import gogoproto in proto file

import "github.com/gogo/protobuf/gogoproto/gogo.proto";

RemoveEmpty

RemoveEmpty overrides EmitDefaults option. It can be used to remove json:"something,omitempty" tags generated by some generators

usage:

in main file

mux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONCustom{RemoveEmpty:true}))

proto file

message User{
    name string = 1 [(gogoproto.jsontag) = "NAME,removeEmpty"];
}

Nested

This feature can be used to nest fields in the resulting JSON while maintaining a flat structure in code.

⚠️ working only with structures generated from proto (use jsonproto marshaller)

Marshall:

Usage:

proto file

message Msg {
    Firstname string = 1 [(gogoproto.jsontag) = "name.firstname"];
    PseudoFirstname string = 2 [(gogoproto.jsontag) = "lastname"];
    EmbedMsg = 3  [(gogoproto.nullable) = false, (gogoproto.embed) = true];
    Lastname string = 4 [(gogoproto.jsontag) = "name.lastname"];
    Inside string  = 5 [(gogoproto.jsontag) = "name.inside.a.b.c"];
}

message EmbedMsg{
    Opt1 string = 1 [(gogoproto.jsontag) = "opt1"];
}
In your code:
msgInput := Msg{
    Firstname:       "One",
	Lastname:        "Two",
	PseudoFirstname: "Three",
	EmbedMsg: EmbedMsg{
		Opt1: "var",
	},
	Inside: "goo",
}
Output:
{
	"lastname": "Three",
	"name": {
		"firstname": "One",
		"inside": {
			"a": {
				"b": {
					"c": "goo"
				}
			}
		},
		"lastname": "Two"
	},
	"opt1": "var"
}

context

IncomingToOutgoingHeaderKey transforms incoming gRPC context into outgoing. It can be used when proxying gRPC requests.

usage:
someClient.someMethod(IncomingToOutgoingContext(ctx), &someMsg{})