nolint:gocyclo // intrinsically long state machine; refactoring would hurt clarity
(ctx context.Context, dag *Server, resultID uint64, call *ResultCall, env PersistedResultEnvelope)
| 205 | |
| 206 | //nolint:gocyclo // intrinsically long state machine; refactoring would hurt clarity |
| 207 | func decodePersistedResultEnvelope(ctx context.Context, dag *Server, resultID uint64, call *ResultCall, env PersistedResultEnvelope) (AnyResult, error) { |
| 208 | setHandle := func(res AnyResult) AnyResult { |
| 209 | if res == nil || env.SessionResourceHandle == "" { |
| 210 | return res |
| 211 | } |
| 212 | shared := res.cacheSharedResult() |
| 213 | if shared == nil { |
| 214 | return res |
| 215 | } |
| 216 | shared.sessionResourceHandle = env.SessionResourceHandle |
| 217 | reqs := set.NewTreeSet(compareSessionResourceHandles) |
| 218 | reqs.Insert(env.SessionResourceHandle) |
| 219 | shared.requiredSessionResources = reqs |
| 220 | return res |
| 221 | } |
| 222 | |
| 223 | switch env.Kind { |
| 224 | case persistedResultKindNull: |
| 225 | return nil, nil |
| 226 | case persistedResultKindObject: |
| 227 | if dag == nil { |
| 228 | return nil, fmt.Errorf("decode object_id envelope: missing current dagql server in context") |
| 229 | } |
| 230 | if call == nil { |
| 231 | return nil, fmt.Errorf("decode object_id envelope: missing authoritative call") |
| 232 | } |
| 233 | objType, ok := dag.ObjectType(env.TypeName) |
| 234 | if !ok { |
| 235 | return nil, fmt.Errorf("decode object_id envelope: unknown object type %q", env.TypeName) |
| 236 | } |
| 237 | decoder, ok := objType.Typed().(PersistedObjectDecoder) |
| 238 | if !ok { |
| 239 | return nil, fmt.Errorf("decode object_id envelope: object type %q does not implement persisted decode", env.TypeName) |
| 240 | } |
| 241 | decodeCtx := ContextWithCall(ctx, call) |
| 242 | valSelf, err := decoder.DecodePersistedObject(decodeCtx, dag, resultID, call, env.ObjectJSON) |
| 243 | if err != nil { |
| 244 | return nil, fmt.Errorf("decode object_id envelope load: %w", err) |
| 245 | } |
| 246 | valRes, err := NewResultForCall(valSelf, call) |
| 247 | if err != nil { |
| 248 | return nil, fmt.Errorf("decode object_id envelope result: %w", err) |
| 249 | } |
| 250 | objRes, err := objType.New(valRes) |
| 251 | if err != nil { |
| 252 | return nil, fmt.Errorf("decode object_id envelope instantiate: %w", err) |
| 253 | } |
| 254 | return setHandle(objRes), nil |
| 255 | case persistedResultKindScalar: |
| 256 | if call == nil { |
| 257 | return nil, fmt.Errorf("decode scalar_json envelope: missing authoritative call") |
| 258 | } |
| 259 | var raw any |
| 260 | if err := json.Unmarshal(env.ScalarJSON, &raw); err != nil { |
| 261 | return nil, fmt.Errorf("decode scalar_json envelope payload: %w", err) |
| 262 | } |
| 263 | if dag != nil { |
| 264 | scalarType, ok := dag.ScalarType(env.TypeName) |
no test coverage detected