Skip to content
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

panic: reflect: reflect.Value.SetString using unaddressable value #336

Open
VibhorGupta1991 opened this issue May 25, 2022 · 1 comment
Open

Comments

@VibhorGupta1991
Copy link

VibhorGupta1991 commented May 25, 2022

HI,

Facing an issue while using the plugin to marshal / unmarshal a non initialized empty struct (only declared)

Below code shows multiple type of objects getting marshalled / unmarshalled using the library.

We have been trying to use the test2 function approach and getting the following panic:
panic: reflect: reflect.Value.SetString using unaddressable value

Can someone please confirm on the following:

  1. Is it expected to break (if yes why) or is it a bug which we are planning to fix/handle in the near future?
  2. If its expected to break then anyway we can handle and make it work at our end?
  3. Can some conceptual understanding be provided on why is the plugin written in a way that it works for all the other cases but not only for test2.
package main

import (
	"fmt"
	"github.com/vmihailenco/msgpack/v5"
)

type ABC struct {
	SampleInt    int    `msgpack:"Abc"`
	SampleBool   bool   `msgpack:"Xyz"`
	SampleString string `msgpack:"Efg"`
}

func main() {
	//test1() //works
	test2() //doesn't work
	//test3() //works
	//test4() //works
}

func test1() {
	var test ABC
	var test2 ABC
	max := marshal(&test, &test2)

	var test5 ABC
	var test6 ABC
	unmarshal(max, &test5, &test6)
	fmt.Println(test5, test6)
}

func test2() {
	var test []ABC
	var test2 []ABC
	max := marshal(&test, &test2)

	var test5 []ABC
	var test6 []ABC
	unmarshal(max, &test5, &test6)
	fmt.Println(test5, test6)
}

func test3() {

	test := []ABC{}
	test2 := []ABC{}
	max := marshal(&test, &test2)

	test5 := []ABC{}
	test6 := []ABC{}
	unmarshal(max, &test5, &test6)
	fmt.Println(test5, test6)
}

func test4() {

	abc1 := ABC{100, false, "Full-Stack Developer"}
	abc2 := ABC{100, false, "Full-Stack Developer"}

	test := []ABC{abc1, abc2}
	test2 := []ABC{abc1, abc2}
	max := marshal(&test, &test2)

	test5 := []ABC{}
	test6 := []ABC{}
	unmarshal(max, &test5, &test6)
	fmt.Println(test5, test6)
}

func marshal(test ...interface{}) string {
	a, _ := msgpack.Marshal(test)
	return string(a)
}

func unmarshal(marsh string, dest ...interface{}) {
	if dest != nil {
		err := msgpack.Unmarshal([]byte(marsh), &dest)
		fmt.Println(err)
	}
}

Hoping to hear from you soon.

Thanks in advance.

@VibhorGupta1991
Copy link
Author

Any help will be appreciated.

Thanks!

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

No branches or pull requests

1 participant