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

Doubling of last slice element when adding template.HTML #6188

Closed
MarkRosemaker opened this issue Aug 9, 2019 · 2 comments · Fixed by #6404
Labels
Bug
Milestone

Comments

@MarkRosemaker
Copy link

@MarkRosemaker MarkRosemaker commented Aug 9, 2019

I came across a weird error that I could boil down to this minimal example:

{{ $c := delimit (slice "c") " " }}<!-- should be 'c' -->
{{ $abc_slice := slice "a" | append "b" $c  }}<!-- should be ['a','b','c'] -->
{{ $abc := delimit $abc_slice "+" }}<!-- should be 'a+b+c' -->
{{ errorf "'%s' (%T) - '%s' (%T)" $c $c $abc $abc }}

Put that in a shortcode and strangely enough, the result is:

'c' (template.HTML) - 'a+b+b+c' (template.HTML)

As you can see, b is doubled.

The problem goes away if you add | print to $c, making it a string. I wonder why it's not a string in the first place.

Thanks in advance for any comments and fixing.

@vazrupe

This comment has been minimized.

Copy link
Contributor

@vazrupe vazrupe commented Oct 9, 2019

There are two causes of this problem.

The return of the delimit function is template.HTML and a bug in the collections.Append function.

template.HTML is actually the same as string, but due to the nature of golang you cannot append to [] string. This can be verified with a simple code.

package main

import (
	"fmt"
)

type inherit string

func main() {
	var s []string
	
	var txt inherit
	txt = "test"
	
	s = append(s, txt) // ./prog.go:15:12: cannot use txt (type inherit) as type string in append

	fmt.Printf("%v %s\n", s, txt)
}

for _, f := range from {
fv := reflect.ValueOf(f)
if !fv.Type().AssignableTo(tot) {
// Fall back to a []interface{} slice.
return appendToInterfaceSlice(tov, from...)
}
tov = reflect.Append(tov, fv)
}

For collections.Append, the problem arises in the code above. If the type cannot be added to an existing slice, add the whole element once again.

@bep

This comment has been minimized.

Copy link
Member

@bep bep commented Oct 9, 2019

template.HTML isn't "the same as string"; if it was, it would have no purpose.

@bep bep added the Bug label Oct 10, 2019
@bep bep added this to the v0.59 milestone Oct 10, 2019
@bep bep closed this in #6404 Oct 11, 2019
bep added a commit that referenced this issue Oct 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.