vert icon indicating copy to clipboard operation
vert copied to clipboard

AssignTo a map[string]any

Open marcus-wishes opened this issue 2 years ago • 2 comments

Hi, I am trying to get an array of JavaScript objects into go (wasm). The problem is that I don't know the properties of this object, so I figured I could use a map[string]any. But in the any part (which can only be string, number, or []string, []number in that case), I get in the frontend it:

Error: invalid assignment from JS type: object to Go kind: interface

I am not sure how to debug actually in wasm, with the systall/js thing. This is my first contact with wasm.

marcus-wishes avatar Feb 26 '23 22:02 marcus-wishes

Looks like it parses my struct successfully when I make the following edit to the vert code:

// assignToObject assigns an object to a value.
func assignToValue(rv reflect.Value, jv js.Value) (reflect.Value, error) {
	switch k := rv.Kind(); k {
	case reflect.Struct:
		return assignToStruct(rv, jv)
	case reflect.Map:
		return assignToMap(rv, jv)
	case reflect.Slice:
		return assignToSlice(rv, jv)
	case reflect.Interface:
		if e := rv.Elem(); e != zero {
			return assignToInterface(rv, e, jv)
		}
		return zero, nil
	default:
		fmt.Printf("vert: unexpected Go kind: %v, %v, %v\n", k, rv, jv.Type())
		return zero, &InvalidAssignmentError{Type: jv.Type(), Kind: k}
	}
}

where the following part was added by me:

case reflect.Interface:
        if e := rv.Elem(); e != zero {
	        return assignToInterface(rv, e, jv)
        }
        return zero, nil

marcus-wishes avatar Feb 27 '23 09:02 marcus-wishes

that's exact what i want @marcus-wishes

suutaku avatar Dec 16 '23 09:12 suutaku