mirror of
https://github.com/photoprism/photoprism.git
synced 2026-07-18 00:59:38 +00:00
Run `go fix ./...` and keep mechanical modernization updates.
- Replace `interface{}` with `any` in signatures and local types
- Apply formatter/style cleanups from go1.26 tooling
- Keep `omitempty` behavior-preserving simplifications suggested by fix
- No functional feature changes intended
Validation:
- go test ./... -run '^$' -count=1 (Go 1.26.0)
- GOTOOLCHAIN=go1.24.10 go test ./... -run '^$' -count=1
Signed-off-by: Michael Mayer <michael@photoprism.app>
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package clean
|
|
|
|
import "strings"
|
|
|
|
// JSON attempts to extract a JSON object or array from raw text.
|
|
// It removes common wrappers such as Markdown code fences and trailing commentary.
|
|
// Returns an empty string when no JSON payload can be found.
|
|
func JSON(raw string) string {
|
|
trimmed := strings.TrimSpace(raw)
|
|
if trimmed == "" {
|
|
return ""
|
|
}
|
|
|
|
if after, ok := strings.CutPrefix(trimmed, "```"); ok {
|
|
trimmed = after
|
|
trimmed = strings.TrimSpace(trimmed)
|
|
|
|
if !strings.HasPrefix(trimmed, "{") && !strings.HasPrefix(trimmed, "[") {
|
|
if idx := strings.Index(trimmed, "\n"); idx != -1 {
|
|
trimmed = trimmed[idx+1:]
|
|
} else {
|
|
return ""
|
|
}
|
|
}
|
|
|
|
if idx := strings.LastIndex(trimmed, "```"); idx != -1 {
|
|
trimmed = trimmed[:idx]
|
|
}
|
|
}
|
|
|
|
trimmed = strings.TrimSpace(trimmed)
|
|
|
|
startObj := strings.Index(trimmed, "{")
|
|
startArr := strings.Index(trimmed, "[")
|
|
|
|
start := -1
|
|
switch {
|
|
case startObj >= 0 && startArr >= 0:
|
|
start = min(startObj, startArr)
|
|
case startObj >= 0:
|
|
start = startObj
|
|
case startArr >= 0:
|
|
start = startArr
|
|
}
|
|
|
|
endObj := strings.LastIndex(trimmed, "}")
|
|
endArr := strings.LastIndex(trimmed, "]")
|
|
|
|
end := -1
|
|
switch {
|
|
case endObj >= 0 && endArr >= 0:
|
|
end = max(endObj, endArr)
|
|
case endObj >= 0:
|
|
end = endObj
|
|
case endArr >= 0:
|
|
end = endArr
|
|
}
|
|
|
|
if start >= 0 && end > start {
|
|
trimmed = trimmed[start : end+1]
|
|
}
|
|
|
|
return strings.TrimSpace(trimmed)
|
|
}
|