youngoli commented on a change in pull request #12588:
URL: https://github.com/apache/beam/pull/12588#discussion_r489138265



##########
File path: sdks/go/pkg/beam/core/runtime/exec/coder.go
##########
@@ -434,6 +600,267 @@ func convertIfNeeded(v interface{}, allocated *FullValue) 
*FullValue {
        return allocated
 }
 
+type iterableEncoder struct {
+       t   reflect.Type
+       enc ElementEncoder
+}
+
+func (c *iterableEncoder) Encode(val *FullValue, w io.Writer) error {
+       // Do a reflect, get the length.
+       rv := reflect.ValueOf(val.Elm)
+       size := rv.Len()
+       if err := coder.EncodeInt32((int32)(size), w); err != nil {
+               return err
+       }
+       var e FullValue
+       for i := 0; i < size; i++ {
+               e.Elm = rv.Index(i).Interface()
+               err := c.enc.Encode(&e, w)
+               if err != nil {
+                       return err
+               }
+       }
+       return nil
+}
+
+type iterableDecoder struct {
+       t   reflect.Type
+       dec ElementDecoder
+}
+
+func (c *iterableDecoder) DecodeTo(r io.Reader, fv *FullValue) error {
+       // (1) Read count prefixed encoded data
+
+       size, err := coder.DecodeInt32(r)
+       if err != nil {
+               return err
+       }
+       n := int(size)
+       switch {
+       case n >= 0:
+               rv, err := c.decodeToSlice(int(n), r)
+               if err != nil {
+                       return err
+               }
+               *fv = FullValue{Elm: rv.Interface()}
+               return nil
+       case n == -1:
+               rv := reflect.MakeSlice(c.t, 0, 0)
+               chunk, err := coder.DecodeVarInt(r)
+               if err != nil {
+                       return err
+               }
+               for chunk != 0 {
+                       rvi, err := c.decodeToSlice(int(chunk), r)
+                       if err != nil {
+                               return err
+                       }
+                       rv = reflect.AppendSlice(rv, rvi)
+                       chunk, err = coder.DecodeVarInt(r)
+                       if err != nil {
+                               return err
+                       }
+               }
+               *fv = FullValue{Elm: rv.Interface()}
+       }
+
+       return nil
+}
+
+func (c *iterableDecoder) decodeToSlice(n int, r io.Reader) (reflect.Value, 
error) {
+       var e FullValue
+       rv := reflect.MakeSlice(c.t, n, n)
+       for i := 0; i < int(n); i++ {
+               err := c.dec.DecodeTo(r, &e)
+               if err != nil {
+                       return reflect.Value{}, err
+               }
+               if e.Elm != nil {
+                       rv.Index(i).Set(reflect.ValueOf(e.Elm))
+               } else {
+                       rv.Index(i).Set(reflect.ValueOf(e.Windows[0]))

Review comment:
       Ah that makes sense now. Yeah definitely could use a comment.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to