Skip to content

Commit 9dbe3d4

Browse files
committed
Use rewrite-json to edit jsons without losing formatting
1 parent d3a0327 commit 9dbe3d4

3 files changed

Lines changed: 26 additions & 23 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Consider Anthropic internal server error as a retriable error.
66
- Improve MCP error logging to show error code, message, and data instead of null.
77
- Improve error handling when MCP promtps fail on server side.
8+
- Use rewrite-json to edit jsons without losing formatting.
89

910
## 0.115.1
1011

deps.edn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
io.github.plumce/plumcp.core-json-cheshire {:mvn/version "0.2.0-beta4"}
77
org.yaml/snakeyaml {:mvn/version "2.4"} ;; used by eca.shared for YAML parsing
88
borkdude/dynaload {:mvn/version "0.3.5"}
9+
dev.ericdallo/rewrite-json {:mvn/version "0.1.1"}
910
selmer/selmer {:mvn/version "1.12.69"}
1011
babashka/fs {:mvn/version "0.5.26"}
1112
babashka/process {:mvn/version "0.6.23"}

src/eca/config.clj

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
[clojure.string :as string]
1818
[clojure.walk :as walk]
1919
[eca.features.agents :as agents]
20+
[rewrite-json.core :as rj]
2021
[eca.interpolation :as interpolation]
2122
[eca.logger :as logger]
2223
[eca.messenger :as messenger]
@@ -615,27 +616,27 @@
615616

616617
(def ^:private config-schema-url "https://eca.dev/config.json")
617618

619+
(defn ^:private flatten-to-paths
620+
"Recursively walks a nested map and returns a sequence of [path value] pairs,
621+
where path is a vector of string keys and value is a leaf (non-map) value.
622+
Mirrors deep-merge semantics: only the touched leaf paths are written."
623+
([m] (flatten-to-paths [] m))
624+
([prefix m]
625+
(reduce-kv (fn [acc k v]
626+
(let [path (conj prefix (if (keyword? k) (name k) (str k)))]
627+
(if (and (map? v) (seq v))
628+
(into acc (flatten-to-paths path v))
629+
(conj acc [path v]))))
630+
[]
631+
m)))
632+
618633
(defn update-global-config! [config]
619-
(let [global-config-file (global-config-file)
620-
current-config (normalize-fields normalization-rules (config-from-global-file))
621-
new-config (deep-merge current-config
622-
(normalize-fields normalization-rules config))
623-
new-config (assoc new-config "$schema" config-schema-url)
624-
new-config-json (json/generate-string new-config {:pretty true})]
625-
(io/make-parents global-config-file)
626-
(spit global-config-file new-config-json)))
627-
628-
(defn update-local-config!
629-
"Deep-merges `config` into the local `.eca/config.json` for `workspace-root-uri`."
630-
[workspace-root-uri config]
631-
(let [config-dir (io/file (shared/uri->filename workspace-root-uri) ".eca")
632-
config-file (io/file config-dir "config.json")
633-
current-config (when (.exists config-file)
634-
(normalize-fields normalization-rules
635-
(safe-read-json-string (slurp config-file) (var *local-config-error*))))
636-
new-config (deep-merge (or current-config {})
637-
(normalize-fields normalization-rules config))
638-
new-config (assoc new-config "$schema" config-schema-url)
639-
new-config-json (json/generate-string new-config {:pretty true})]
640-
(io/make-parents config-file)
641-
(spit config-file new-config-json)))
634+
(let [file (global-config-file)
635+
raw (if (.exists file) (slurp file) "{}")
636+
root (rj/parse-string raw)
637+
root (reduce (fn [r [path v]] (rj/assoc-in r path v))
638+
root
639+
(flatten-to-paths config))
640+
root (rj/assoc-in root ["$schema"] config-schema-url)]
641+
(io/make-parents file)
642+
(spit file (rj/to-string root))))

0 commit comments

Comments
 (0)