Skip to content

Fix panic when the decoding pointer type with custom decoding function#67

Closed
adityarsuryavamshi wants to merge 1 commit intogo-playground:masterfrom
adityarsuryavamshi:master
Closed

Fix panic when the decoding pointer type with custom decoding function#67
adityarsuryavamshi wants to merge 1 commit intogo-playground:masterfrom
adityarsuryavamshi:master

Conversation

@adityarsuryavamshi
Copy link
Contributor

@adityarsuryavamshi adityarsuryavamshi commented Mar 18, 2025

Fixes Or Enhances

When a pointer type has a custom decoder and the decoded value is nil, we get a panic.

Example

func main() {
	type CustomTime *time.Time
	type TestError struct {
		CT CustomTime
	}
	var test TestError
	decoder = form.NewDecoder()
	decoder.RegisterCustomTypeFunc(func(s []string) (interface{}, error) {
		if s[0] == "" {
			return nil, nil
		}
		parsed, err := time.Parse(time.RFC3339, s[0])
		if err != nil {
			return nil, err
		}
		return CustomTime(&parsed), nil
	}, CustomTime(nil))

	values := url.Values{
		"CT": []string{""},
	}
	decoder.Decode(&test, values)
}
panic: reflect: call of reflect.Value.Set on zero Value

This PR specifically handles the case for the case when the type is a pointer.

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/admins

@coveralls
Copy link

coveralls commented Mar 18, 2025

Coverage Status

coverage: 99.815% (+0.001%) from 99.814%
when pulling 58d4ec6 on adityarsuryavamshi:master
into 8785d3c on go-playground:master.

@adityarsuryavamshi adityarsuryavamshi closed this by deleting the head repository Mar 18, 2025
@adityarsuryavamshi
Copy link
Contributor Author

reopened here #68

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

Successfully merging this pull request may close these issues.

2 participants