gorilla/schema

[question] Unable to decode slice of structs

ankorGH opened this issue · 1 comments

Describe the bug

A clear and concise description of what the bug is.

Hi, thanks for the project. It has been a lifesaver. I am unable to decode a slice of struct after implementing a TextUnmarshaler on that struct.

Versions

Go version: go version go1.16.3 darwin/arm64
package version: run git rev-parse HEAD inside the repo
schema v1.2.0

Steps to Reproduce

How can the bug be triggered?
Run the code snippet below

Expected behavior

What output or behaviour were you expecting instead?
Output should be struct F with a Slice of Addresses, {["Lagos", "Accra"]}

Code Snippets

A minimum viable code snippet can be useful! (use backticks to format it).

package main

import (
	"fmt"

	"github.com/gorilla/schema"
)

type Address struct {
	location string
}

func NewAddress(loc string) Address {
	return Address{
		location: loc,
	}
}

func (a *Address) UnmarshalText(data []byte) error {
	*a = NewAddress(string(data))
	return nil
}

func (a *Address) String() {
        return a.loc
}

type F struct {
	Addresses []Address
}

func main() {
	query := map[string][]string{
		"Addresses": {"accra", "lagos"},
	}

	var f F

	d := schema.NewDecoder()
	if err := d.Decode(&f, query); err != nil {
		fmt.Println("error:", err)
	}
	fmt.Println(f)
}

stale commented

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.