FetchState fetches the state from the Context stored under S.Key()
(ctx context.Context)
| 182 | |
| 183 | // FetchState fetches the state from the Context stored under S.Key() |
| 184 | func FetchState[S WithState](ctx context.Context) *S { |
| 185 | p := ctx.Value(persistedEnv) |
| 186 | if p == nil { |
| 187 | panic("need to invoke SetupState at least once") |
| 188 | } |
| 189 | |
| 190 | newS := *new(S) |
| 191 | key := persistedKey(newS) |
| 192 | |
| 193 | store := *p.(*map[string]any) |
| 194 | |
| 195 | state := store[key] |
| 196 | if state == nil { |
| 197 | panic(fmt.Sprintf("no state found for key %s, make sure to invoke SetupState for %T first", key, newS)) |
| 198 | } |
| 199 | |
| 200 | return state.(*S) |
| 201 | } |
| 202 | |
| 203 | // HasState returns true if the state for the provided type is present in the context |
| 204 | func HasState[S WithState](ctx context.Context) bool { |
nothing calls this directly
no test coverage detected