Can any one help me on encode issue for nested struct?
Go playground to reproduce the issue: playground
Getting the below issue:
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/[email protected]/encoder.go:167 +0x9d
github.com/gorilla/schema.(*Encoder).encode(0xc000010210, 0x4fb800, 0xc0000160c0, 0x99, 0xc000060270, 0xc0000160c0, 0xc000068f38)
/tmp/gopath426458645/pkg/mod/github.com/gorilla/[email protected]/encoder.go:135 +0xa03
github.com/gorilla/schema.(*Encoder).Encode(0xc000010210, 0x4fb800, 0xc0000160c0, 0xc000060270, 0xc000068ea0, 0x40ce50)
/tmp/gopath426458645/pkg/mod/github.com/gorilla/[email protected]/encoder.go:29 +0xb2
main.main()
/tmp/sandbox392948750/prog.go:93 +0x548
code snippet:
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,
}
//encoder.RegisterEncoder(mdi.MetricDataQueries, func(reflect.Value) string { return "1" })
form := url.Values{}
err := encoder.Encode(mdi, form)
if err != nil {
fmt.Printf("Error Encode: %v
", err)
}
fmt.Printf("Encoded value: %v
", form)
res := form.Encode()
fmt.Printf("Resonse: %v
", res)
}
What i see is the struct **MetricDataQueries []*MetricDataQuery schema:"MetricDataQueries.member"**
is creating issue but not sure why?
question from:
https://stackoverflow.com/questions/65916560/schema-encode-failing-for-pointer-to-a-slice-of-struct 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…