I am writing a service for search functionality. when i am passing values in the body to get the specific record i am able to get it based on the struct value only for PHONE.
I am very new to golang.
i need to search the values using all the fields in the struct for ex.phone or firstname or lastname of the patientstruct
my struct is as below
type PatientEntity struct {
ID int64
FirstName string
LastName string
Phone string
}
my code is
func SearchPatientsHandler(res http.ResponseWriter, req *http.Request) {
patient := &PatientEntity{}
if err := json.NewDecoder(req.Body).Decode(patient); err != nil {
panic(err)
}
patients := []*PatientEntity{}
q := datastore.NewQuery(KINDPATIENT).Filter("FirstName =",patient.FirstName)
keys, err := q.GetAll(ctx, &patients)
if err != nil {
// Handle error
return
}
json.NewEncoder(res).Encode(patients)
}
i need to search with all the values of struct .how can i resolve.
If I understand correctly, you're trying to combine filters with OR
. According to the docs, this is not possible:
Cloud Datastore currently only natively supports combining filters with the AND operator.
There's a tracking issue for this, but it's been there for ages with no action.
You can instead issue a separate query for each filter, then merge the results (query for firstname, then query for lastname, then query for phone. Finally: combine all returns results while removing duplicates).
Something along the lines of (pseudocode, but that's the logic for an OR search):
var results []Patients
if firstname != "" {
results = append(results, query("firstname = ", firstname)
}
if lastname != "" {
results = append(results, query("lastname = ", lastname)
}
etc...
removeDuplicates(results)