// TODO will probably have more state for validating
{{- range $field := .Type.Fields }}
{{- if not $field.IsOptional }}
{{ $field.Name }}__isset bool
{{- end}}
{{- end}}
}
func (mb *{{ .Type.Name }}__MapBuilder) Insert(k, v ipld.Node) error {
panic("TODO now")
ks, err := k.AsString()
if err != nil {
return ipld.ErrInvalidKey{"not a string: " + err.Error()}
}
switch ks {
{{- range $field := .Type.Fields }}
case "{{ $field.Name }}":
{{- if $field.IsNullable }}
if v.IsNull() {
mb.v.{{ $field.Name }} = nil
{{- if $field.IsOptional }}
mb.v.{{ $field.Name }}__exists = true
{{- else}}
mb.{{ $field.Name }}__isset = true
{{- end}}
return nil
}
{{- else}}
if v.IsNull() {
panic("type mismatch on struct field assignment: cannot assign null to non-nullable field") // FIXME need an error type for this
}
{{- end}}
tv, ok := v.(typed.Node)
if !ok {
panic("need typed.Node for insertion into struct") // FIXME need an error type for this
}
x, ok := v.({{ $field.Type.Name }})
if !ok {
panic("field '{{$field.Name}}' in type {{.Type.Name}} is type {{$field.Type.Name}}; cannot assign "+tv.Type().Name()) // FIXME need an error type for this
}
{{- if and $field.IsOptional $field.IsNullable }}
mb.v.{{ $field.Name }} = &x
mb.v.{{ $field.Name }}__exists = true
{{- else if or $field.IsOptional $field.IsNullable }}