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

scale decoding of strings in structs is not working #370

Closed
noot opened this issue Nov 1, 2019 · 0 comments · Fixed by #371
Closed

scale decoding of strings in structs is not working #370

noot opened this issue Nov 1, 2019 · 0 comments · Fixed by #371

Comments

@noot
Copy link
Contributor

noot commented Nov 1, 2019

Currently, trying to use scale to decode structs where fields are strings or arrays of strings doesn't work.

Strings on their own (not in struct) works for encoding/decoding and encoding strings in structs is working fine.

eg. if we run this from the codec package:

func TestEncodeAndDecodeStringInStruct(t *testing.T) {
	test := &struct{
		A string
	} {
		A: "noot",
	}

	enc, err := Encode(test)
	if err != nil {
		t.Fatal(err)
	}

	dec, err := Decode(enc, &struct{A string}{A: ""})
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(test, dec) {
		t.Fatalf("Fail: got %v expected %v", dec, test)
	}
}

This will not work, the resulting A field will be empty: Fail: got &{} expected &{noot}

A workaround right now is to cast all strings in a struct to []byte then use scale, which works fine.

for example, this works:

func TestEncodeAndDecodeByteArrayInStruct(t *testing.T) {
	test := &struct{
		A []byte
	} {
		A: []byte("noot"),
	}

	enc, err := Encode(test)
	if err != nil {
		t.Fatal(err)
	}

	dec, err := Decode(enc, &struct{A []byte}{A: []byte{}})
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(test, dec) {
		t.Fatalf("Fail: got %v expected %v", dec, test)
	}
}
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

Successfully merging a pull request may close this issue.

1 participant