How to desialize Aerospike record in Go? - go

I am having quite complex schema in aerospike:-
DATA SCHEMA:
bin name: user_ids
Type: List of Strings
bin name: user_w
Type: List of Integers
bin name: users
Type: map<String<List>> where list is again list(size 3) of lists each of type String
I am able to read this schema directly into Java object with following data structure:-
userIds = (List<String>) r.getList("user_ids");
userWeights = (List<String>) r.getList("user_w");
users = (Map<String, List>) r.getValue("users");
However my following go struct is not able to retrieve it. Its coming as empty. Is something wrong with the struct schema?
type AudienceRecord struct {
user_ids []string
user_w []int64
users map[string][][]string
}

Your user_w schema is either List of integers, or list of strings ?
Because your java and go schemas aren't equivalent here.
That is why Go struct is not able to parse your aerospike data.

Related

How to query in GraphQL with no fixed input object contents?

I want to query to get a string, for which I have to provide a input object, however the input data can have multiple optional keys and quite grow large as well. So I just wanted something like Object as data type while defining schema.
// example of supposed schema
input SampleInput {
id: ID
data: Object // such thing not exist, wanted something like this.
}
type Query {
myquery(input: SampleInput!): String
}
here data input can be quite large so I do not want to define a type for it. is there a way around this?

Query on Array of strings to get matched values from Datastore

I have following scenario where struct is
type Band struct {
Name string `json:"name"`
Albums []String `json:"album"`
GradeLevel []string `json:"gradeLevel,omitempty"`
Topics []string `json:"topics,omitempty"`
}
Data stored like (Sample)
**Name Albums GradeLevel Topics**
Sample ["sample","Test"] ["grade1"] ["Children","Poems"]
test ["Test"] ["grade2","grade1"] ["therapy","slow"]
Here how to query to get appropriate values from Band kind with given inputs like
Request to query is {"album" : ["sample","Test"] , "gradeLevel" : ["grade1"] , "topic" : ["poem"]}
With combination of inputs are possible from front-end so how to query based input to display list of values for above scenario.
You can find information on writing queries in Go to retrieve data from Firestore in Datastore mode here [1]. To query if an array contains a value, you need to use an equality filter [2].
[1] https://cloud.google.com/datastore/docs/concepts/queries
[2] https://cloud.google.com/datastore/docs/concepts/queries#array_values

Serialize to JSON dynamic structure

All examples of working with JSON describe how to serialize to JSON simple or user types (like a struct).
But I have a different case: a) I don't know the fields of my type/object b) every object will have different types.
Here is my case in pseudocode:
while `select * from item` do
while `select fieldname, fieldvalue from fields where fields.itemid = item.id` do
...
For each entity in my database I get field names and field values. In the result I need to get something like this:
{
"item.field1": value,
...
"item.fieldN": value,
"custom_fields": {
"fields.field1": value,
...
"fields.fieldK": value
}
}
What is the best way to do it in Go? Is there any useful libraries or functions in standard library ?
Update: The source of data is the database. In the result i need to get JSON as string to POST it to external web service. So, the program just read data from database and make POST requests to REST service.
What exactly is your target type supposed to be? It can't be a struct since you do not know the fields beforehand.
The only fitting type to me seems to be a map of type map[string]interface{}: with it any nested structure can be achieved:
a := map[string]interface{}{
"item.field1": "val1",
"item.field2": "val2",
"item.fieldN": "valN",
"custom_fields": map[string]interface{}{
"fields.field1": "cval1",
"fields.field2": "cval2",
},
}
b, err := json.Marshal(a)
See playground sample here.
Filling this structure from a database as you hinted at should probably be a custom script (not using json).
Note: custom_fields can also be of other types depending on what type the value column is in the database. If the value column is a string use map[string]string.

How to save struct based type with a map property into mongodb

I want to use mongodb as session storage and save a struct based data type into mongodb.
The struct type looks like:
type Session struct {
Id string
Data map[string]interface{}
}
And create reference to Session struct type and put some data into properties like:
type Authen struct {
Name, Email string
}
a := &Authen{Name: "Foo", Email: "foo#example.com"}
s := &Session{}
s.Id = "555555"
s.Data["logged"] = a
How to save the session data s into mongodb and how to query those data back and save into a reference again?
I think the problem can occurs with the data property of type map[string]interface{}.
As driver to mongodb I would use mgo
There's nothing special to be done for inserts. Just insert that session value into the database as usual, and the map type will be properly inserted:
err := collection.Insert(&session)
Assuming the structure described, this will insert the following document into the database:
{id: "555555", data: {logged: {name: "foo", email: "foo#example.com"}}}
You cannot easily query it back like that, though, because the map[string]interface{} does not give the bson package a good hint about what the value type is (it'll end up as a map, instead of Authen). To workaround this, you'd need to implement the bson.Setter interface in the type used by the Data field.

Getting ID from Google's datastore using Golang

I have a Go struct:
type Foo struct{
Name: string
Age: int
}
I am able to save and retrieve Foos in datastore.
So I may edit a Foo, I want to get the ID that datastore assigns a Foo, to use in an anchor.
I verified that IDs are assigned using Google's datastore dashboard, but how do I get the ID off of a retrieved Foo?
GetAll returns the keys along with any error.
keys, err := q.GetAll(c, &foos)
keys is an array of datastore.Key

Resources