ExprValueAsProto converts between ref.Val and cel.expr.ExprValue. The result ExprValue is the serialized proto form.
(res ref.Val)
| 147 | // ExprValueAsProto converts between ref.Val and cel.expr.ExprValue. |
| 148 | // The result ExprValue is the serialized proto form. |
| 149 | func ExprValueAsProto(res ref.Val) (*celpb.ExprValue, error) { |
| 150 | switch res := res.(type) { |
| 151 | case *types.Unknown: |
| 152 | return &celpb.ExprValue{ |
| 153 | Kind: &celpb.ExprValue_Unknown{ |
| 154 | Unknown: &celpb.UnknownSet{ |
| 155 | Exprs: res.IDs(), |
| 156 | }, |
| 157 | }}, nil |
| 158 | case *types.Err: |
| 159 | return &celpb.ExprValue{ |
| 160 | Kind: &celpb.ExprValue_Error{ |
| 161 | Error: &celpb.ErrorSet{ |
| 162 | // Keeping the error code as UNKNOWN since there's no error codes associated with |
| 163 | // Cel-Go runtime errors. |
| 164 | Errors: []*celpb.Status{{Code: 2, Message: res.Error()}}, |
| 165 | }, |
| 166 | }, |
| 167 | }, nil |
| 168 | default: |
| 169 | val, err := ValueAsProto(res) |
| 170 | if err != nil { |
| 171 | return nil, err |
| 172 | } |
| 173 | return &celpb.ExprValue{ |
| 174 | Kind: &celpb.ExprValue_Value{Value: val}}, nil |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // ValueAsProto converts between ref.Val and cel.expr.Value. |
| 179 | // The result Value is the serialized proto form. The ref.Val must not be error or unknown. |