Skip to content

Add EncodeToStringWith and EncodeToValuesWith convenience functions#16

Merged
ajg merged 1 commit intomasterfrom
encode-to-with
Feb 17, 2026
Merged

Add EncodeToStringWith and EncodeToValuesWith convenience functions#16
ajg merged 1 commit intomasterfrom
encode-to-with

Conversation

@ajg
Copy link
Owner

@ajg ajg commented Jul 18, 2017

No description provided.

@ajg ajg force-pushed the encode-to-with branch from 2ef78cf to 5f6a2d9 Compare July 18, 2017 04:48
@ajg ajg mentioned this pull request Jul 18, 2017
@godblesshugh
Copy link
Contributor

could import a global config for this? avoid to using a long and same params every time.

@ajg
Copy link
Owner Author

ajg commented Mar 18, 2019

could import a global config for this? avoid to using a long and same params every time.

@godblesshugh, great suggestion, and I wish I could have gotten to it earlier; alas, hopefully you can still take advantage of it. How about something like this: https://github.com/ajg/form/compare/options?expand=1

@godblesshugh
Copy link
Contributor

godblesshugh commented Mar 18, 2019

been 2 years 😂😂😂
a little memory lost, but i found this in the repository i forked:

func EncodeToString(dst interface{}, needEmptyValue ...bool) (string, error) {
	v := reflect.ValueOf(dst)
	z := false
	if len(needEmptyValue) != 0 {
		z = needEmptyValue[0]
	}
	n, err := encodeToNode(v, z)

and this:

func fieldInfo(f reflect.StructField, tagName ...string) (k string, oe bool) {
	_tagName := "form"
	if len(tagName) > 0 {
		_tagName = tagName[0]
	}
	if f.PkgPath != "" { // Skip private fields.
		return omittedKey, oe
	}

	k = f.Name
	tag := f.Tag.Get(_tagName)
	if tag == "" {
		if len(tagName) == 0 && _tagName != "json" {
			return fieldInfo(f, "json") // using json as secondary
		} else {
			return k, oe
		}
	}

	ps := strings.SplitN(tag, ",", 2)
	if ps[0] != "" {
		k = ps[0]
	}
	if len(ps) == 2 {
		oe = ps[1] == "omitempty"
	}
	return k, oe
}

I can create a PR if it helps.

@ajg
Copy link
Owner Author

ajg commented Mar 18, 2019

@godblesshugh, 😆indeed. And yeah, a PR would be helpful.

@godblesshugh
Copy link
Contributor

#19

@buchanaf
Copy link

buchanaf commented Feb 4, 2020

@ajg, will this ever be merged?

Add parameterized versions of EncodeToString and EncodeToValues that
accept custom delimiter, escape, and keepZeros options. The original
functions now delegate to these new variants with default settings.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ajg ajg merged commit e5aae99 into master Feb 17, 2026
7 checks passed
@ajg ajg deleted the encode-to-with branch February 17, 2026 02:28
@ajg ajg added the resolved label Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments