go icon indicating copy to clipboard operation
go copied to clipboard

x/mobile/bind: support slices of supported structs

Open scisci opened this issue 9 years ago • 32 comments
trafficstars

Just want to know if it will ever be possible to support slices of supported. I currently have to write a wrapper object around an array to pass it back and forth between IOS and Go.

scisci avatar Dec 01 '15 20:12 scisci

/cc @crawshaw @hyangah

bradfitz avatar Dec 02 '15 02:12 bradfitz

It will not be impossible and eventually it should be supported. I don't know anyone who's currently working on this problem.

hyangah avatar Dec 03 '15 20:12 hyangah

yeah this is the most frustrating issue for me right now

dcu avatar Jan 05 '16 19:01 dcu

Do you have any ideas when this will be done? What I could do?

I tried and failed using: https://golang.org/pkg/container/list/ as an alternative

weitzj avatar Jan 13 '16 21:01 weitzj

+1 Would highly recommend this as currently falling short without this.

ioArchman avatar Jan 21 '16 16:01 ioArchman

This is stopping me from using gomobile in anything worthwhile at the moment

mpiannucci avatar Jan 21 '16 16:01 mpiannucci

@scisci - If you don't mind can you please share across a gist on how you handle the problem currently as said? My requirement would be to return an array/slice of custom modal class objects with 4 string properties.

ioArchman avatar Jan 21 '16 16:01 ioArchman

hi @ioArchman, I made a quick repo here:

https://github.com/scisci/go-mobile-collection

If you think it can work for you, you will probably want to fork it and modify the render.go function to add/remove the methods you want on your collection wrapper. Feel free to make a comment on the project if you have any questions. Its pretty directly adapted from another tutorial on go generics, which you can find here http://www.onebigfluke.com/2014/12/generic-programming-go-generate.html

Keep in mind, I don't consider this a good solution. It feels hacky working with a wrapper on the objective-c/swift side, but it does work. Really would prefer something that feels more natural.

scisci avatar Jan 21 '16 18:01 scisci

Thanks @scisci. Will look into this and try utilizing for now. Agreed on your last point too.

ioArchman avatar Jan 22 '16 04:01 ioArchman

hi @hyangah, do you have any hints on how this would be technically implemented in case someone wanted to attempt it from outside the gomobile team?

scisci avatar Jan 26 '16 05:01 scisci

👍

anacrolix avatar Sep 10 '16 15:09 anacrolix

This is very limiting.

anacrolix avatar Sep 10 '16 15:09 anacrolix

Is this still on the road map? I found this library and would like to use it, but this issue may be keep me from using gomobile.

kmcrawford avatar Feb 14 '17 12:02 kmcrawford

It seems this is a show-stopper for many people. Are there any plans to support this?

kliron avatar Jan 05 '18 19:01 kliron

What's the status of this issue? It has been around for quite a while. It looks like the main show stopper for many people.

CC: @hyangah @crawshaw

4h0q avatar Jun 29 '18 12:06 4h0q

I just ran into this, and it would be great to be able to support slices.

dradtke avatar Sep 14 '18 18:09 dradtke

Just linking the stack overflow question here : https://stackoverflow.com/questions/44382115/golang-gomobile-tool-for-cross-platform-slices-of-struct-return-type

saurabha5 avatar Oct 04 '18 13:10 saurabha5

have any update for this issue?

nhatlee avatar Oct 26 '18 10:10 nhatlee

Will there be any news?

gituser9 avatar May 02 '19 10:05 gituser9

Currently no one is working on the x/mobile project and adding this feature is not a small project.

Not ideal but need to work around by writing a wrapper package for binding, or using what @scisci mentioned for now.

hyangah avatar May 02 '19 15:05 hyangah

Does the project have a future?

gituser9 avatar May 03 '19 11:05 gituser9

It's community supported at this point, so its future depends on you. There are others from the community working on it, but not full time.

bradfitz avatar May 03 '19 14:05 bradfitz

Anyone currently working on this? If not, could you give some pointers where to start with this?

MariusVanDerWijden avatar Apr 21 '20 21:04 MariusVanDerWijden

As a really slow and dumb bypass, you could serialize the data into JSON, pass the string and unserialize it on the other side. Use this only as a last resort, though.

makivlach avatar Sep 22 '20 00:09 makivlach

A workaround from my real project. Maybe will helpful for someone. On Go side:

type Field struct {
    Key       string
    Value     string
    Protected bool
}

type FieldReceiver interface {
    Add(field *Field)
}

func GetEntryFields(receiver FieldReceiver) {
    for _, v := range entry.Values {
        field := Field{
            Key:       v.Key,
            Value:     v.Value.Content,
            Protected: v.Value.Protected}
        receiver.Add(&field)
    }
}

Android side:

val fields = mutableListOf<Field>()
Mylib.getEntryFields() { field ->
    run {
        fields += Field(field.key, field.value, field.protected)
    }
}

vladimir-skorokhodov avatar Oct 11 '21 21:10 vladimir-skorokhodov

A workaround with String as an example:

type StringCollection interface {
	Add(s string) StringCollection
	Get(i int) string
	Size() int
}

// TODO solve this with generics
type StringArray struct {
	items []string
}

func (array StringArray) Add(s string) StringArray {
	array.items = append(array.items, s)
	return array
}

func (array StringArray) Get(i int) string {
	return array.items[i]
}

func (array StringArray) Size() int {
	return len(array.items)
}

Usage in go:

func GetExampleStringArray() *StringArray {
	strings := []string{"example1", "example2"}
	return &StringArray{items: strings}
}

On Android you can use this extension to convert it to a List<String>:

fun StringArray.toStringList(): List<String> {
    val list = mutableListOf<String>()
    for (i in 0 until size()) {
        list.add(get(i))
    }
    return list
}

fun main() {
    GoPackage.getExampleStringArray().toStringList()
}

nrobi144 avatar Sep 09 '22 10:09 nrobi144

Is there any news? I was very surprised that this isn't implemented by default, I need to make very ugly workarounds to make my binding work.

tex0l avatar Dec 05 '22 11:12 tex0l

Is there any news?

coder-free avatar Mar 22 '23 04:03 coder-free

I've implemented most of the work required for slices of structs here: https://github.com/golang/mobile/pull/101

I could certainly use some help on the final stretch though, especially if you've managed to get the gobind test environment working correctly. In particular, I think the last bits necessary are to:

  1. Update the go-side refnum marshalling
  2. Add an Obj-C implementation
  3. Test that it all works

kevmo314 avatar May 05 '24 18:05 kevmo314

Change https://go.dev/cl/583196 mentions this issue: bind: support slices of structs

gopherbot avatar May 05 '24 18:05 gopherbot