Datastore "no such struct field" - google-cloud-datastore

I'm trying to fetch data from my Datastore database, I hit a wall and can't seem to get any further...
What works:
type Foo struct {
X int `json:"x" datastore:"first"`
Y int `json:"y" datastore:"second"`
Z int `json:"z" datastore:"third"`
}
What I want:
type Foo struct {
X int `json:"x" datastore:"first"
Y int `json:"y" datastore:"second"`
Bar []Bars `json:"more"`
}
type Bars struct {
Z int `json:"z" datastore:"third"`
}
I'm getting the error datastore: Cannot load field "third" into "main.Foo": no such struct field.
Can anyone help?

Related

MQL4/5 CList Search method always returning null pointer

I'm trying to use the CList Search method in an application. I have attached a very simple example below.
In this example, I always get a null pointer in the variable result. I tried it in MQL4 and MQL5. Has anyone ever made the Search method work? If so, where is my mistake? With my question, I refer to this implementation of a linked list in MQL (it's the standard implementation). Of course, in my application, I do not want to find the first list item, but items that match specific criteria. But even this trivial example does not work for me.
#property strict
#include <Arrays\List.mqh>
#include <Object.mqh>
class MyType : public CObject {
private:
int val;
public:
MyType(int val);
int GetVal(void);
};
MyType::MyType(int val): val(val) {}
int MyType::GetVal(void) {
return val;
}
void OnStart() {
CList *list = new CList();
list.Add(new MyType(3));
// This returns a valid pointer with
// the correct value
MyType* first = list.GetFirstNode();
// This always returns NULL, even though the list
// contains its first element
MyType* result = list.Search(first);
delete list;
}
CList is a kind of linked list. A classical arraylist is CArrayObj in MQL4/5 with Search() and some other methods. You have to sort the list (so implement virtual int Compare(const CObject *node,const int mode=0) const method) before calling search.
virtual int MyType::Compare(const CObject *node,const int mode=0) const {
MyType *another=(MyType*)node;
return this.val-another.GetVal();
}
void OnStart(){
CArrayObj list=new CArrayObj();
list.Add(new MyType(3));
list.Add(new MyType(4));
list.Sort();
MyType *obj3=new MyType(3), *obj2=new MyType(2);
int index3=list.Search(obj3);//found, 0
int index2=list.Search(obj2);//not found, -1
delete(obj3);
delete(obj2);
}

Indirect of interface type in Go

I'm trying to create a function that will create a new instance of an interface, and assign that instance to a variable that has the type of the interface. Here is a simple example program (which does not compile):
package main
import (
"fmt"
)
type Foo interface {
Foo(int) int
}
type Foo_impl struct {}
func (f *Foo_impl) Foo(x int) int {
return x * 2
}
func main() {
var x *Foo_impl
constructFoo(x)
fmt.Println("Hello, playground")
}
func constructFoo(x Foo) {
*x = Foo_impl{} // Blows up here - invalid indirect of x (type Foo)
}
Is it possible via reflection to indirect an interface variable, and assign to the underlying value? If I were not using interfaces, I would do something like this,
func main() {
var x int
foo(&x)
fmt.Printf("%d\n", x)
}
func foo(x *int) {
*x = 4
}
And as expected, this will print out 4. The issue is that interface variables cannot be indirected in the normal way. Is there a way around this?
But why can't you be more idiomatic and do
func constructFoo() Foo {
return &Foo_impl{}
}
then, in main:
func main() {
fmt.Println(constructFoo().Foo(10))
}
?
Also, there is accept interfaces, return structs approach which may be interesting for you.
Hope this helps a bit.
I was able to write a function that did what I want
package main
import (
"fmt"
"reflect"
)
type Y interface {
SetX(int)
}
type X struct {
test int
}
func (x *X) SetX(param int) {
x.test = param
}
func main() {
var x *X
y := foo(&x)
y.SetX(12)
fmt.Printf("%+v", x)
}
func foo(x interface{}) Y {
t := reflect.TypeOf(x)
pointerType := t.Elem()
realType := pointerType.Elem()
pointer := reflect.New(realType)
reflect.Indirect(reflect.ValueOf(x)).Set(pointer)
return pointer.Interface().(Y)
}
The foo function can initialize any double pointer to a type that implements Y, and it returns the new instance as a Y.
Implementing an interface will help you to pass mock structs to your function and then using type assertion you can get the value of struct. Basically interface is the only way in which you can wrap your any type and pass it to the function and then using type assertions you can get the underlying value.
package main
import (
"fmt"
)
type Foo interface {
Foo(int) int
}
type Foo_impl struct {}
func (f *Foo_impl) Foo(x int) int {
return x * 2
}
func main() {
var x *Foo_impl
constructFoo(x)
}
func constructFoo(x interface{}) {
fmt.Println(x.(interface{}).(*Foo_impl).Foo(10)) // dereference the type to call the function on pointer receiver
}
Also It is required to dereference the value of type struct passed to the constructor to call the method using pointer receiver.
Check working code on Go Playground
In Golang Type assertions is defined as:
For an expression x of interface type and a type T, the primary
expression
x.(T)
asserts that x is not nil and that the value stored in x is of type T.
The notation x.(T) is called a type assertion.
More precisely, if T is not an interface type, x.(T) asserts that the
dynamic type of x is identical to the type T. In this case, T must
implement the (interface) type of x; otherwise the type assertion is
invalid since it is not possible for x to store a value of type T. If
T is an interface type, x.(T) asserts that the dynamic type of x
implements the interface T.
In Go, if we have a type
type Foo_impl struct {}
We usually using
func NewFoo_impl() *Foo_impl
to create this instance of this structure(if need)
There is no instance of the interface, we just say a type implement an interface or not.
So your code can be
var x Foo
x = NewFoo_impl()
// or x = &Foo_impl{}
About indirect the interface type, it's not hard to understand by knowing it just like void* in C.
Dereference it won't return the type you want, in fact, the compiler also doesn't know how to deal with it. It became an incomplete type, so Go's decision is disallowing it.
Here is a solution for your requirements, however a pointer of the type that is being passed to you constructor method can not be nil, one way to address it is to use default instance.
package main
import (
"fmt"
)
var defaultFooImpl = &Foo_impl{}
type Foo interface {
Foo(int) int
}
type Foo_impl struct {
id int
}
func (f *Foo_impl) Foo(x int) int {
return x * 2
}
func main() {
var x *Foo_impl = defaultFooImpl
constructFoo(x)
fmt.Println("Hello, playground %v", x)
}
func constructFoo(x Foo) {
switch value :=x.(type) {
case *Foo_impl:
*value = Foo_impl{2}
}
}
Yet another approach with varadic function that accepts multiple nil pointers to Foo,
package main
import (
"fmt"
)
type Foo interface {
Foo(int) int
}
type Foo_impl struct {
id int
}
func (f *Foo_impl) Foo(x int) int {
return x * 2
}
func main() {
var x *Foo_impl
var x1 = []Foo{x}
constructFoo(x1...)
fmt.Println("Hello, playground %v", x1[0])
}
func constructFoo(x ...Foo) {
for i, foo := range x {
switch (foo).(type) {
case *Foo_impl:
x[i] = &Foo_impl{2}
}
}
}

How can we use the actual type pointed to by an empty interface to create a new instance of that type in Go?

Please check the below program. Var z is of type interface{}. It stores the type of struct X. But I cannot use it to create a new instance of X. I have some requirement where I need to keep the type of the object in an interface{} variable, and use that to create an instance of that type. Here is a link for the snippet on go playground
package main
import (
"fmt"
"reflect"
)
type X struct {
a int
b int
}
type MyInt int
func main() {
x := X{}
y := reflect.TypeOf(x)
fmt.Printf("%v\n", reflect.New(y))
var z interface{}
z = y
fmt.Printf("%v\n", z) // prints main.X
//Below line throws the error
fmt.Printf("%v\n", reflect.New(z)) //----> This line throws error
}
You can use type assertion to extract the reflect.Type value from an interface{} value:
fmt.Printf("%v\n", reflect.New(z.(reflect.Type))) //----> Works!
Your modified example on the Go Playground.
Note that the above example panics if z does not hold a value of type reflect.Type or if it is nil. You can use the special comma-ok form to prevent that:
if t, ok := z.(reflect.Type); ok {
fmt.Printf("%v\n", reflect.New(t)) // t is of type reflect.Type
} else {
fmt.Println("Not the expected type or nil!")
}

SignalR is sending my struct wrongly

So, after trying to understand whats happens with my messages in a big class... i've found out with a small test that:
public struct Test
{
public int X {get;set};
public int Y {get;set};
public Test(int x, int y)
{
X = x;
Y = y;
}
}
// hub
var sendMe = new Test(12,20);
Clients.All.Test(sendMe);
...and client gets Test = (0,0)!
Looks like a big bug.
Do i need to fill a bugreport?
I believe this is because JSon.NET (which is used by SignalR client to deserialize payload) does not handle structs by default. You can change your struct to a class.

How do I create a new vector of Trait from vectors of types that implement Trait?

I would like to create a new vector that contains objects that implement Trait, from some vectors I already have which contain those objects.
trait Foo {
//
}
struct Bar {
i: i32,
}
struct Baz {
c: char,
}
impl Foo for Bar {
//
}
impl Foo for Baz {
//
}
fn main() {
let v1 = vec![Bar{i: 2},Bar{i: 4}];
let v2 = vec![Baz{c: '2'},Baz{c: '4'}];
let mut v_all: Vec<Box<Foo>> = Vec::new();
v_all.extend(v1.into_iter());
v_all.extend(v2.into_iter());
}
This of course gets me
<anon>:34:11: 34:33 error: type mismatch resolving `<collections::vec::IntoIter<Bar> as core::iter::Iterator>::Item == Box<Foo>`: expected struct Bar, found box
<anon>:34 v_all.extend(v1.into_iter());
How could I achieve this, if possible?
Well, if you have a Bar, and you need a Box<Foo>, then you need to first box the value, then cast it to a trait object, which looks like this:
v_all.extend(v1.into_iter().map(|e| Box::new(e) as Box<Foo>));
v_all.extend(v2.into_iter().map(|e| Box::new(e) as Box<Foo>));

Resources