youngoli commented on a change in pull request #12127: URL: https://github.com/apache/beam/pull/12127#discussion_r447385783
########## File path: sdks/go/pkg/beam/core/graph/coder/bytes_test.go ########## @@ -0,0 +1,61 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package coder + +import ( + "bytes" + "fmt" + "strings" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestEncodeDecodeBytes(t *testing.T) { + longString := strings.Repeat(" this sentence is 32 characters.", 8) // 256 characters to ensure LP works. + tests := []struct { + v []byte + encoded []byte + }{ + {v: []byte{}, encoded: []byte{0}}, + {v: []byte{42}, encoded: []byte{1, 42}}, + {v: []byte{42, 23}, encoded: []byte{2, 42, 23}}, + {v: []byte(longString), encoded: append([]byte{128, 2}, []byte(longString)...)}, Review comment: I can't figure out why the size part of the encoded buffer is `[]byte{128, 2}` here. It should represent 256 right? ########## File path: sdks/go/pkg/beam/core/graph/coder/iterable.go ########## @@ -0,0 +1,124 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package coder + +import ( + "fmt" + "io" + "reflect" + + "github.com/apache/beam/sdks/go/pkg/beam/internal/errors" +) + +// TODO(lostluck): 2020.06.29 export these for use for others? + +// iterableEncoder reflectively encodes a slice or array type using +// the beam fixed length iterable encoding. +func iterableEncoder(rt reflect.Type, encode func(reflect.Value, io.Writer) error) func(reflect.Value, io.Writer) error { + return func(rv reflect.Value, w io.Writer) error { + size := rv.Len() + if err := EncodeInt32((int32)(size), w); err != nil { + return err + } + for i := 0; i < size; i++ { + if err := encode(rv.Index(i), w); err != nil { + return err + } + } + return nil + } +} + +// iterableDecoderForSlice can decode from both the fixed sized and +// multi-chunk variants of the beam iterable protocol. +// Returns an error for other protocols (such as state backed). +func iterableDecoderForSlice(rt reflect.Type, decodeToElem func(reflect.Value, io.Reader) error) func(reflect.Value, io.Reader) error { + return func(ret reflect.Value, r io.Reader) error { + // (1) Read count prefixed encoded data + size, err := DecodeInt32(r) + if err != nil { + return err + } + n := int(size) + switch { + case n >= 0: + rv := reflect.MakeSlice(rt, n, n) + if err := decodeToIterable(rv, r, decodeToElem); err != nil { + return err + } + ret.Set(rv) + return nil + case n == -1: + chunk, err := DecodeVarInt(r) + if err != nil { + return err + } + rv := reflect.MakeSlice(rt, 0, int(chunk)) + for chunk != 0 { + fmt.Printf("chunk: %d\n", chunk) Review comment: Is this Printf intentional? I would imagine this line would use logging normally, so a Printf looks like a leftover line after debugging. ---------------------------------------------------------------- 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: [email protected]
