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

The tag did not match exactly #4113

Open
xishengcai opened this issue Dec 4, 2024 · 1 comment
Open

The tag did not match exactly #4113

xishengcai opened this issue Dec 4, 2024 · 1 comment

Comments

@xishengcai
Copy link

Description

	type tHeader struct {
		Limit  int `header:"limit"`
		Limit2 int `json:"limit"`
	}

If my http request header has limit , I want only math tag: "header:"limi"

Expectations

func TestHeaderBinding(t *testing.T) {
	h := Header
	assert.Equal(t, "header", h.Name())

	type tHeader struct {
		Limit  int `header:"limit"`
		Limit2 int `json:"limit"`
	}

	var theader tHeader
	req := requestWithBody(http.MethodGet, "/", "")
	req.Header.Add("limit", "1000")
	require.NoError(t, h.Bind(req, &theader))
	assert.Equal(t, 1000, theader.Limit)
	assert.Equal(t, 0, theader.Limit2)

	req = requestWithBody(http.MethodGet, "/", "")
	req.Header.Add("fail", `{fail:fail}`)

	type failStruct struct {
		Fail map[string]any `header:"fail"`
	}

	err := h.Bind(req, &failStruct{})
	require.Error(t, err)
}

@xishengcai
Copy link
Author

open ai has been give answer:


	if vKind != reflect.Struct || !field.Anonymous {
		if _, exists := field.Tag.Lookup(tag); exists {
			ok, err := tryToSetValue(value, field, setter, tag)
			if err != nil {
				return false, err
			}
			if ok {
				return true, nil
			}
		}
	}

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

1 participant