Files
streamrip-go/internal/jsonutil/jsonutil.go
T
JorenandClaude Opus 5 26fb466afc refactor: finish the jsonutil dedupe; back up config before auto-upgrade
Duplicated helpers
  cmd/rip had its own copies of six helpers that already existed in
  internal/jsonutil: asString (StringFromAny), searchInt (IntFromAny),
  nestedSearchInt and nestedSearchString (NestedAny/NestedString),
  firstNonEmpty (FirstNonEmpty) and firstPositiveInt. firstPositiveInt was
  duplicated a third and fourth time in provider/yandex and provider/beatport,
  so it moves into jsonutil alongside the rest. jsonutil is back to 100%
  coverage.

  searchBool is deliberately left alone, with a comment saying why: it accepts
  only a real bool, and switching it to jsonutil.BoolFromAny would start
  coercing strings and numbers, changing how search results get labelled.

config auto-upgrade is no longer destructive
  UpgradeOutdated re-serializes the whole config from the parsed struct, so any
  key this version does not know about is dropped, and it runs automatically
  from run() without prompting. It now copies the original to
  <path>.bak-<unixtime> (0600, since it holds the same credentials) and
  validates the result before writing. It returns the backup path so the CLI
  can tell the user where it went. The comment states plainly that this is a
  version stamp rather than a migration framework.

cmd/rip tests
  The command bodies are testable now that they are functions returning exit
  codes rather than arms of a switch that called os.Exit. Covers the usage
  errors for every command, the quality floor on `rip id`, config path output,
  the --version flag, and that applyQualityOverride reaches all six sources and
  clamps only the two with a floor. cmd/rip coverage 17% -> 26%.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-03 17:26:24 +02:00

161 lines
3.7 KiB
Go

// Package jsonutil provides shared helpers for working with untyped JSON
// values (map[string]any) that come from API responses across all providers.
package jsonutil
import (
"strconv"
"strings"
)
// StringFromAny converts a dynamic JSON value to a string.
// Numeric types are formatted without trailing zeroes.
func StringFromAny(v any) string {
switch t := v.(type) {
case string:
return t
case int:
return strconv.Itoa(t)
case int64:
return strconv.FormatInt(t, 10)
case float64:
return strconv.FormatFloat(t, 'f', -1, 64)
default:
return ""
}
}
// IntFromAny converts a dynamic JSON value to an int.
// Handles int, int64, float64, and string types.
func IntFromAny(v any) int {
switch t := v.(type) {
case int:
return t
case int32:
return int(t)
case int64:
return int(t)
case float64:
return int(t)
case string:
i, _ := strconv.Atoi(strings.TrimSpace(t))
return i
default:
return 0
}
}
// FloatFromAny converts a dynamic JSON value to a float64.
func FloatFromAny(v any) float64 {
switch t := v.(type) {
case float64:
return t
case int:
return float64(t)
case int64:
return float64(t)
default:
return 0
}
}
// BoolFromAny converts a dynamic JSON value to a bool.
// Supports bool, string ("true"/"1"/"yes"), and numeric types.
func BoolFromAny(v any) bool {
switch t := v.(type) {
case bool:
return t
case string:
l := strings.ToLower(strings.TrimSpace(t))
return l == "1" || l == "true" || l == "yes"
case int:
return t != 0
case int64:
return t != 0
case float64:
return t != 0
default:
return false
}
}
// FirstNonEmpty returns the first string in items that is non-empty after trimming.
func FirstNonEmpty(items ...string) string {
for _, item := range items {
if strings.TrimSpace(item) != "" {
return strings.TrimSpace(item)
}
}
return ""
}
// NestedMap returns the value at m[key] as a map[string]any.
// Returns an empty map if the key is missing or the value is not a map.
func NestedMap(m map[string]any, key string) map[string]any {
v, ok := m[key].(map[string]any)
if !ok {
return map[string]any{}
}
return v
}
// NestedAny traverses a chain of map keys and returns the final value.
func NestedAny(v map[string]any, keys ...string) any {
cur := any(v)
for _, key := range keys {
m, ok := cur.(map[string]any)
if !ok {
return nil
}
cur = m[key]
}
return cur
}
// NestedString traverses a chain of map keys and returns the final value as a string.
func NestedString(v map[string]any, keys ...string) string {
return StringFromAny(NestedAny(v, keys...))
}
// TitleCase capitalises the first rune of s. This is a simple ASCII replacement
// for the deprecated strings.Title function, suitable for source names like
// "qobuz" → "Qobuz".
func TitleCase(s string) string {
if s == "" {
return s
}
r := []rune(s)
if r[0] >= 'a' && r[0] <= 'z' {
r[0] -= 'a' - 'A'
}
return string(r)
}
// DisplayArtistNames joins artist names the way the providers present them:
// "A", "A & B", or "A, B & C". Previously duplicated byte-for-byte in
// internal/app, provider/yandex and provider/beatport.
func DisplayArtistNames(names []string) string {
switch len(names) {
case 0:
return ""
case 1:
return names[0]
case 2:
return names[0] + " & " + names[1]
default:
return strings.Join(names[:len(names)-1], ", ") + " & " + names[len(names)-1]
}
}
// FirstPositiveInt returns the first value greater than zero, or 0 if there is
// none. Useful for picking among optional metadata fields where 0 means
// "absent". Previously duplicated in provider/yandex, provider/beatport and
// cmd/rip.
func FirstPositiveInt(values ...int) int {
for _, v := range values {
if v > 0 {
return v
}
}
return 0
}