marshalForProto will attempt to convert from aibridge.Metadata into a proto-friendly map[string]*anypb.Any. If any marshaling fails, rather return a map with the error details since we don't want to fail Record* funcs if metadata can't encode, since it's, well, metadata.
(in aibridge.Metadata)
| 127 | // If any marshaling fails, rather return a map with the error details since we don't want to fail Record* funcs if metadata can't encode, |
| 128 | // since it's, well, metadata. |
| 129 | func marshalForProto(in aibridge.Metadata) map[string]*anypb.Any { |
| 130 | out := make(map[string]*anypb.Any, len(in)) |
| 131 | if len(in) == 0 { |
| 132 | return out |
| 133 | } |
| 134 | |
| 135 | // Instead of returning error, just encode error into metadata. |
| 136 | encodeErr := func(err error) map[string]*anypb.Any { |
| 137 | errVal, _ := anypb.New(structpb.NewStringValue(err.Error())) |
| 138 | mdVal, _ := anypb.New(structpb.NewStringValue(fmt.Sprintf("%+v", in))) |
| 139 | return map[string]*anypb.Any{ |
| 140 | "error": errVal, |
| 141 | "metadata": mdVal, |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | for k, v := range in { |
| 146 | sv, err := structpb.NewValue(v) |
| 147 | if err != nil { |
| 148 | return encodeErr(err) |
| 149 | } |
| 150 | |
| 151 | av, err := anypb.New(sv) |
| 152 | if err != nil { |
| 153 | return encodeErr(err) |
| 154 | } |
| 155 | |
| 156 | out[k] = av |
| 157 | } |
| 158 | return out |
| 159 | } |
no test coverage detected