I have this function which takes an unknown amount of input from a struct:
func GetAllXXXByQueryFilters(ctx context.Context, filters ...XXXFilters) ([]XXX, error) {
var allKeys []*datastore.Key
var xxx []XXX
for _, filter := range filters {
query := datastore.NewQuery("XXX")
if filter.Foo != "" {
query = query.Filter("foo =", filter.Foo)
}
if filter.Bar != "" {
query = query.Filter("bar =", filter.Bar)
}
keys, err := models.DSClient().GetAll(ctx, query, &xxx)
if err != nil {
return nil, err
}
allKeys = append(keys) // PROBLEM HERE
}
for i, key := range allKeys {
xxx[i].ID = key.ID
}
return xxx, nil
}
The problem is that allKeys will override previous query results. I have previously overcome the problem by doing:
keys1, err := models.DSClient().GetAll(ctx, query1, &xxx)
keys2, err := models.DSClient().GetAll(ctx, query2, &xxx)
keys = append(keys1, keys2...)
But I wanted my GetAllXXXByQueryFilters to be more sophisticated and avoid duplicated code, and now I can't seem to figure out how I can store the keys from the for _, filter := range filters
loop and then append them all afterwards. So I was hoping that maybe there was a smarter way to do/replace the allKeys = append(keys)
line so that it doesn't override previous keys?