handle := {{ start_call }}
if handle == 0 {
if err := lastError(); err != nil {
return nil, err
}
return nil, fmt.Errorf("failed to start {{ method_name }} stream")
}
ch := make(chan {{ item_type }})
go func() {
defer close(ch)
defer C.{{ fn_free }}(handle)
for {
chunkPtr := C.{{ fn_next }}(handle)
if chunkPtr == 0 {
// Null = clean end-of-stream (errno 0) or stream error (errno != 0).
// In either case there are no more chunks; close the channel.
return
}
jsonPtr := C.{{ item_to_json_fn }}(chunkPtr)
if jsonPtr == nil {
C.{{ item_free_fn }}(chunkPtr)
return
}
{% if item_is_sum_type %}
chunk, unmarshalErr := Unmarshal{{ item_type }}([]byte(C.GoString(jsonPtr)))
{% else %}
var chunk {{ item_type }}
unmarshalErr := json.Unmarshal([]byte(C.GoString(jsonPtr)), &chunk)
{% endif %}
C.{{ free_string_fn }}(jsonPtr)
C.{{ item_free_fn }}(chunkPtr)
if unmarshalErr != nil {
return
}
ch <- chunk
}
}()
return ch, nil
}