golang grpc stubs, omit non empty values on marshalling - go

so I have this fields on my struct (autogenerated)
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"-"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Skipping fields if json marshal is super easy, you just set it to -
Is there a way to do a similar trick with grpc? Don't want to output all fields, also don't want to manually nullify some of the fields on the object, even though it's possible
Thanks

Related

Golang reflect get nil pointer underlying type

I would like to know if there is a way to "cast" a nil pointer of a struct into the (empty) struct itself using reflection or anything else.
My method parameters are an empty parent struct as an interface, a field name as a string and the field value as an interface.
The point is to get the real type of the field by going through the parent struct until I find the field matching the given name. I cannot just get the type of the value, because there will be cases where it won't return the correct type (for example a time.Time will be seen as a string).
The method can take any struct as parent, which is why it takes interfaces as parameters.
My problem is that the parent struct that is given is always a empty one (it serves only as a model), with default values. So fields that are pointers to substructs are nil. So to find the field given as parameter, I have to get the underlying substruct instead of the nil pointer to continue the search inside.
I cannot find a way to achieve that, I am not sure if that's even possible.
Any help is welcomed.

Is it possible to map a prefixed list of env values into a map with Viper?

I have a dotenv file in the current format
KEY_PATH=/keys
LOG_LEVEL=WARNING
DB_CUSTOMER1=dbone
DB_CUSTOMER2=dbtwo
I also have a struct in the form of
type MyConfiguration struct {
KeyPath string `mapstructure:"KEY_PATH"`
CustomerDB map[string]string `<???>`
LogLevel string `mapstructure:"LOG_LEVEL"`
}
I'm looking and failing to find a way where I could map config keys taken as DB_CUSTOMER1=val to a map in the form of "CUSTOMER1": "val" either manually (eg: ask Viper for all keys with prefix DB_ and then set them myself) or automatically (but it seems Viper doesn't have a way to extract key/values this way).
I would appreciate any pointers.
Thanks!
spf13/viper predominantly uses mapstructure package to convert between one native Go type to another i.e. when un-marshaling. You need to define an annotation that would cause any unused values to this map. There is an option to collect such reminder values. You need to modify the map to take an interface as
CustomerDB map[string]interface{} `mapstructure:",remain"`
which would collect all your DB_* field values to the map as interface types, which you can type assert to get the required string value.

Remove a field from a struct, transform a struct

From the Google Drive API I get a struct type File. This struct is almost the same as the struct I would like to insert into BigQuery.
The File struct contains an "AppProperties" field that is incompatible with BigQuery:
AppProperties map[string]string `json:"appProperties,omitempty"`
So the end result that I would like to have is the same struct, with the AppProperties field removed. So basically the question is "how to remove a field from a struct" but that does not make sense as the struct values are mutable, but the struct fields are not. At least that is what I understand now.
The only solution I can think of is, copy the File struct definition and omit the AppProperties field and move all the field values over.
What would be the right way to do this in Go?
The only way is to copy struct and omit needless field for you, it's tedious but there is no other way...

In golang what does `something` mean

I have this XML reader struct:
type Recurlyservers struct {
XMLName xml.Name `xml:"servers"`
Version string `xml: "version,attr"`
Svs []server `xml: "server"`
Description string `xml:",innerxml"`
}
What is the meaning of this xml:"servers" or xml: "version,attr"? I don't know what this `` is. I would like to search in Google but I don't know it's name. What is it? and can I use the standard struct without this? Because the XML reading is not working without this.
Those are called field tags. They're used by the xml encoder/decoder to map the property names to the values in the actual data. In your example they're completely necessary because the fields in XML are lower cased, in Go to have fields on a struct exported they have to be upper cased. Since the xml names diverge from the field names on your type, you have to specify what goes where for the encoding package.
This same convention is used from almost all data transformation/encoding/storage libraries.

Similar to .net attributes in Go

What is similar to .net attributes in go lang.
Or how this could be achieved ?
Perhaps the most similar mechanism is Struct Tags. Not the most elegant, but they can be evaluated at runtime and provide metadata on struct members.
From the reflect package documentation: type StructTag
They are used, for example, in JSON and XML encoding for custom element names.
For example, using the standard json package, say I have a struct with a field I don't want to appear in my JSON, another field I want to appear only if it is not empty, and a third one I want to refer to with a different name than the struct's internal name. Here's how you specify it with tags:
type Foo struct {
Bar string `json:"-"` //will not appear in the JSON serialization at all
Baz string `json:",omitempty"` //will only appear in the json if not empty
Gaz string `json:"fuzz"` //will appear with the name fuzz, not Gaz
}
I'm using it to document and validate parameters in REST API calls, among other uses.
If you keep the 'optionally space-separated key:"value"' syntax, you can use the Get Method of StructTag to access the values of individual keys, as in the example.

Resources