Skip to content

Question: Pointers with js tags #504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
flimzy opened this issue Aug 23, 2016 · 2 comments
Open

Question: Pointers with js tags #504

flimzy opened this issue Aug 23, 2016 · 2 comments

Comments

@flimzy
Copy link
Member

flimzy commented Aug 23, 2016

Question:

Is code like the following expected to work?

type X struct {
    *js.Object
    Baz *string `js:"baz"`
}

Of course the reason I ask is that it's not working. But then, I'm not really sure what I would expect it to do. So it seems quite plausible that it's not meant to work. In that case, perhaps a more graceful error message would be my feature request.

At the moment, if I attempt to assign something to X.Baz, then try to retrieve it, I get:

TypeError: Cannot assign to read only property '0' of [object String]

And if I try to read from it before it's initialized, I get:

TypeError: Cannot read property 'constructor' of undefined

Which is the same behavior I get if I attempt to read an uninitialized array with a js tag. But I can initialize the array first, and then things work.

My test code is as follows:

package main

import (
    "fmt"
    "github.com/gopherjs/gopherjs/js"
)

type X struct {
    *js.Object
    Baz *string `js:"baz"`
}

func main() {
    o := js.Global.Get("Object").New()
    x := &X{Object: o}
// Uncomment the following two lines to attempt assignment
//  var oink string = "oink"
//  x.Baz = &oink
    fmt.Printf("x = %s\n", *x.Baz)
}
@dmitshur
Copy link
Member

Thanks for reporting this @flimzy.

It's definitely a bug/issue if it's not documented and doesn't work. We should fix it either via documentation and more graceful error message, or by actually implementing it.

I haven't looked to closely at this yet, I plan to do that sometime. At a glance, I suspect we shouldn't try to support this... But I might be missing important details.

Is there a good reason to use pointers rather than values for fields? I'm only asking to understand this issue better.

@dmitshur dmitshur added the bug label Aug 26, 2016
@flimzy
Copy link
Member Author

flimzy commented Aug 26, 2016

The reason I was experimenting with it is that I wanted to use nullable values in a struct. Of course, JS objects already support this in a way that Go does not, as JS distinguishes between undefined and false, undefined and 0, undefined and "", etc.

Ultimately, in my case, I decided that the cleanest solution was a "pure" Go struct (as in, no embedded *js.Object, and no js tags), with a .compile() method which does an intelligent conversion to a map[string]interface{} to be passed to the JS method I was wrapping.

I haven't spent a lot of effort thinking about what I would expect should happen with pointers + js tags, or whether there is a legitimate use case for it. Although I haven't tested, I expect something similar would happen with an interface with a js tag, which might make sense in some cases, such as:

type Foo struct {
    *js.Object
    Error error `js:"error"`
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants