{{/*
Copyright 2019-present Facebook Inc. All rights reserved.
This source code is licensed under the Apache 2.0 license found
in the LICENSE file in the root directory of this source tree.
*/}}

{{ define "dialect/gremlin/decode/one" }}
{{ $receiver := $.Receiver }}

// FromResponse scans the gremlin response data into {{ $.Name }}.
func ({{ $receiver }} *{{ $.Name }}) FromResponse(res *gremlin.Response) error {
	vmap, err := res.ReadValueMap()
	if err != nil {
		return err
	}
	{{- $scan := print "scan" $receiver }}
	var {{ $scan }} struct {
		ID   {{ $.ID.Type }}  `json:"id,omitempty"`
		{{ range $_, $f := $.Fields }}
			{{- $f.StructField }} {{ if $f.IsTime }}int64{{ else }}{{ if $f.Nillable }}*{{ end }}{{ $f.Type }}{{ end }} `json:"{{ $f.StorageKey }},omitempty"`
		{{ end }}
	}
	if err := vmap.Decode(&{{ $scan }}); err != nil {
		return err
	}
	{{ $receiver }}.ID = {{ $scan }}.ID
	{{ range $_, $f := $.Fields }}
		{{- $receiver }}.{{ $f.StructField }} = {{- if $f.IsTime }}time.Unix(0, {{ $scan }}.{{ $f.StructField }}) {{ else }}{{- $scan }}.{{ $f.StructField }}{{ end }}
	{{ end -}}
	return nil
}
{{ end }}

{{ define "dialect/gremlin/decode/many" }}
{{ $receiver := $.Receiver }}
{{ $slice := $.Scope.Slice }}

// FromResponse scans the gremlin response data into {{ $slice }}.
func ({{ $receiver }} *{{ $slice }}) FromResponse(res *gremlin.Response) error {
	vmap, err := res.ReadValueMap()
	if err != nil {
		return err
	}
	{{- $scan := print "scan" $receiver }}
	var {{ $scan }} []struct {
		ID   {{ $.ID.Type }}  `json:"id,omitempty"`
		{{ range $_, $f := $.Fields }}
			{{- $f.StructField }} {{ if $f.IsTime }}int64{{ else }}{{ if $f.Nillable }}*{{ end }}{{ $f.Type }}{{ end }} `json:"{{ $f.StorageKey }},omitempty"`
		{{ end }}
	}
	if err := vmap.Decode(&{{ $scan }}); err != nil {
		return err
	}
	for _, v := range {{ $scan }} {
		*{{ $receiver }} = append(*{{ $receiver }}, &{{ $.Name }}{
			ID: v.ID,
			{{ range $_, $f := $.Fields }}
				{{- $f.StructField }}: {{- if $f.IsTime }}time.Unix(0, v.{{ $f.StructField }}) {{ else }}v.{{ $f.StructField }}{{ end }},
			{{ end -}}
		})
	}
	return nil
}
{{ end }}