Skip to content

Anonimous fields without prefix #14

@Hamper

Description

@Hamper

Is possible parse anonimous fields without prefix if it's tag ommited or empty string (like json)?

Example:

package main

import (
	"encoding/json"
	"fmt"

	"github.com/ajg/form"
)

type T1 struct {
	X int `json:"x,omitempty" form:"x,omitempty"`
	Y int `json:"y,omitempty" form:"y,omitempty"`
}

type T2 struct {
	*T1 `json:"" form:""`
	A   int `json:"a,omitempty" form:"a,omitempty"`
	B   int `json:"b,omitempty" form:"b,omitempty"`
}

type T3 struct {
	*T1 `json:"t1" form:"t1"`
	A   int `json:"a,omitempty" form:"a,omitempty"`
	B   int `json:"b,omitempty" form:"b,omitempty"`
}

func main() {
	x := T2{&T1{1, 2}, 3, 4}
	s, _ := json.Marshal(x)
	s2, _ := form.EncodeToString(x)
	fmt.Println(string(s), s2)

	x2 := T3{&T1{1, 2}, 3, 4}
	s, _ = json.Marshal(x2)
	s2, _ = form.EncodeToString(x2)
	fmt.Println(string(s), s2)
}

Output:

{"x":1,"y":2,"a":3,"b":4} T1.x=1&T1.y=2&a=3&b=4
{"t1":{"x":1,"y":2},"a":3,"b":4} a=3&b=4&t1.x=1&t1.y=2

Second result is ok, but in first best result is "x=1&y=2&a=3&b=4"

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions