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

Empty string element in a slice disappears after decoding #105

Closed
mariaefi29 opened this issue May 16, 2018 · 6 comments
Closed

Empty string element in a slice disappears after decoding #105

mariaefi29 opened this issue May 16, 2018 · 6 comments

Comments

@mariaefi29
Copy link

mariaefi29 commented May 16, 2018

For example, I have a struct:

type Post struct { Images []string }

If I want to put in the struct values like (contains three elements, including empty string):

["a", "", "b"]

After decoding I have a slice with two elements excluding an empty string as a result.

Could you please tell me if it Is a bug or a way it works? Thank you!

@kisielk
Copy link
Contributor

kisielk commented May 16, 2018 via email

@mariaefi29
Copy link
Author

package main

import (
	"fmt"
	"github.com/gorilla/schema"
	"net/url"
)

func main() {

	type Images struct {
		Images []string
	}

	decoder := schema.NewDecoder()

	values := url.Values{}
	values.Add("Images", "a")
	values.Add("Images", "")
	values.Add("Images", "b")

	images := Images{}

	fmt.Println("Values are --->", values)
	fmt.Println("The length of values is --->", len(values["Images"]))

	err := decoder.Decode(&images, values)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Images after decoding are --->", images)
	fmt.Println("The length of Images after decoding is --->", len(images.Images))

}

@kisielk kisielk added bug and removed bug labels May 26, 2018
@kisielk
Copy link
Contributor

kisielk commented May 26, 2018

Hi @mariaefi29

Sorry for taking a while back to you. What you need to do is call decoder.ZeroEmpty(true) before calling the Decode method. That will cause empty strings to produce the zero value of the decoded type, otherwise they are ignored.

@kisielk kisielk closed this as completed May 26, 2018
@mariaefi29
Copy link
Author

Thank you very much for your help!

@novikov1337danil
Copy link

novikov1337danil commented Apr 9, 2019

Hi @mariaefi29

Sorry for taking a while back to you. What you need to do is call decoder.ZeroEmpty(true) before calling the Decode method. That will cause empty strings to produce the zero value of the decoded type, otherwise they are ignored.

Hello! For me ZeroEmpty still not working. I still get an empty string instead of nil

Here is my code:
var user models.User
d := schema.NewDecoder()
d.ZeroEmpty(true)
err = d.Decode(&user, r.PostForm)

@elithrar
Copy link
Contributor

elithrar commented Apr 9, 2019 via email

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

4 participants