gorilla/schema

[bug] Golang gorilla schema encode failing for pointer to a slice of struct

sahajavidya opened this issue · 1 comments

Describe the bug

A clear and concise description of what the bug is.

Golang gorilla schema encode failing for pointer to a slice of struct

Versions

Go version: go version -- go version go1.14
package version: run git rev-parse HEAD inside the repo

Steps to Reproduce

How can the bug be triggered?


go playground

Expected behavior

What output or behaviour were you expecting instead?
Should not crash with error -
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4d661d]

goroutine 1 [running]:
github.com/gorilla/schema.typeEncoder.func1(0x4e0de0, 0xc00000e028, 0x196, 0x0, 0x4e0de0)
/tmp/gopath426458645/pkg/mod/github.com/gorilla/schema@v1.2.0/encoder.go:167 +0x9d
github.com/gorilla/schema.(*Encoder).encode(0xc000010210, 0x4fb800, 0xc0000160c0, 0x99, 0xc000060270, 0xc0000160c0, 0xc000068f38)
/tmp/gopath426458645/pkg/mod/github.com/gorilla/schema@v1.2.0/encoder.go:135 +0xa03
github.com/gorilla/schema.(*Encoder).Encode(0xc000010210, 0x4fb800, 0xc0000160c0, 0xc000060270, 0xc000068ea0, 0x40ce50)
/tmp/gopath426458645/pkg/mod/github.com/gorilla/schema@v1.2.0/encoder.go:29 +0xb2
main.main()
/tmp/sandbox392948750/prog.go:93 +0x548

Code Snippets

A minimum viable code snippet can be useful! (use backticks to format it).

package main

import (
	"fmt"
	"net/url"
	"time"

	"github.com/gorilla/schema"
)

var (
	encoder = schema.NewEncoder()
)

type GetMetricDataInput struct {
	EndTime           *time.Time         `schema:"EndTime"`
	MaxDatapoints     *int64             `schema:"MaxDatapoints"`
	MetricDataQueries []*MetricDataQuery `schema:"MetricDataQueries.member"`
	NextToken         *string            `schema:"NextToken"`
	ScanBy            *string            `schema:"ScanBy"`
	StartTime         *time.Time         `schema:"StartTime"`
}

type MetricDataQuery struct {
	Id         *string
	Label      *string
	MetricStat *MetricStat
	Period     *int64
	ReturnData *bool
}

type MetricStat struct {
	Metric *Metric
	Period *int64
	Stat   *string
}

type Metric struct {
	Dimensions []*Dimension
	MetricName *string
	Namespace  *string
}

type Dimension struct {
	Name  *string
	Value *string
}

func main() {

	endTime := time.Now()
	var maxDatapoints int64 = 1
	id := "id"
	label := "label"
	instanceID := "InstanceId"
	val := "i-09603b2a8a61d831f"
	metricName := "CPUUtilization"
	namespace := "AWS/EC2"
	var period int64 = 60
	stat := "Minimum"
	returnData := true
	scanby := "scanby"
	startTime := time.Now()

	mdi := GetMetricDataInput{
		EndTime:       &endTime,
		MaxDatapoints: &maxDatapoints,
		MetricDataQueries: []*MetricDataQuery{{
			Id:    &id,
			Label: &label,
			MetricStat: &MetricStat{
				Metric: &Metric{
					Dimensions: []*Dimension{{
						Name:  &instanceID,
						Value: &val,
					}},
					MetricName: &metricName,
					Namespace:  &namespace,
				},
				Period: &period,
				Stat:   &stat,
			},
			Period:     &period,
			ReturnData: &returnData,
		}},
		NextToken: nil,
		ScanBy:    &scanby,
		StartTime: &startTime,
	}

	form := url.Values{}
	err := encoder.Encode(mdi, form)
	if err != nil {
		fmt.Printf("Error Encode: %v\n", err)
	}
	fmt.Printf("Encoded value: %v\n", form)

	res := form.Encode()
	fmt.Printf("Resonse: %v\n", res)

}

stale commented

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.