Imported from visdomtech/orcacommon (
utils/AGENTS.md). Install upstream withnpx skills add visdomtech/orcacommon --skill utils. Copyright stays with the author.
Utility Functions
General-purpose helpers: struct↔map conversion (JSON-based and reflection-based), HTTP/network utilities, embedded Postgres process probes, and a split-level slog handler.
Architecture
Pure utility functions with no internal orchestration or state management. Each file is independent — no cross-file coordination within the package. The embedded Postgres probes (embedded_pg.go) use POSIX signals and are excluded on Windows via build constraints; embedded_pg_windows.go provides no-op stubs.
Components
Struct↔Map Conversion (convert.go)
JSON-based:
StructToMap[T](*T)— serializes a json-tagged struct pointer tomap[string]anyviajson.Marshal/Unmarshal.MapToStruct[T](map[string]any)— deserializes a map back into a typed struct pointer.PrettyJSON(v)— indented JSON for debugging.
Reflection-based (StructToMapLC):
- Converts PascalCase field names to camelCase.
- Flattens embedded structs into the parent map.
- Respects
json.Marshalerimplementations (usesMarshalJSONinstead of field reflection). - Configurable via functional options:
WithIDSuffix()—"UserID"→"userId"instead of"userID".WithOmitEmpty()— skip empty strings, nil pointers, nil/empty slices.WithNilMapToEmpty()— nil map →{}instead ofnull.WithNilSliceToEmpty()— nil slice →[]instead ofnull.
Network Helpers (network.go)
IsFromLocalhost(req)— checksRemoteAddragainst127.0.0.1/::1.WriteJSONResponse(w, status, v)— setsContent-Typeand writes JSON.RequestHost(req)— returnsX-Forwarded-Hostorreq.Host.GetFreePort()— allocates an unused TCP port on127.0.0.1(small race window in high-contention).
Embedded Postgres Probes (embedded_pg.go, embedded_pg_windows.go)
Unix-only (build constraint !windows). Windows has no-op stubs — all probes return zero values; ReadPostmasterPort additionally returns an error ("not supported on windows").
IsDataPathInitialized(dataPath)— checks forPG_VERSIONfile.CheckPIDFile(dataPath)— readspostmaster.pid, probes process liveness viaSignal(0).IsPortListening(host, port, timeout)— TCP dial check.ReadPostmasterPort(dataPath)— reads port from line 4 ofpostmaster.pid.ReuseEmbeddedPG(dataPath)— composite check: PID alive AND port listening →(true, port).IsEmbeddedPGRunning(dataPath)— convenience wrapper aroundReuseEmbeddedPG.KillEmbeddedPG(pid)— sends SIGKILL with waitpid fallback for zombie reaping; used as force-kill fallback whenpg_ctl stopfails. Verifies process identity viaps -pbefore sending signal to guard against PID reuse. Polls up to 5s for process death.IsProcessAlive(pid)—Signal(0)liveness probe; handles Go 1.24+os.FindProcessbehavior.
Split-Level slog Handler (slog_handler.go)
SplitLevelHandler routes log records to stdout (below Error) and stderr (Error and above). Implements slog.Handler interface. Composes two child handlers (StdHandler, ErrHandler).
Note:
SplitLevelHandlercurrently has no dedicated test coverage. Add tests when modifying.
Configuration
N/A — pure utility functions, no configuration required.