form icon indicating copy to clipboard operation
form copied to clipboard

Add EncodeToStringWith and EncodeToValuesWith convenience functions

Open ajg opened this issue 8 years ago β€’ 6 comments

ajg avatar Jul 18 '17 04:07 ajg

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

godblesshugh avatar Jul 21 '17 07:07 godblesshugh

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

ajg avatar Mar 18 '19 05:03 ajg

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.

godblesshugh avatar Mar 18 '19 06:03 godblesshugh

@godblesshugh, πŸ˜†indeed. And yeah, a PR would be helpful.

ajg avatar Mar 18 '19 21:03 ajg

#19

godblesshugh avatar Mar 22 '19 08:03 godblesshugh

@ajg, will this ever be merged?

buchanaf avatar Feb 04 '20 18:02 buchanaf