Due to the lack of generics in go this function accepts callback which should return particular item by it index.
( len int, item func(i int) proto.Message, )
| 89 | // Due to the lack of generics in go |
| 90 | // this function accepts callback which should return particular item by it index. |
| 91 | func (e *JSONPBEncoder) encodeSlice( |
| 92 | len int, |
| 93 | item func(i int) proto.Message, |
| 94 | ) ([]byte, error) { |
| 95 | var buf bytes.Buffer |
| 96 | buf.WriteString("[") |
| 97 | for i := range len { |
| 98 | pb := item(i) |
| 99 | bs, err := e.marshaler.Marshal(pb) |
| 100 | if err != nil { |
| 101 | return nil, err |
| 102 | } |
| 103 | buf.Write(bs) |
| 104 | |
| 105 | if i < len-1 { |
| 106 | buf.WriteString(",") |
| 107 | } |
| 108 | } |
| 109 | buf.WriteString("]") |
| 110 | return buf.Bytes(), nil |
| 111 | } |
| 112 | |
| 113 | // constructor callback must create empty object, add it to result slice, and return it. |
| 114 | func (e *JSONPBEncoder) DecodeSlice( |
no test coverage detected