This may require reflection but I'm not sure. I am trying to loop through an array of required fields in a struct. If any of those fields are nil I want to throw an error essentially. I've got the basic form down but I realized I don't know how in Go to pass a struct field name via a variabel
imagine you have a struct called EmailTemplate and it has a field called template_id
In this case I want to know if EmailTemplate.TemplateId is nil
emailDef.Fields is a string array ["TemplateId"]
I want to check if those fields are in the EmailTemplate struct and if they are nil
for field := range emailDef.Fields {
fmt.Println(emailDef.Fields[field])
if EmailTemplate.[emailDef.Fields[field]] == nil {
missingField := true
}
}
is along the lines of what I am thinking but I know that is wrong as a struct isn't an array. emailDef.Fields[field] would be equivalent to TemplateId
Your loop stuff doesn't make a whole lot of sense to me so I'll give a general example with a single field in a string called field. If you have a slice or array of fields you want to check for then you'll want to range over that using the current value for field.
import "reflect"
st := reflect.TypeOf(EmailTemplate)
v, ok := st.FieldByName(field)
if ok {
// field existed on EmailTemplate, now check if it's nil
if v.IsNil() {
// the field field on instance EmailTemplate was nil, do something
}
}
Now assuming you have a list of fields you need to check are all non-nil, then just add a loop like so;
for field := range requiredFields {
st := reflect.TypeOf(EmailTemplate)
v, ok := st.FieldByName(field)
if ok {
// field existed on EmailTemplate, now check if it's nil
if v.IsNil() {
// the field field on instance EmailTemplate was nil, do something
// maybe raise error since the field was nil
} else {
//the field wasn't found at all, probably time to raise an error
}
}
}
reflect package docs are here; https://golang.org/pkg/reflect/
Related
I connect BigQuery with Go language as the following API documentation demonstrates,
https://cloud.google.com/bigquery/docs/reference/libraries?hl=en_US
After that, I need to get sql results in specific row and column, and judge if it equals a specific string. Could I change bigquery.Value to string and how to do that?
See how to use RowIterator.Next() here.
Next loads the next row into dst. Its return value is iterator.Done if there are no more results. Once Next returns iterator.Done, all subsequent calls will return iterator.Done.
dst may implement ValueLoader, or may be a *[]Value, *map[string]Value, or struct pointer.
Value is of type interface{} so if you are sure that value you have is string str := fmt.Sprintf("%v", row[i]) should work. It is often better to define a struct type that has members representing fields of query result row (with types mapped according to the table in documentation I linked above) and give the pointer to it to RowIterator.Next() instead of slice/map of bigquery.Value.
type myRow struct {
Name string
Num int
}
// ...
q := client.Query("select name, num from t1")
it, err := q.Read(ctx)
// handle err
for {
// instead of: var row []bigquery.Value
var row myRow // <-- use custom struct type here
err := it.Next(&row)
if err == iterator.Done {
break
}
// handle err != nil
someFuncThatTakesString(row.Name)
}
I have the following code snippet that after going some reflection, it sets a struct's field to a string value
switch fType := v.(type) {
case MyCompositeFlagString:
s, ok := userInput.(string)
if !ok {
log.Printf("Erroneous input type:%T and input value: %v\n", userInput, userInput)
return ErrUnexpectedInput
}
valueField := values.Elem().Field(i).FieldByName("MyFlagString").FieldByName("Value")
valueField.SetString(s)
I don't see any SetSlice method in reflect package.
How can I perform the above operation when the valueField is of type []string ?
Value.SetString() is a convenience method for setting string values. There isn't a separate method for all types for obvious reasons, but there is a "generic" Value.Set() method, you may use that. You just have to obtain a reflect.Value from the value you want to set:
var someSlice ...
valueField.Set(reflect.ValueOf(someSlice))
I would like to iterate over the fields of a struct after unmarshalling a JSON object into it and check for the fields whose value was not set (i.e. are empty).
I can get the value of each field and compare that to the reflect.Zero value for the corresponding type
json.Unmarshal([]byte(str), &res)
s := reflect.ValueOf(&res).Elem()
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
v := reflect.ValueOf(f.Interface())
if (reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())) {
....
But the problem, of course, is that this will not work well for bool or int values.
If a bool field is set to false in the JSON or an int field is set to 0, they will be equal to the zero value of their type. The aforementioned check will consider the fields to be uninitialized, even though they actually have a value set.
I know one way around this is to use pointers, but I just don't see how that would be possible in this case as I'm working with reflect.Value types, not the actual struct.
As you've mentioned, you could use pointers.
The json package can handle unmarshalling values into pointers for you. You've not included the json payload you are trying to unmarshal, or the struct you are unmarshalling into, so I've made up an example.
// json
{
"foo": true,
"number_of_foos": 14
}
// go struct
type Foo struct {
Present bool `json:"foo"`
Num int `json:"number_of_foos"`
}
Here if the keys foo or number_of_foos is missing, then as you've correctly observed, the zero value (false/ 0) will be used. In general the best advice is to make use of the zero value. Create structures so that zero values of false are useful, rather than a pain.
This is not always possible, so changing the types of the fields in the Foo struct to be pointers will allow you to check the 3 cases you are after.
Present
Present and zero
Missing
here is the same struct with pointers:
// go struct
type Foo struct {
Present *bool `json:"foo"`
Num *int `json:"number_of_foos"`
}
Now you can check for presence of the value with fooStruct.Present != nil and if that condition holds, you can assume that the value in the field is the one you wanted.
There is no need to use the reflect package.
Another way of doing the same is by implementing json.Unmarshaler.
type MaybeInt struct {
Present bool
Value int
}
func (i *MaybeInt) UnmarshalJSON(bs []byte) error {
if e := json.Unmarshal(bs, &i.Value); e != nil {
return e
}
i.Present = true
return nil
}
You can then use MaybeInt in your top-level structure:
type Top struct {
N MaybeInt `json:"n"`
M MaybeInt `json:"m"`
}
func main() {
t := Top{}
if e := json.Unmarshal([]byte(` { "n": 4930 } `), &t); e != nil {
panic(e)
}
fmt.Println(t.N, t.M)
}
See it working on the playground
Try using the golang validator package. The validator package offers a required attribute that might do the required job for your need. The official documentation for required attribute states:
This validates that the value is not the data types default zero value. For numbers ensures value is not zero. For strings ensures value is not "". For slices, maps, pointers, interfaces, channels and functions ensures the value is not nil.
The example illustrating the same can be seen at: https://github.com/go-playground/validator/blob/v9/_examples/struct-level/main.go.
Hope this solves your requirement.
I have a function in Go, the return value of which I hope to encode using gob. The return value is a struct pointer. However even though I do understand what exported variables are, I am not quite sure how to get it working.
Here is what my function is like
fun loadXYZ(root *structABC) *structABC{
const once = "stateData.bin"
rd, errr := ioutil.ReadFile(once)
if errr!=nil{
//Do some computation and store in "root"
buf := &bytes.Buffer{}
errr = gob.NewEncoder(buf).Encode(root)
if errr != nil {
panic(errr)
}
errr = ioutil.WriteFile(once, buf.Bytes(), 0666)
if errr != nil {
panic(errr)
}
return root
}
var d *structABC
errr = gob.NewDecoder(bytes.NewReader(rd)).Decode(&d)
if errr != nil {
panic(errr)
}
return d
}
This is the error I get
panic: gob: type main.stateNode has no exported fields
I know why the error is occurring. But can someone help me solve it?
In go, fields and variables that start with an Uppercase letter are "Exported", and are visible to other packages. Fields that start with a lowercase letter are "unexported", and are only visible inside their own package.
The encoding/gob package depends on reflection to encode values, and can only see exported struct fields.
In order to make things encodable, capitalize the first letter of each field name in your stateNode struct that you want to be saved.
Exported field it's a filed which name started with capitalized char like:
type stateNode struct {
ImExported string // Exported
butImNot string // unexported
}
I'm writing code that allows data access from a database. However, I find myself repeating the same code for similar types and fields. How can I write generic functions for the same?
e.g. what I want to achieve ...
type Person{FirstName string}
type Company{Industry string}
getItems(typ string, field string, val string) ([]interface{}) {
...
}
var persons []Person
persons = getItems("Person", "FirstName", "John")
var companies []Company
cs = getItems("Company", "Industry", "Software")
So you're definitely on the right track with the idea of returning a slice of nil interface types. However, you're going to run into problems when you try accessing specific members or calling specific methods, because you're not going to know what type you're looking for. This is where type assertions are going to come in very handy. To extend your code a bit:
getPerson(typ string, field string, val string) []Person {
slice := getItems(typ, field, val)
output := make([]Person, 0)
i := 0
for _, item := range slice {
// Type assertion!
thing, ok := item.(Person)
if ok {
output = append(output, thing)
i++
}
}
return output
}
So what that does is it performs a generic search, and then weeds out only those items which are of the correct type. Specifically, the type assertion:
thing, ok := item.(Person)
checks to see if the variable item is of type Person, and if it is, it returns the value and true, otherwise it returns nil and false (thus checking ok tells us if the assertion succeeded).
You can actually, if you want, take this a step further, and define the getItems() function in terms of another boolean function. Basically the idea would be to have getItems() run the function pass it on each element in the database and only add that element to the results if running the function on the element returns true:
getItem(critera func(interface{})bool) []interface{} {
output := make([]interface{}, 0)
foreach _, item := range database {
if criteria(item) {
output = append(output, item)
}
}
}
(honestly, if it were me, I'd do a hybrid of the two which accepts a criteria function but also accepts the field and value strings)
joshlf13 has a great answer. I'd expand a little on it though to maintain some additional type safety. instead of a critera function I would use a collector function.
// typed output array no interfaces
output := []string{}
// collector that populates our output array as needed
func collect(i interface{}) {
// The only non typesafe part of the program is limited to this function
if val, ok := i.(string); ok {
output = append(output, val)
}
}
// getItem uses the collector
func getItem(collect func(interface{})) {
foreach _, item := range database {
collect(item)
}
}
getItem(collect) // perform our get and populate the output array from above.
This has the benefit of not requiring you to loop through your interface{} slice after a call to getItems and do yet another cast.