ExprValueAsProto converts between ref.Val and cel.expr.ExprValue. The result ExprValue is the serialized proto form.
(res ref.Val)
| 171 | // ExprValueAsProto converts between ref.Val and cel.expr.ExprValue. |
| 172 | // The result ExprValue is the serialized proto form. |
| 173 | func ExprValueAsProto(res ref.Val) (*celpb.ExprValue, error) { |
| 174 | switch res := res.(type) { |
| 175 | case *types.Unknown: |
| 176 | return &celpb.ExprValue{ |
| 177 | Kind: &celpb.ExprValue_Unknown{ |
| 178 | Unknown: &celpb.UnknownSet{ |
| 179 | Exprs: res.IDs(), |
| 180 | }, |
| 181 | }}, nil |
| 182 | case *types.Err: |
| 183 | return &celpb.ExprValue{ |
| 184 | Kind: &celpb.ExprValue_Error{ |
| 185 | Error: &celpb.ErrorSet{ |
| 186 | // Keeping the error code as UNKNOWN since there's no error codes associated with |
| 187 | // Cel-Go runtime errors. |
| 188 | Errors: []*celpb.Status{{Code: 2, Message: res.Error()}}, |
| 189 | }, |
| 190 | }, |
| 191 | }, nil |
| 192 | default: |
| 193 | val, err := ValueAsProto(res) |
| 194 | if err != nil { |
| 195 | return nil, err |
| 196 | } |
| 197 | return &celpb.ExprValue{ |
| 198 | Kind: &celpb.ExprValue_Value{Value: val}}, nil |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // ValueAsProto converts between ref.Val and cel.expr.Value. |
| 203 | // The result Value is the serialized proto form. The ref.Val must not be error or unknown. |