From a58a0747bbf6d40bb436e7cf8cbc19814cd54754 Mon Sep 17 00:00:00 2001 From: Nanaloveyuki Date: Fri, 17 Jul 2026 21:19:11 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20migrate=20to=20core=20json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api/async-logger-build-config-to-json.md | 6 +- docs/api/async-logger-config-to-json.md | 6 +- docs/api/async-logger-state-to-json.md | 8 +- docs/api/async-runtime-state-to-json.md | 6 +- docs/api/file-rotation-config-to-json.md | 8 +- docs/api/file-sink-policy-to-json.md | 8 +- docs/api/file-sink-state-to-json.md | 8 +- docs/api/logger-config-to-json.md | 8 +- docs/api/queue-config-to-json.md | 8 +- docs/api/runtime-file-state-to-json.md | 6 +- docs/api/sink-config-to-json.md | 6 +- .../stringify-async-logger-build-config.md | 6 +- docs/api/stringify-async-logger-config.md | 6 +- docs/api/stringify-async-logger-state.md | 4 +- docs/api/stringify-async-runtime-state.md | 4 +- docs/api/stringify-file-sink-policy.md | 2 +- docs/api/stringify-logger-config.md | 4 +- docs/api/stringify-queue-config.md | 6 +- docs/api/stringify-runtime-file-state.md | 2 +- docs/api/stringify-sink-config.md | 4 +- docs/api/stringify-text-formatter-config.md | 4 +- docs/api/text-formatter-config-to-json.md | 6 +- moon.mod | 1 - src-async/BitLoggerAsync_test.mbt | 130 ++++++----- src-async/async_logger_shared.mbt | 14 +- src-async/json_test_helpers_test.mbt | 32 +++ src-async/moon.pkg | 2 +- src-async/pkg.generated.mbti | 9 +- src-async/utils/async_models.mbt | 152 +++++-------- src-async/utils/moon.pkg | 2 +- src-async/utils/pkg.generated.mbti | 9 +- src/config.mbt | 14 +- src/config_model/config.mbt | 204 ++++++++---------- src/config_model/moon.pkg | 2 +- src/config_model/pkg.generated.mbti | 9 +- src/file_model/file_json.mbt | 71 +++--- src/file_model/moon.pkg | 2 +- src/file_model/pkg.generated.mbti | 12 +- src/file_model/runtime_file_state.mbt | 20 +- src/json_test_helpers_test.mbt | 32 +++ src/logger_config_json_helpers_test.mbt | 141 ++++++------ src/moon.pkg | 1 - src/pkg.generated.mbti | 17 +- src/runtime/moon.pkg | 2 +- src/runtime/pkg.generated.mbti | 3 +- src/runtime/runtime_logger.mbt | 4 +- src/runtime_file_json_test.mbt | 113 +++++----- src/runtime_logger.mbt | 4 +- src/sinks_file.mbt | 6 +- 49 files changed, 533 insertions(+), 601 deletions(-) create mode 100644 src-async/json_test_helpers_test.mbt create mode 100644 src/json_test_helpers_test.mbt diff --git a/docs/api/async-logger-build-config-to-json.md b/docs/api/async-logger-build-config-to-json.md index a53d772..98381dc 100644 --- a/docs/api/async-logger-build-config-to-json.md +++ b/docs/api/async-logger-build-config-to-json.md @@ -13,14 +13,14 @@ key-word: ## Async-logger-build-config-to-json -Convert `AsyncLoggerBuildConfig` into a `JsonValue`. This helper exports both the base synchronous logger config and the async runtime config as one structured payload. +Convert `AsyncLoggerBuildConfig` into a `Json`. This helper exports both the base synchronous logger config and the async runtime config as one structured payload. ### Interface ```moonbit pub fn async_logger_build_config_to_json( config : AsyncLoggerBuildConfig, -) -> @json_parser.JsonValue {} +) -> Json {} ``` #### input @@ -29,7 +29,7 @@ pub fn async_logger_build_config_to_json( #### output -- `JsonValue` - Structured JSON representation of the full async build config. +- `Json` - Structured JSON representation of the full async build config. ### Explanation diff --git a/docs/api/async-logger-config-to-json.md b/docs/api/async-logger-config-to-json.md index a44343e..973ff2c 100644 --- a/docs/api/async-logger-config-to-json.md +++ b/docs/api/async-logger-config-to-json.md @@ -13,12 +13,12 @@ key-word: ## Async-logger-config-to-json -Convert a typed `AsyncLoggerConfig` into a `JsonValue`. This helper exports async queue capacity, overflow policy, batch sizing, linger timing, and flush behavior in a structured form. +Convert a typed `AsyncLoggerConfig` into a `Json`. This helper exports async queue capacity, overflow policy, batch sizing, linger timing, and flush behavior in a structured form. ### Interface ```moonbit -pub fn async_logger_config_to_json(config : AsyncLoggerConfig) -> @json_parser.JsonValue {} +pub fn async_logger_config_to_json(config : AsyncLoggerConfig) -> Json {} ``` #### input @@ -27,7 +27,7 @@ pub fn async_logger_config_to_json(config : AsyncLoggerConfig) -> @json_parser.J #### output -- `JsonValue` - Structured JSON representation of the async config. +- `Json` - Structured JSON representation of the async config. ### Explanation diff --git a/docs/api/async-logger-state-to-json.md b/docs/api/async-logger-state-to-json.md index beff06e..adb23b7 100644 --- a/docs/api/async-logger-state-to-json.md +++ b/docs/api/async-logger-state-to-json.md @@ -13,12 +13,12 @@ key-word: ## Async-logger-state-to-json -Convert `AsyncLoggerState` into a `JsonValue`. This helper is the structured export path for async logger runtime snapshots or manually constructed state values when callers want machine-readable diagnostics instead of a plain string. +Convert `AsyncLoggerState` into a `Json`. This helper is the structured export path for async logger runtime snapshots or manually constructed state values when callers want machine-readable diagnostics instead of a plain string. ### Interface ```moonbit -pub fn async_logger_state_to_json(state : AsyncLoggerState) -> @json_parser.JsonValue {} +pub fn async_logger_state_to_json(state : AsyncLoggerState) -> Json {} ``` #### input @@ -27,7 +27,7 @@ pub fn async_logger_state_to_json(state : AsyncLoggerState) -> @json_parser.Json #### output -- `JsonValue` - Structured JSON representation of the async logger snapshot. +- `Json` - Structured JSON representation of the async logger snapshot. ### Explanation @@ -62,7 +62,7 @@ In this example, callers receive a structured value that can be composed into la When another serializer or pipeline expects a JSON value: ```moonbit let payload = async_logger_state_to_json(logger.state()) -println(@json_parser.stringify(payload)) +println(payload.stringify()) ``` In this example, the helper stays useful even outside the built-in stringify wrapper. diff --git a/docs/api/async-runtime-state-to-json.md b/docs/api/async-runtime-state-to-json.md index 19185c3..1ea04f2 100644 --- a/docs/api/async-runtime-state-to-json.md +++ b/docs/api/async-runtime-state-to-json.md @@ -13,12 +13,12 @@ key-word: ## Async-runtime-state-to-json -Convert `AsyncRuntimeState` into a `JsonValue`. This helper exports the async runtime mode and background worker capability in a structured form. +Convert `AsyncRuntimeState` into a `Json`. This helper exports the async runtime mode and background worker capability in a structured form. ### Interface ```moonbit -pub fn async_runtime_state_to_json(state : AsyncRuntimeState) -> @json_parser.JsonValue {} +pub fn async_runtime_state_to_json(state : AsyncRuntimeState) -> Json {} ``` #### input @@ -27,7 +27,7 @@ pub fn async_runtime_state_to_json(state : AsyncRuntimeState) -> @json_parser.Js #### output -- `JsonValue` - Structured JSON representation of the runtime state. +- `Json` - Structured JSON representation of the runtime state. ### Explanation diff --git a/docs/api/file-rotation-config-to-json.md b/docs/api/file-rotation-config-to-json.md index fd4ed31..03e6f86 100644 --- a/docs/api/file-rotation-config-to-json.md +++ b/docs/api/file-rotation-config-to-json.md @@ -13,12 +13,12 @@ key-word: ## File-rotation-config-to-json -Convert `FileRotation` into a `JsonValue`. On the root `src` facade, this helper forwards to the concrete serializer owned by `src/file_model`. +Convert `FileRotation` into a `Json`. On the root `src` facade, this helper forwards to the concrete serializer owned by `src/file_model`. ### Interface ```moonbit -pub fn file_rotation_config_to_json(config : FileRotation) -> @json_parser.JsonValue {} +pub fn file_rotation_config_to_json(config : FileRotation) -> Json {} ``` #### input @@ -27,7 +27,7 @@ pub fn file_rotation_config_to_json(config : FileRotation) -> @json_parser.JsonV #### output -- `JsonValue` - Structured JSON representation of the rotation policy. +- `Json` - Structured JSON representation of the rotation policy. ### Explanation @@ -82,7 +82,7 @@ e.g.: ### Notes -1. Use this helper when downstream code expects `JsonValue` rather than a typed `FileRotation` value. +1. Use this helper when downstream code expects `Json` rather than a typed `FileRotation` value. 2. Pair it with `file_rotation(...)` when building rotation policy in code before export. diff --git a/docs/api/file-sink-policy-to-json.md b/docs/api/file-sink-policy-to-json.md index c0b148b..da49dab 100644 --- a/docs/api/file-sink-policy-to-json.md +++ b/docs/api/file-sink-policy-to-json.md @@ -13,12 +13,12 @@ key-word: ## File-sink-policy-to-json -Convert `FileSinkPolicy` into a `JsonValue`. On the root `src` facade, this helper forwards to the concrete serializer owned by `src/file_model`. +Convert `FileSinkPolicy` into a `Json`. On the root `src` facade, this helper forwards to the concrete serializer owned by `src/file_model`. ### Interface ```moonbit -pub fn file_sink_policy_to_json(policy : FileSinkPolicy) -> @json_parser.JsonValue {} +pub fn file_sink_policy_to_json(policy : FileSinkPolicy) -> Json {} ``` #### input @@ -27,7 +27,7 @@ pub fn file_sink_policy_to_json(policy : FileSinkPolicy) -> @json_parser.JsonVal #### output -- `JsonValue` - Structured JSON representation of the file policy. +- `Json` - Structured JSON representation of the file policy. ### Explanation @@ -73,7 +73,7 @@ e.g.: ### Notes -1. Use this helper when downstream code expects `JsonValue` rather than text. +1. Use this helper when downstream code expects `Json` rather than text. 2. It pairs naturally with `FileSink::policy()`, `RuntimeSink::file_policy()`, and related default-policy accessors. diff --git a/docs/api/file-sink-state-to-json.md b/docs/api/file-sink-state-to-json.md index 1f78705..76af629 100644 --- a/docs/api/file-sink-state-to-json.md +++ b/docs/api/file-sink-state-to-json.md @@ -13,12 +13,12 @@ key-word: ## File-sink-state-to-json -Convert `FileSinkState` into a `JsonValue`. On the root `src` facade, this helper forwards to the concrete serializer owned by `src/file_model`. +Convert `FileSinkState` into a `Json`. On the root `src` facade, this helper forwards to the concrete serializer owned by `src/file_model`. ### Interface ```moonbit -pub fn file_sink_state_to_json(state : FileSinkState) -> @json_parser.JsonValue {} +pub fn file_sink_state_to_json(state : FileSinkState) -> Json {} ``` #### input @@ -27,7 +27,7 @@ pub fn file_sink_state_to_json(state : FileSinkState) -> @json_parser.JsonValue #### output -- `JsonValue` - Structured JSON representation of the file sink state. +- `Json` - Structured JSON representation of the file sink state. ### Explanation @@ -72,7 +72,7 @@ e.g.: ### Notes -1. Use this helper when diagnostics consumers expect `JsonValue`. +1. Use this helper when diagnostics consumers expect `Json`. 2. It pairs naturally with `FileSink::state()`, `RuntimeSink::file_state()`, and `ConfiguredLogger::file_state()`. diff --git a/docs/api/logger-config-to-json.md b/docs/api/logger-config-to-json.md index 98ea472..85e11b8 100644 --- a/docs/api/logger-config-to-json.md +++ b/docs/api/logger-config-to-json.md @@ -13,12 +13,12 @@ key-word: ## Logger-config-to-json -Convert a typed `LoggerConfig` into a `JsonValue`. This helper is the structured export path when config should be persisted, inspected, or embedded into larger JSON payloads. +Convert a typed `LoggerConfig` into a `Json`. This helper is the structured export path when config should be persisted, inspected, or embedded into larger JSON payloads. ### Interface ```moonbit -pub fn logger_config_to_json(config : LoggerConfig) -> @json_parser.JsonValue {} +pub fn logger_config_to_json(config : LoggerConfig) -> Json {} ``` #### input @@ -27,7 +27,7 @@ pub fn logger_config_to_json(config : LoggerConfig) -> @json_parser.JsonValue {} #### output -- `JsonValue` - JSON representation of the logger config. +- `Json` - JSON representation of the logger config. ### Explanation @@ -73,5 +73,5 @@ e.g.: 1. Use this helper when you need a reusable JSON value rather than a final JSON string. -2. Use `stringify_logger_config(...)` when the next consumer expects JSON text instead of `JsonValue`. +2. Use `stringify_logger_config(...)` when the next consumer expects JSON text instead of `Json`. diff --git a/docs/api/queue-config-to-json.md b/docs/api/queue-config-to-json.md index 31fdded..877a7ef 100644 --- a/docs/api/queue-config-to-json.md +++ b/docs/api/queue-config-to-json.md @@ -13,12 +13,12 @@ key-word: ## Queue-config-to-json -Convert a typed `QueueConfig` into a `JsonValue`. This helper is the structured export path for synchronous queue wrapper configuration when callers want machine-readable config output. +Convert a typed `QueueConfig` into a `Json`. This helper is the structured export path for synchronous queue wrapper configuration when callers want machine-readable config output. ### Interface ```moonbit -pub fn queue_config_to_json(queue : QueueConfig) -> @json_parser.JsonValue {} +pub fn queue_config_to_json(queue : QueueConfig) -> Json {} ``` #### input @@ -27,7 +27,7 @@ pub fn queue_config_to_json(queue : QueueConfig) -> @json_parser.JsonValue {} #### output -- `JsonValue` - Structured JSON representation of the queue config. +- `Json` - Structured JSON representation of the queue config. ### Explanation @@ -74,5 +74,5 @@ e.g.: 1. Use this helper when you need a reusable JSON value rather than a final JSON string. -2. Use `stringify_queue_config(...)` when the next consumer expects text instead of `JsonValue`. +2. Use `stringify_queue_config(...)` when the next consumer expects text instead of `Json`. diff --git a/docs/api/runtime-file-state-to-json.md b/docs/api/runtime-file-state-to-json.md index 96bd7d1..445b084 100644 --- a/docs/api/runtime-file-state-to-json.md +++ b/docs/api/runtime-file-state-to-json.md @@ -13,12 +13,12 @@ key-word: ## Runtime-file-state-to-json -Convert `RuntimeFileState` into a `JsonValue`. On the root `src` facade, this helper forwards to the concrete serializer owned by `src/file_model`. +Convert `RuntimeFileState` into a `Json`. On the root `src` facade, this helper forwards to the concrete serializer owned by `src/file_model`. ### Interface ```moonbit -pub fn runtime_file_state_to_json(state : RuntimeFileState) -> @json_parser.JsonValue {} +pub fn runtime_file_state_to_json(state : RuntimeFileState) -> Json {} ``` #### input @@ -27,7 +27,7 @@ pub fn runtime_file_state_to_json(state : RuntimeFileState) -> @json_parser.Json #### output -- `JsonValue` - Structured JSON representation of the runtime file state. +- `Json` - Structured JSON representation of the runtime file state. ### Explanation diff --git a/docs/api/sink-config-to-json.md b/docs/api/sink-config-to-json.md index 0846af8..85b6b18 100644 --- a/docs/api/sink-config-to-json.md +++ b/docs/api/sink-config-to-json.md @@ -13,12 +13,12 @@ key-word: ## Sink-config-to-json -Convert `SinkConfig` into a `JsonValue`. This helper is used directly for sink export and indirectly when exporting `LoggerConfig`. +Convert `SinkConfig` into a `Json`. This helper is used directly for sink export and indirectly when exporting `LoggerConfig`. ### Interface ```moonbit -pub fn sink_config_to_json(config : SinkConfig) -> @json_parser.JsonValue {} +pub fn sink_config_to_json(config : SinkConfig) -> Json {} ``` #### input @@ -27,7 +27,7 @@ pub fn sink_config_to_json(config : SinkConfig) -> @json_parser.JsonValue {} #### output -- `JsonValue` - JSON representation of the sink configuration. +- `Json` - JSON representation of the sink configuration. ### Explanation diff --git a/docs/api/stringify-async-logger-build-config.md b/docs/api/stringify-async-logger-build-config.md index 6013b49..853f8ed 100644 --- a/docs/api/stringify-async-logger-build-config.md +++ b/docs/api/stringify-async-logger-build-config.md @@ -40,7 +40,7 @@ Detailed rules explaining key parameters and behaviors - `pretty=false` returns compact JSON. - `pretty=true` returns indented JSON for human inspection. - This helper is built on top of `async_logger_build_config_to_json(...)`. -- Internally it serializes the `JsonValue` result with `@json_parser.stringify(...)` or `@json_parser.stringify_pretty(value, 2)`, so the text form stays aligned with the structured async build-config export helper. +- Internally it serializes the `Json` result with `value.stringify(...)` or `value.stringify(indent=2)`, so the text form stays aligned with the structured async build-config export helper. - The output keeps `logger` and `async_config` as separate sections, matching supported parser input. - The serialized `logger` section preserves the full `LoggerConfig` shape, even though `build_async_text_logger(...)` later consumes only the selected text-oriented subset of that logger config. - That includes preserving whatever `logger.sink.kind` text is present in the config, even though the later text-specific builder path still ignores that sink-kind branch and constructs `FormattedConsoleSink` from `logger.sink.text_formatter`. @@ -80,7 +80,7 @@ In this example, compact JSON is returned without extra formatting. ### Error Case e.g.: -- If callers need a `JsonValue` for further composition, they should use `async_logger_build_config_to_json(...)` instead. +- If callers need a `Json` for further composition, they should use `async_logger_build_config_to_json(...)` instead. - If only one layer of config is required, this helper may be broader than necessary. @@ -96,7 +96,7 @@ e.g.: 4. In particular, serialized `logger.sink.kind` text is descriptive config data, not a guarantee that the text-specific builder path will branch on that sink kind later. -5. Use `async_logger_build_config_to_json(...)` when the next consumer still needs a `JsonValue` for composition before final stringification. +5. Use `async_logger_build_config_to_json(...)` when the next consumer still needs a `Json` for composition before final stringification. 6. After parsing, the same text can also feed the application or library facade builders; string export preserves one shared build-config shape, not a direct-builder-only route. diff --git a/docs/api/stringify-async-logger-config.md b/docs/api/stringify-async-logger-config.md index fcebf31..056767b 100644 --- a/docs/api/stringify-async-logger-config.md +++ b/docs/api/stringify-async-logger-config.md @@ -37,7 +37,7 @@ Detailed rules explaining key parameters and behaviors - `pretty=false` returns compact JSON suitable for transport and snapshots. - `pretty=true` returns indented JSON for humans. - This helper is built on top of `async_logger_config_to_json(...)`. -- Internally it serializes the `JsonValue` result with `@json_parser.stringify(...)` or `@json_parser.stringify_pretty(value, 2)`, so the text form stays aligned with the structured async-config export helper. +- Internally it serializes the `Json` result with `value.stringify(...)` or `value.stringify(indent=2)`, so the text form stays aligned with the structured async-config export helper. - The exported text follows the supported async config schema rather than internal queue implementation details. - Canonical policy labels such as `DropNewest` and `Never` are emitted even though the parser also accepts aliases like `DropLatest` and `None`. @@ -68,7 +68,7 @@ In this example, compact JSON is returned without extra formatting. ### Error Case e.g.: -- If callers need a `JsonValue` for composition, they should use `async_logger_config_to_json(...)` instead. +- If callers need a `Json` for composition, they should use `async_logger_config_to_json(...)` instead. - If invalid constructor inputs were normalized earlier, the resulting text contains the normalized config values. @@ -78,5 +78,5 @@ e.g.: 2. If negative `max_pending` semantics matter, remember that the serialized text preserves the config value while runtime queue creation later clamps the queue limit to `0`. -3. Use `async_logger_config_to_json(...)` when the next consumer still needs a `JsonValue` for composition before final stringification. +3. Use `async_logger_config_to_json(...)` when the next consumer still needs a `Json` for composition before final stringification. diff --git a/docs/api/stringify-async-logger-state.md b/docs/api/stringify-async-logger-state.md index b3139ec..6fd3f85 100644 --- a/docs/api/stringify-async-logger-state.md +++ b/docs/api/stringify-async-logger-state.md @@ -40,7 +40,7 @@ Detailed rules explaining key parameters and behaviors - `pretty=false` returns compact JSON. - `pretty=true` returns indented JSON suitable for human diagnostics. - This helper is built on top of `async_logger_state_to_json(...)`. -- Internally it stringifies the same JSON snapshot shape returned by the public export helper, using `@json_parser.stringify(...)` or `@json_parser.stringify_pretty(value, 2)`. +- Internally it stringifies the same JSON snapshot shape returned by the public export helper, using `value.stringify(...)` or `value.stringify(indent=2)`. - The compact form matches snapshots such as `{"runtime":{"mode":"native_worker","background_worker":true},"phase":"ready","pending_count":0,...}`. - String output is convenient for logs, CLI output, and startup diagnostics. - The serializer does not care whether the input came from a live logger read or a synthetic `AsyncLoggerState::new(...)` call. @@ -83,7 +83,7 @@ e.g.: 2. The compact output shape is already locked by the async runtime snapshot tests, so it is suitable for stable diagnostics and assertions. -3. Use `async_logger_state_to_json(...)` when the next consumer still needs a `JsonValue` for composition before final stringification. +3. Use `async_logger_state_to_json(...)` when the next consumer still needs a `Json` for composition before final stringification. 4. Pair it with `AsyncLogger::state()` for current logger diagnostics, or with `AsyncLoggerState::new(...)` when exporting a manual snapshot built by tests or adapters. diff --git a/docs/api/stringify-async-runtime-state.md b/docs/api/stringify-async-runtime-state.md index b551b66..bcf4a8e 100644 --- a/docs/api/stringify-async-runtime-state.md +++ b/docs/api/stringify-async-runtime-state.md @@ -40,7 +40,7 @@ Detailed rules explaining key parameters and behaviors - `pretty=false` returns compact JSON. - `pretty=true` returns indented JSON for human diagnostics. - This helper is built on top of `async_runtime_state_to_json(...)`. -- Internally it serializes the `JsonValue` result with `@json_parser.stringify(...)` or `@json_parser.stringify_pretty(value, 2)`, so the text form stays aligned with the structured runtime-state export helper. +- Internally it serializes the `Json` result with `value.stringify(...)` or `value.stringify(indent=2)`, so the text form stays aligned with the structured runtime-state export helper. - The compact form matches the tested snapshot shape such as `{"mode":"native_worker","background_worker":true}`. - It is well-suited for startup banners, support reports, and target capability logs. @@ -79,5 +79,5 @@ e.g.: 2. This helper is the direct text form of the same two-field runtime snapshot exported by `async_runtime_state_to_json(...)`. -3. Use `async_runtime_state_to_json(...)` when the next consumer still needs a `JsonValue` for composition before final stringification. +3. Use `async_runtime_state_to_json(...)` when the next consumer still needs a `Json` for composition before final stringification. diff --git a/docs/api/stringify-file-sink-policy.md b/docs/api/stringify-file-sink-policy.md index 2e3ce4c..ce93c44 100644 --- a/docs/api/stringify-file-sink-policy.md +++ b/docs/api/stringify-file-sink-policy.md @@ -66,7 +66,7 @@ In this example, compact JSON is produced without extra formatting logic. ### Error Case e.g.: -- If callers need a `JsonValue` for composition rather than text, `file_sink_policy_to_json(...)` is the better API. +- If callers need a `Json` for composition rather than text, `file_sink_policy_to_json(...)` is the better API. - If rotation is disabled, the output still includes `rotation` as `null`. diff --git a/docs/api/stringify-logger-config.md b/docs/api/stringify-logger-config.md index 4b6416d..b0abf1d 100644 --- a/docs/api/stringify-logger-config.md +++ b/docs/api/stringify-logger-config.md @@ -37,7 +37,7 @@ Detailed rules explaining key parameters and behaviors - `pretty=false` produces compact JSON. - `pretty=true` produces indented human-readable JSON. - This helper builds on top of `logger_config_to_json(...)`. -- Internally it serializes the `JsonValue` result with `@json_parser.stringify(...)` or `@json_parser.stringify_pretty(value, 2)`, so the text form stays aligned with the structured logger-config export helper. +- Internally it serializes the `Json` result with `value.stringify(...)` or `value.stringify(indent=2)`, so the text form stays aligned with the structured logger-config export helper. - Output is stable and suited for roundtrip config workflows. ### How to Use @@ -71,7 +71,7 @@ e.g.: ### Notes -1. Use this helper when the next consumer expects JSON text instead of `JsonValue`. +1. Use this helper when the next consumer expects JSON text instead of `Json`. 2. Use `logger_config_to_json(...)` when you still need to embed the config inside a larger JSON object before final stringification. diff --git a/docs/api/stringify-queue-config.md b/docs/api/stringify-queue-config.md index 3892fb7..aee04fb 100644 --- a/docs/api/stringify-queue-config.md +++ b/docs/api/stringify-queue-config.md @@ -37,7 +37,7 @@ Detailed rules explaining key parameters and behaviors - `pretty=false` returns compact JSON suitable for logs and snapshots. - `pretty=true` returns indented JSON for human inspection. - This helper is built on top of `queue_config_to_json(...)`. -- Internally it serializes the `JsonValue` result with `@json_parser.stringify(...)` or `@json_parser.stringify_pretty(value, 2)`, so the text form stays aligned with the structured export helper. +- Internally it serializes the `Json` result with `value.stringify(...)` or `value.stringify(indent=2)`, so the text form stays aligned with the structured export helper. - The resulting text follows the same queue schema accepted by config parsing flows. ### How to Use @@ -67,13 +67,13 @@ In this example, compact JSON is returned without extra formatting. ### Error Case e.g.: -- If callers need a `JsonValue` for further composition, this API is the wrong layer and `queue_config_to_json(...)` should be used instead. +- If callers need a `Json` for further composition, this API is the wrong layer and `queue_config_to_json(...)` should be used instead. - If queue policy is too aggressive for workload burst size, serialization still succeeds because this helper only exports config. ### Notes -1. Use this helper when the next consumer expects JSON text instead of `JsonValue`. +1. Use this helper when the next consumer expects JSON text instead of `Json`. 2. Use `queue_config_to_json(...)` when you still need to embed the queue config inside a larger JSON object before final stringification. diff --git a/docs/api/stringify-runtime-file-state.md b/docs/api/stringify-runtime-file-state.md index 0830aa1..3f73467 100644 --- a/docs/api/stringify-runtime-file-state.md +++ b/docs/api/stringify-runtime-file-state.md @@ -69,7 +69,7 @@ In this example, compact JSON is returned without extra formatting logic. ### Error Case e.g.: -- If callers need a `JsonValue` rather than text, `runtime_file_state_to_json(...)` is the better API. +- If callers need a `Json` rather than text, `runtime_file_state_to_json(...)` is the better API. - If queue wrapping is not relevant, `stringify_file_sink_state(...)` may be the simpler API. diff --git a/docs/api/stringify-sink-config.md b/docs/api/stringify-sink-config.md index 6662136..7cd17a0 100644 --- a/docs/api/stringify-sink-config.md +++ b/docs/api/stringify-sink-config.md @@ -37,7 +37,7 @@ Detailed rules explaining key parameters and behaviors - `pretty=false` gives compact JSON. - `pretty=true` gives indented output. - This helper builds on top of `sink_config_to_json(...)`. -- Internally it serializes the `JsonValue` result with `@json_parser.stringify(...)` or `@json_parser.stringify_pretty(value, 2)`, so the text form stays aligned with the structured sink export helper. +- Internally it serializes the `Json` result with `value.stringify(...)` or `value.stringify(indent=2)`, so the text form stays aligned with the structured sink export helper. - It is useful when examples or generated docs want to show only sink-specific config. ### How to Use @@ -71,7 +71,7 @@ e.g.: ### Notes -1. Use this helper when the next consumer expects JSON text instead of `JsonValue`. +1. Use this helper when the next consumer expects JSON text instead of `Json`. 2. Use `sink_config_to_json(...)` when you still need to embed the sink config inside a larger JSON object before final stringification. diff --git a/docs/api/stringify-text-formatter-config.md b/docs/api/stringify-text-formatter-config.md index 1df2e9b..c58cd5c 100644 --- a/docs/api/stringify-text-formatter-config.md +++ b/docs/api/stringify-text-formatter-config.md @@ -40,7 +40,7 @@ Detailed rules explaining key parameters and behaviors - `pretty=false` returns compact JSON. - `pretty=true` returns indented JSON for humans. - This helper is built on top of `text_formatter_config_to_json(...)`. -- Internally it serializes the `JsonValue` result with `@json_parser.stringify(...)` or `@json_parser.stringify_pretty(value, 2)`, so the text form stays aligned with the structured formatter export helper. +- Internally it serializes the `Json` result with `value.stringify(...)` or `value.stringify(indent=2)`, so the text form stays aligned with the structured formatter export helper. - The output preserves the supported formatter config schema instead of any runtime-only formatter instance details. ### How to Use @@ -76,7 +76,7 @@ e.g.: ### Notes -1. Use this helper when the next consumer expects JSON text instead of `JsonValue`. +1. Use this helper when the next consumer expects JSON text instead of `Json`. 2. Use `text_formatter_config_to_json(...)` when you still need to embed formatter config inside a larger JSON object before final stringification. diff --git a/docs/api/text-formatter-config-to-json.md b/docs/api/text-formatter-config-to-json.md index 518dfe7..9b009bb 100644 --- a/docs/api/text-formatter-config-to-json.md +++ b/docs/api/text-formatter-config-to-json.md @@ -13,12 +13,12 @@ key-word: ## Text-formatter-config-to-json -Convert a typed `TextFormatterConfig` into a `JsonValue`. This helper exports formatter toggles, separators, color settings, markup behavior, and optional style tags in a machine-readable form. +Convert a typed `TextFormatterConfig` into a `Json`. This helper exports formatter toggles, separators, color settings, markup behavior, and optional style tags in a machine-readable form. ### Interface ```moonbit -pub fn text_formatter_config_to_json(config : TextFormatterConfig) -> @json_parser.JsonValue {} +pub fn text_formatter_config_to_json(config : TextFormatterConfig) -> Json {} ``` #### input @@ -27,7 +27,7 @@ pub fn text_formatter_config_to_json(config : TextFormatterConfig) -> @json_pars #### output -- `JsonValue` - Structured JSON representation of the formatter config. +- `Json` - Structured JSON representation of the formatter config. ### Explanation diff --git a/moon.mod b/moon.mod index d84d46c..498eac2 100644 --- a/moon.mod +++ b/moon.mod @@ -3,7 +3,6 @@ name = "Nanaloveyuki/BitLogger" version = "0.7.1" import { - "maria/json_parser@0.1.1", "moonbitlang/async@0.20.2", } diff --git a/src-async/BitLoggerAsync_test.mbt b/src-async/BitLoggerAsync_test.mbt index 77d24a8..7795da7 100644 --- a/src-async/BitLoggerAsync_test.mbt +++ b/src-async/BitLoggerAsync_test.mbt @@ -831,29 +831,29 @@ test "async json helpers export stable structured shapes" { flush=AsyncFlushPolicy::Batch, ), ) - let config_obj = config_json.as_object().unwrap() + let config_obj = config_json.expect_json_object() inspect( - @json_parser.stringify(config_json), + config_json.stringify(), content="{\"max_pending\":8,\"max_batch\":3,\"linger_ms\":25,\"overflow\":\"DropOldest\",\"flush\":\"Batch\"}", ) inspect( - config_obj.get("max_pending").unwrap().as_number().unwrap().to_int(), + config_obj.get("max_pending").unwrap().expect_json_number().to_int(), content="8", ) inspect( - config_obj.get("max_batch").unwrap().as_number().unwrap().to_int(), + config_obj.get("max_batch").unwrap().expect_json_number().to_int(), content="3", ) inspect( - config_obj.get("linger_ms").unwrap().as_number().unwrap().to_int(), + config_obj.get("linger_ms").unwrap().expect_json_number().to_int(), content="25", ) inspect( - config_obj.get("overflow").unwrap().as_string().unwrap(), + config_obj.get("overflow").unwrap().expect_json_string(), content="DropOldest", ) inspect( - config_obj.get("flush").unwrap().as_string().unwrap(), + config_obj.get("flush").unwrap().expect_json_string(), content="Batch", ) @@ -874,85 +874,84 @@ test "async json helpers export stable structured shapes" { ), ), ) - let build_obj = build_json.as_object().unwrap() - let logger_obj = build_obj.get("logger").unwrap().as_object().unwrap() - let sink_obj = logger_obj.get("sink").unwrap().as_object().unwrap() + let build_obj = build_json.expect_json_object() + let logger_obj = build_obj.get("logger").unwrap().expect_json_object() + let sink_obj = logger_obj.get("sink").unwrap().expect_json_object() let formatter_obj = sink_obj .get("text_formatter") .unwrap() - .as_object() - .unwrap() - let async_obj = build_obj.get("async_config").unwrap().as_object().unwrap() + .expect_json_object() + let async_obj = build_obj.get("async_config").unwrap().expect_json_object() inspect( - logger_obj.get("min_level").unwrap().as_string().unwrap(), + logger_obj.get("min_level").unwrap().expect_json_string(), content="WARN", ) inspect( - logger_obj.get("target").unwrap().as_string().unwrap(), + logger_obj.get("target").unwrap().expect_json_string(), content="async.roundtrip", ) inspect( - logger_obj.get("timestamp").unwrap().as_bool().unwrap(), + logger_obj.get("timestamp").unwrap().expect_json_bool(), content="true", ) inspect(logger_obj.get("queue") is None, content="true") inspect( - sink_obj.get("kind").unwrap().as_string().unwrap(), + sink_obj.get("kind").unwrap().expect_json_string(), content="text_console", ) - inspect(sink_obj.get("path").unwrap().as_string().unwrap(), content="") - inspect(sink_obj.get("append").unwrap().as_bool().unwrap(), content="true") + inspect(sink_obj.get("path").unwrap().expect_json_string(), content="") + inspect(sink_obj.get("append").unwrap().expect_json_bool(), content="true") inspect( - sink_obj.get("auto_flush").unwrap().as_bool().unwrap(), + sink_obj.get("auto_flush").unwrap().expect_json_bool(), content="true", ) inspect( - formatter_obj.get("show_timestamp").unwrap().as_bool().unwrap(), + formatter_obj.get("show_timestamp").unwrap().expect_json_bool(), content="true", ) inspect( - formatter_obj.get("separator").unwrap().as_string().unwrap(), + formatter_obj.get("separator").unwrap().expect_json_string(), content=" ", ) inspect( - formatter_obj.get("field_separator").unwrap().as_string().unwrap(), + formatter_obj.get("field_separator").unwrap().expect_json_string(), content=" ", ) inspect( - formatter_obj.get("template").unwrap().as_string().unwrap(), + formatter_obj.get("template").unwrap().expect_json_string(), content="", ) inspect( - formatter_obj.get("color_mode").unwrap().as_string().unwrap(), + formatter_obj.get("color_mode").unwrap().expect_json_string(), content="never", ) inspect( - formatter_obj.get("color_support").unwrap().as_string().unwrap(), + formatter_obj.get("color_support").unwrap().expect_json_string(), content="truecolor", ) inspect( - formatter_obj.get("style_markup").unwrap().as_string().unwrap(), + formatter_obj.get("style_markup").unwrap().expect_json_string(), content="full", ) inspect( - async_obj.get("max_pending").unwrap().as_number().unwrap().to_int(), + async_obj.get("max_pending").unwrap().expect_json_number().to_int(), content="2", ) inspect( - async_obj.get("max_batch").unwrap().as_number().unwrap().to_int(), + async_obj.get("max_batch").unwrap().expect_json_number().to_int(), content="5", ) inspect( - async_obj.get("linger_ms").unwrap().as_number().unwrap().to_int(), + async_obj.get("linger_ms").unwrap().expect_json_number().to_int(), content="40", ) inspect( - async_obj.get("overflow").unwrap().as_string().unwrap(), + async_obj.get("overflow").unwrap().expect_json_string(), content="DropNewest", ) inspect( - async_obj.get("flush").unwrap().as_string().unwrap(), + async_obj.get("flush").unwrap().expect_json_string(), content="Shutdown", ) } @@ -961,103 +960,102 @@ test "async json helpers export stable structured shapes" { test "async build config json export materializes parsed omitted defaults" { let parsed = parse_async_logger_build_config_text("{}") let build_json = async_logger_build_config_to_json(parsed) - let build_obj = build_json.as_object().unwrap() - let logger_obj = build_obj.get("logger").unwrap().as_object().unwrap() - let sink_obj = logger_obj.get("sink").unwrap().as_object().unwrap() + let build_obj = build_json.expect_json_object() + let logger_obj = build_obj.get("logger").unwrap().expect_json_object() + let sink_obj = logger_obj.get("sink").unwrap().expect_json_object() let formatter_obj = sink_obj .get("text_formatter") .unwrap() - .as_object() - .unwrap() - let async_obj = build_obj.get("async_config").unwrap().as_object().unwrap() + .expect_json_object() + let async_obj = build_obj.get("async_config").unwrap().expect_json_object() inspect( - @json_parser.stringify(build_json), + build_json.stringify(), content="{\"logger\":{\"min_level\":\"INFO\",\"target\":\"\",\"timestamp\":false,\"sink\":{\"kind\":\"console\",\"path\":\"\",\"append\":true,\"auto_flush\":true,\"text_formatter\":{\"show_timestamp\":true,\"show_level\":true,\"show_target\":true,\"show_fields\":true,\"separator\":\" \",\"field_separator\":\" \",\"template\":\"\",\"color_mode\":\"never\",\"color_support\":\"truecolor\",\"style_markup\":\"full\",\"target_style_markup\":\"disabled\",\"fields_style_markup\":\"disabled\"}}},\"async_config\":{\"max_pending\":0,\"max_batch\":1,\"linger_ms\":0,\"overflow\":\"Blocking\",\"flush\":\"Never\"}}", ) inspect( - logger_obj.get("min_level").unwrap().as_string().unwrap(), + logger_obj.get("min_level").unwrap().expect_json_string(), content="INFO", ) - inspect(logger_obj.get("target").unwrap().as_string().unwrap(), content="") + inspect(logger_obj.get("target").unwrap().expect_json_string(), content="") inspect( - logger_obj.get("timestamp").unwrap().as_bool().unwrap(), + logger_obj.get("timestamp").unwrap().expect_json_bool(), content="false", ) inspect(logger_obj.get("queue") is None, content="true") - inspect(sink_obj.get("kind").unwrap().as_string().unwrap(), content="console") - inspect(sink_obj.get("path").unwrap().as_string().unwrap(), content="") - inspect(sink_obj.get("append").unwrap().as_bool().unwrap(), content="true") + inspect(sink_obj.get("kind").unwrap().expect_json_string(), content="console") + inspect(sink_obj.get("path").unwrap().expect_json_string(), content="") + inspect(sink_obj.get("append").unwrap().expect_json_bool(), content="true") inspect( - sink_obj.get("auto_flush").unwrap().as_bool().unwrap(), + sink_obj.get("auto_flush").unwrap().expect_json_bool(), content="true", ) inspect( - formatter_obj.get("show_timestamp").unwrap().as_bool().unwrap(), + formatter_obj.get("show_timestamp").unwrap().expect_json_bool(), content="true", ) inspect( - formatter_obj.get("show_level").unwrap().as_bool().unwrap(), + formatter_obj.get("show_level").unwrap().expect_json_bool(), content="true", ) inspect( - formatter_obj.get("show_target").unwrap().as_bool().unwrap(), + formatter_obj.get("show_target").unwrap().expect_json_bool(), content="true", ) inspect( - formatter_obj.get("show_fields").unwrap().as_bool().unwrap(), + formatter_obj.get("show_fields").unwrap().expect_json_bool(), content="true", ) inspect( - formatter_obj.get("separator").unwrap().as_string().unwrap(), + formatter_obj.get("separator").unwrap().expect_json_string(), content=" ", ) inspect( - formatter_obj.get("field_separator").unwrap().as_string().unwrap(), + formatter_obj.get("field_separator").unwrap().expect_json_string(), content=" ", ) inspect( - formatter_obj.get("template").unwrap().as_string().unwrap(), + formatter_obj.get("template").unwrap().expect_json_string(), content="", ) inspect( - formatter_obj.get("color_mode").unwrap().as_string().unwrap(), + formatter_obj.get("color_mode").unwrap().expect_json_string(), content="never", ) inspect( - formatter_obj.get("color_support").unwrap().as_string().unwrap(), + formatter_obj.get("color_support").unwrap().expect_json_string(), content="truecolor", ) inspect( - formatter_obj.get("style_markup").unwrap().as_string().unwrap(), + formatter_obj.get("style_markup").unwrap().expect_json_string(), content="full", ) inspect( - formatter_obj.get("target_style_markup").unwrap().as_string().unwrap(), + formatter_obj.get("target_style_markup").unwrap().expect_json_string(), content="disabled", ) inspect( - formatter_obj.get("fields_style_markup").unwrap().as_string().unwrap(), + formatter_obj.get("fields_style_markup").unwrap().expect_json_string(), content="disabled", ) inspect(formatter_obj.get("style_tags") is None, content="true") inspect( - async_obj.get("max_pending").unwrap().as_number().unwrap().to_int(), + async_obj.get("max_pending").unwrap().expect_json_number().to_int(), content="0", ) inspect( - async_obj.get("max_batch").unwrap().as_number().unwrap().to_int(), + async_obj.get("max_batch").unwrap().expect_json_number().to_int(), content="1", ) inspect( - async_obj.get("linger_ms").unwrap().as_number().unwrap().to_int(), + async_obj.get("linger_ms").unwrap().expect_json_number().to_int(), content="0", ) inspect( - async_obj.get("overflow").unwrap().as_string().unwrap(), + async_obj.get("overflow").unwrap().expect_json_string(), content="Blocking", ) - inspect(async_obj.get("flush").unwrap().as_string().unwrap(), content="Never") + inspect(async_obj.get("flush").unwrap().expect_json_string(), content="Never") } ///| @@ -1068,7 +1066,7 @@ test "async config parsers reject malformed input" { })() catch { err => err.to_string() } - inspect(invalid_json_error.contains("UnexpectedToken"), content="true") + inspect(invalid_json_error != "no error", content="true") let wrong_type_error = (fn() -> String raise { ignore(parse_async_logger_config_text("{\"max_pending\":\"many\"}")) @@ -1144,7 +1142,7 @@ test "async runtime capability helpers stay consistent" { ) inspect(state.background_worker == worker_supported, content="true") inspect( - @json_parser.stringify(async_runtime_state_to_json(state)), + async_runtime_state_to_json(state).stringify(), content=if worker_supported { "{\"mode\":\"native_worker\",\"background_worker\":true}" } else { @@ -1225,7 +1223,7 @@ test "async logger state snapshot reflects current counters and runtime" { content="Shutdown", ) inspect( - @json_parser.stringify(async_logger_state_to_json(state)), + async_logger_state_to_json(state).stringify(), content=if async_runtime_supports_background_worker() { "{\"runtime\":{\"mode\":\"native_worker\",\"background_worker\":true},\"phase\":\"ready\",\"pending_count\":0,\"dropped_count\":0,\"is_closed\":false,\"is_running\":false,\"has_failed\":false,\"backlog_retained\":false,\"can_rerun\":true,\"terminal\":false,\"last_error\":\"\",\"flush_policy\":\"Shutdown\"}" } else { diff --git a/src-async/async_logger_shared.mbt b/src-async/async_logger_shared.mbt index 01827ca..7d62d00 100644 --- a/src-async/async_logger_shared.mbt +++ b/src-async/async_logger_shared.mbt @@ -48,9 +48,7 @@ pub fn async_runtime_state() -> AsyncRuntimeState { } ///| -pub fn async_runtime_state_to_json( - state : AsyncRuntimeState, -) -> @json_parser.JsonValue { +pub fn async_runtime_state_to_json(state : AsyncRuntimeState) -> Json { @utils.async_runtime_state_to_json(state) } @@ -63,9 +61,7 @@ pub fn stringify_async_runtime_state( } ///| -pub fn async_logger_state_to_json( - state : AsyncLoggerState, -) -> @json_parser.JsonValue { +pub fn async_logger_state_to_json(state : AsyncLoggerState) -> Json { @utils.async_logger_state_to_json(state) } @@ -88,9 +84,7 @@ pub fn parse_async_logger_config_text( } ///| -pub fn async_logger_config_to_json( - config : AsyncLoggerConfig, -) -> @json_parser.JsonValue { +pub fn async_logger_config_to_json(config : AsyncLoggerConfig) -> Json { @utils.async_logger_config_to_json(config) } @@ -115,7 +109,7 @@ pub fn parse_async_logger_build_config_text( ///| pub fn async_logger_build_config_to_json( config : AsyncLoggerBuildConfig, -) -> @json_parser.JsonValue { +) -> Json { @utils.async_logger_build_config_to_json(config) } diff --git a/src-async/json_test_helpers_test.mbt b/src-async/json_test_helpers_test.mbt new file mode 100644 index 0000000..fca2862 --- /dev/null +++ b/src-async/json_test_helpers_test.mbt @@ -0,0 +1,32 @@ +///| +fn Json::expect_json_object(self : Json) -> Map[String, Json] { + match self { + Json::Object(value) => value + _ => abort("Expected JSON object") + } +} + +///| +fn Json::expect_json_number(self : Json) -> Double { + match self { + Json::Number(value, ..) => value + _ => abort("Expected JSON number") + } +} + +///| +fn Json::expect_json_string(self : Json) -> String { + match self { + Json::String(value) => value + _ => abort("Expected JSON string") + } +} + +///| +fn Json::expect_json_bool(self : Json) -> Bool { + match self { + Json::True => true + Json::False => false + _ => abort("Expected JSON bool") + } +} diff --git a/src-async/moon.pkg b/src-async/moon.pkg index 4e551c3..e9c9145 100644 --- a/src-async/moon.pkg +++ b/src-async/moon.pkg @@ -1,7 +1,7 @@ import { "Nanaloveyuki/BitLogger/src" @bitlogger, "Nanaloveyuki/BitLogger/src-async/utils", - "maria/json_parser", + "moonbitlang/core/json", "moonbitlang/async", "moonbitlang/async/aqueue", "moonbitlang/core/env", diff --git a/src-async/pkg.generated.mbti b/src-async/pkg.generated.mbti index f50601a..c800288 100644 --- a/src-async/pkg.generated.mbti +++ b/src-async/pkg.generated.mbti @@ -6,7 +6,6 @@ import { "Nanaloveyuki/BitLogger/src/core", "Nanaloveyuki/BitLogger/src/runtime", "Nanaloveyuki/BitLogger/src/sink_graph", - "maria/json_parser", "moonbitlang/async/aqueue", "moonbitlang/core/ref", } @@ -14,11 +13,11 @@ import { // Values pub fn[S] async_logger(S, config? : @utils.AsyncLoggerConfig, min_level? : @core.Level, target? : String, flush? : (S) -> Unit raise) -> AsyncLogger[S] -pub fn async_logger_build_config_to_json(@utils.AsyncLoggerBuildConfig) -> @json_parser.JsonValue +pub fn async_logger_build_config_to_json(@utils.AsyncLoggerBuildConfig) -> Json -pub fn async_logger_config_to_json(@utils.AsyncLoggerConfig) -> @json_parser.JsonValue +pub fn async_logger_config_to_json(@utils.AsyncLoggerConfig) -> Json -pub fn async_logger_state_to_json(@utils.AsyncLoggerState) -> @json_parser.JsonValue +pub fn async_logger_state_to_json(@utils.AsyncLoggerState) -> Json pub fn async_runtime_mode() -> @utils.AsyncRuntimeMode @@ -26,7 +25,7 @@ pub fn async_runtime_mode_label(@utils.AsyncRuntimeMode) -> String pub fn async_runtime_state() -> @utils.AsyncRuntimeState -pub fn async_runtime_state_to_json(@utils.AsyncRuntimeState) -> @json_parser.JsonValue +pub fn async_runtime_state_to_json(@utils.AsyncRuntimeState) -> Json pub fn async_runtime_supports_background_worker() -> Bool diff --git a/src-async/utils/async_models.mbt b/src-async/utils/async_models.mbt index 395f8f0..a6a6c57 100644 --- a/src-async/utils/async_models.mbt +++ b/src-async/utils/async_models.mbt @@ -154,12 +154,10 @@ pub fn AsyncLoggerState::new( } ///| -pub fn async_runtime_state_to_json( - state : AsyncRuntimeState, -) -> @json_parser.JsonValue { - @json_parser.JsonValue::Object({ - "mode": @json_parser.JsonValue::String(async_runtime_mode_label(state.mode)), - "background_worker": @json_parser.JsonValue::Bool(state.background_worker), +pub fn async_runtime_state_to_json(state : AsyncRuntimeState) -> Json { + Json::object({ + "mode": Json::string(async_runtime_mode_label(state.mode)), + "background_worker": Json::boolean(state.background_worker), }) } @@ -170,9 +168,9 @@ pub fn stringify_async_runtime_state( ) -> String { let value = async_runtime_state_to_json(state) if pretty { - @json_parser.stringify_pretty(value, 2) + value.stringify(indent=2) } else { - @json_parser.stringify(value) + value.stringify() } } @@ -186,37 +184,25 @@ fn async_flush_policy_label(policy : AsyncFlushPolicy) -> String { } ///| -fn async_logger_state_to_json_value( - state : AsyncLoggerState, -) -> @json_parser.JsonValue { - @json_parser.JsonValue::Object({ +fn async_logger_state_to_json_value(state : AsyncLoggerState) -> Json { + Json::object({ "runtime": async_runtime_state_to_json(state.runtime), - "phase": @json_parser.JsonValue::String( - async_lifecycle_phase_label(state.phase), - ), - "pending_count": @json_parser.JsonValue::Number( - state.pending_count.to_double(), - ), - "dropped_count": @json_parser.JsonValue::Number( - state.dropped_count.to_double(), - ), - "is_closed": @json_parser.JsonValue::Bool(state.is_closed), - "is_running": @json_parser.JsonValue::Bool(state.is_running), - "has_failed": @json_parser.JsonValue::Bool(state.has_failed), - "backlog_retained": @json_parser.JsonValue::Bool(state.backlog_retained), - "can_rerun": @json_parser.JsonValue::Bool(state.can_rerun), - "terminal": @json_parser.JsonValue::Bool(state.terminal), - "last_error": @json_parser.JsonValue::String(state.last_error), - "flush_policy": @json_parser.JsonValue::String( - async_flush_policy_label(state.flush_policy), - ), + "phase": Json::string(async_lifecycle_phase_label(state.phase)), + "pending_count": Json::number(state.pending_count.to_double()), + "dropped_count": Json::number(state.dropped_count.to_double()), + "is_closed": Json::boolean(state.is_closed), + "is_running": Json::boolean(state.is_running), + "has_failed": Json::boolean(state.has_failed), + "backlog_retained": Json::boolean(state.backlog_retained), + "can_rerun": Json::boolean(state.can_rerun), + "terminal": Json::boolean(state.terminal), + "last_error": Json::string(state.last_error), + "flush_policy": Json::string(async_flush_policy_label(state.flush_policy)), }) } ///| -pub fn async_logger_state_to_json( - state : AsyncLoggerState, -) -> @json_parser.JsonValue { +pub fn async_logger_state_to_json(state : AsyncLoggerState) -> Json { async_logger_state_to_json_value(state) } @@ -227,9 +213,9 @@ pub fn stringify_async_logger_state( ) -> String { let value = async_logger_state_to_json_value(state) if pretty { - @json_parser.stringify_pretty(value, 2) + value.stringify(indent=2) } else { - @json_parser.stringify(value) + value.stringify() } } @@ -293,53 +279,38 @@ fn parse_async_flush(name : String) -> AsyncFlushPolicy raise { pub fn parse_async_logger_config_text( input : String, ) -> AsyncLoggerConfig raise { - let root = @json_parser.parse(input) - let obj = match root.as_object() { - Some(obj) => obj - None => raise Failure::Failure("Expected object for async logger config") + let root = @json.parse(input) + let obj = match root { + Json::Object(obj) => obj + _ => raise Failure::Failure("Expected object for async logger config") } let max_pending = match obj.get("max_pending") { - Some(value) => - match value.as_number() { - Some(number) => number.to_int() - None => - raise Failure::Failure("Expected number at async_config.max_pending") - } + Some(Json::Number(number, ..)) => number.to_int() + Some(_) => + raise Failure::Failure("Expected number at async_config.max_pending") None => 0 } let overflow = match obj.get("overflow") { - Some(value) => - match value.as_string() { - Some(text) => parse_async_overflow(text) - None => - raise Failure::Failure("Expected string at async_config.overflow") - } + Some(Json::String(text)) => parse_async_overflow(text) + Some(_) => + raise Failure::Failure("Expected string at async_config.overflow") None => AsyncOverflowPolicy::Blocking } let max_batch = match obj.get("max_batch") { - Some(value) => - match value.as_number() { - Some(number) => number.to_int() - None => - raise Failure::Failure("Expected number at async_config.max_batch") - } + Some(Json::Number(number, ..)) => number.to_int() + Some(_) => + raise Failure::Failure("Expected number at async_config.max_batch") None => 1 } let linger_ms = match obj.get("linger_ms") { - Some(value) => - match value.as_number() { - Some(number) => number.to_int() - None => - raise Failure::Failure("Expected number at async_config.linger_ms") - } + Some(Json::Number(number, ..)) => number.to_int() + Some(_) => + raise Failure::Failure("Expected number at async_config.linger_ms") None => 0 } let flush = match obj.get("flush") { - Some(value) => - match value.as_string() { - Some(text) => parse_async_flush(text) - None => raise Failure::Failure("Expected string at async_config.flush") - } + Some(Json::String(text)) => parse_async_flush(text) + Some(_) => raise Failure::Failure("Expected string at async_config.flush") None => AsyncFlushPolicy::Never } AsyncLoggerConfig::new( @@ -352,23 +323,19 @@ pub fn parse_async_logger_config_text( } ///| -pub fn async_logger_config_to_json( - config : AsyncLoggerConfig, -) -> @json_parser.JsonValue { - @json_parser.JsonValue::Object({ - "max_pending": @json_parser.JsonValue::Number( - config.max_pending.to_double(), - ), - "max_batch": @json_parser.JsonValue::Number(config.max_batch.to_double()), - "linger_ms": @json_parser.JsonValue::Number(config.linger_ms.to_double()), - "overflow": @json_parser.JsonValue::String( +pub fn async_logger_config_to_json(config : AsyncLoggerConfig) -> Json { + Json::object({ + "max_pending": Json::number(config.max_pending.to_double()), + "max_batch": Json::number(config.max_batch.to_double()), + "linger_ms": Json::number(config.linger_ms.to_double()), + "overflow": Json::string( match config.overflow { AsyncOverflowPolicy::Blocking => "Blocking" AsyncOverflowPolicy::DropOldest => "DropOldest" AsyncOverflowPolicy::DropNewest => "DropNewest" }, ), - "flush": @json_parser.JsonValue::String( + "flush": Json::string( match config.flush { AsyncFlushPolicy::Never => "Never" AsyncFlushPolicy::Batch => "Batch" @@ -385,9 +352,9 @@ pub fn stringify_async_logger_config( ) -> String { let value = async_logger_config_to_json(config) if pretty { - @json_parser.stringify_pretty(value, 2) + value.stringify(indent=2) } else { - @json_parser.stringify(value) + value.stringify() } } @@ -409,21 +376,20 @@ pub fn AsyncLoggerBuildConfig::new( pub fn parse_async_logger_build_config_text( input : String, ) -> AsyncLoggerBuildConfig raise { - let root = @json_parser.parse(input) - let obj = match root.as_object() { - Some(obj) => obj - None => + let root = @json.parse(input) + let obj = match root { + Json::Object(obj) => obj + _ => raise Failure::Failure( "Expected object at async logger build config root", ) } let logger = match obj.get("logger") { - Some(value) => - @bitlogger.parse_logger_config_text(@json_parser.stringify(value)) + Some(value) => @bitlogger.parse_logger_config_text(value.stringify()) None => @bitlogger.default_logger_config() } let async_config = match obj.get("async_config") { - Some(value) => parse_async_logger_config_text(@json_parser.stringify(value)) + Some(value) => parse_async_logger_config_text(value.stringify()) None => AsyncLoggerConfig::new() } AsyncLoggerBuildConfig::new(logger~, async_config~) @@ -432,8 +398,8 @@ pub fn parse_async_logger_build_config_text( ///| pub fn async_logger_build_config_to_json( config : AsyncLoggerBuildConfig, -) -> @json_parser.JsonValue { - @json_parser.JsonValue::Object({ +) -> Json { + Json::object({ "logger": @bitlogger.logger_config_to_json(config.logger), "async_config": async_logger_config_to_json(config.async_config), }) @@ -446,8 +412,8 @@ pub fn stringify_async_logger_build_config( ) -> String { let value = async_logger_build_config_to_json(config) if pretty { - @json_parser.stringify_pretty(value, 2) + value.stringify(indent=2) } else { - @json_parser.stringify(value) + value.stringify() } } diff --git a/src-async/utils/moon.pkg b/src-async/utils/moon.pkg index 81bcffe..04d87d5 100644 --- a/src-async/utils/moon.pkg +++ b/src-async/utils/moon.pkg @@ -1,4 +1,4 @@ import { "Nanaloveyuki/BitLogger/src" @bitlogger, - "maria/json_parser", + "moonbitlang/core/json", } diff --git a/src-async/utils/pkg.generated.mbti b/src-async/utils/pkg.generated.mbti index 2783dc0..743ab58 100644 --- a/src-async/utils/pkg.generated.mbti +++ b/src-async/utils/pkg.generated.mbti @@ -3,19 +3,18 @@ package "Nanaloveyuki/BitLogger/src-async/utils" import { "Nanaloveyuki/BitLogger/src/config_model", - "maria/json_parser", } // Values -pub fn async_logger_build_config_to_json(AsyncLoggerBuildConfig) -> @json_parser.JsonValue +pub fn async_logger_build_config_to_json(AsyncLoggerBuildConfig) -> Json -pub fn async_logger_config_to_json(AsyncLoggerConfig) -> @json_parser.JsonValue +pub fn async_logger_config_to_json(AsyncLoggerConfig) -> Json -pub fn async_logger_state_to_json(AsyncLoggerState) -> @json_parser.JsonValue +pub fn async_logger_state_to_json(AsyncLoggerState) -> Json pub fn async_runtime_mode_label(AsyncRuntimeMode) -> String -pub fn async_runtime_state_to_json(AsyncRuntimeState) -> @json_parser.JsonValue +pub fn async_runtime_state_to_json(AsyncRuntimeState) -> Json pub fn compatibility_async_runtime_mode() -> AsyncRuntimeMode diff --git a/src/config.mbt b/src/config.mbt index ad04dda..ed1b321 100644 --- a/src/config.mbt +++ b/src/config.mbt @@ -39,7 +39,7 @@ pub fn parse_logger_config_text( } ///| -pub fn queue_config_to_json(queue : QueueConfig) -> @json_parser.JsonValue { +pub fn queue_config_to_json(queue : QueueConfig) -> Json { @config_model.queue_config_to_json(queue) } @@ -52,9 +52,7 @@ pub fn stringify_queue_config( } ///| -pub fn text_formatter_config_to_json( - config : TextFormatterConfig, -) -> @json_parser.JsonValue { +pub fn text_formatter_config_to_json(config : TextFormatterConfig) -> Json { @config_model.text_formatter_config_to_json(config) } @@ -67,14 +65,12 @@ pub fn stringify_text_formatter_config( } ///| -pub fn file_rotation_config_to_json( - config : FileRotation, -) -> @json_parser.JsonValue { +pub fn file_rotation_config_to_json(config : FileRotation) -> Json { @file_model.file_rotation_config_to_json(config) } ///| -pub fn sink_config_to_json(config : SinkConfig) -> @json_parser.JsonValue { +pub fn sink_config_to_json(config : SinkConfig) -> Json { @config_model.sink_config_to_json(config) } @@ -87,7 +83,7 @@ pub fn stringify_sink_config( } ///| -pub fn logger_config_to_json(config : LoggerConfig) -> @json_parser.JsonValue { +pub fn logger_config_to_json(config : LoggerConfig) -> Json { @config_model.logger_config_to_json(config) } diff --git a/src/config_model/config.mbt b/src/config_model/config.mbt index ac3f43b..cef7ad8 100644 --- a/src/config_model/config.mbt +++ b/src/config_model/config.mbt @@ -170,102 +170,84 @@ pub fn default_logger_config() -> LoggerConfig { ///| fn expect_object( - value : @json_parser.JsonValue, + value : Json, context : String, -) -> Map[String, @json_parser.JsonValue] raise ConfigError { - match value.as_object() { - Some(obj) => obj - None => raise ConfigError::InvalidConfig("Expected object at " + context) +) -> Map[String, Json] raise ConfigError { + match value { + Json::Object(obj) => obj + _ => raise ConfigError::InvalidConfig("Expected object at " + context) } } ///| fn get_string( - obj : Map[String, @json_parser.JsonValue], + obj : Map[String, Json], key : String, default? : String = "", ) -> String raise ConfigError { match obj.get(key) { None => default - Some(value) => - match value.as_string() { - Some(text) => text - None => - raise ConfigError::InvalidConfig("Expected string at key " + key) - } + Some(Json::String(text)) => text + Some(_) => raise ConfigError::InvalidConfig("Expected string at key " + key) } } ///| fn get_optional_string( - obj : Map[String, @json_parser.JsonValue], + obj : Map[String, Json], key : String, ) -> String? raise ConfigError { match obj.get(key) { None => None - Some(value) => - match value.as_string() { - Some(text) => Some(text) - None => - raise ConfigError::InvalidConfig("Expected string at key " + key) - } + Some(Json::String(text)) => Some(text) + Some(_) => raise ConfigError::InvalidConfig("Expected string at key " + key) } } ///| fn get_bool( - obj : Map[String, @json_parser.JsonValue], + obj : Map[String, Json], key : String, default~ : Bool, ) -> Bool raise ConfigError { match obj.get(key) { None => default - Some(value) => - match value.as_bool() { - Some(flag) => flag - None => raise ConfigError::InvalidConfig("Expected bool at key " + key) - } + Some(Json::True) => true + Some(Json::False) => false + Some(_) => raise ConfigError::InvalidConfig("Expected bool at key " + key) } } ///| fn get_int( - obj : Map[String, @json_parser.JsonValue], + obj : Map[String, Json], key : String, default~ : Int, ) -> Int raise ConfigError { match obj.get(key) { None => default - Some(value) => - match value.as_number() { - Some(number) => number.to_int() - None => - raise ConfigError::InvalidConfig("Expected number at key " + key) - } + Some(Json::Number(number, ..)) => number.to_int() + Some(_) => raise ConfigError::InvalidConfig("Expected number at key " + key) } } ///| fn get_optional_int64_string( - obj : Map[String, @json_parser.JsonValue], + obj : Map[String, Json], key : String, ) -> Int64? raise ConfigError { match obj.get(key) { None => None - Some(value) => - match value.as_string() { - Some(text) => - Some( - @string.parse_int64(text) catch { - _ => - raise ConfigError::InvalidConfig( - "Expected int64 string at key " + key, - ) - }, - ) - None => - raise ConfigError::InvalidConfig("Expected string at key " + key) - } + Some(Json::String(text)) => + Some( + @string.parse_int64(text) catch { + _ => + raise ConfigError::InvalidConfig( + "Expected int64 string at key " + key, + ) + }, + ) + Some(_) => raise ConfigError::InvalidConfig("Expected string at key " + key) } } @@ -355,7 +337,7 @@ fn sink_kind_label(kind : SinkKind) -> String { ///| fn parse_text_formatter_config( - value : @json_parser.JsonValue, + value : Json, ) -> TextFormatterConfig raise ConfigError { let obj = expect_object(value, "text_formatter") TextFormatterConfig::new( @@ -388,7 +370,7 @@ fn parse_text_formatter_config( ///| fn parse_text_style_config( - value : @json_parser.JsonValue, + value : Json, context : String, ) -> @formatting.TextStyle raise ConfigError { let obj = expect_object(value, context) @@ -404,7 +386,7 @@ fn parse_text_style_config( ///| fn parse_style_tags_config( - value : @json_parser.JsonValue, + value : Json, ) -> Map[String, @formatting.TextStyle] raise ConfigError { let obj = expect_object(value, "text_formatter.style_tags") let style_tags : Map[String, @formatting.TextStyle] = Map([]) @@ -418,9 +400,7 @@ fn parse_style_tags_config( } ///| -fn parse_queue_config( - value : @json_parser.JsonValue, -) -> QueueConfig raise ConfigError { +fn parse_queue_config(value : Json) -> QueueConfig raise ConfigError { let obj = expect_object(value, "queue") QueueConfig::new( get_int(obj, "max_pending", default=0), @@ -430,7 +410,7 @@ fn parse_queue_config( ///| fn parse_file_rotation_config( - value : @json_parser.JsonValue, + value : Json, ) -> @file_model.FileRotation raise ConfigError { let obj = expect_object(value, "sink.rotation") let max_backups = get_int(obj, "max_backups", default=1) @@ -446,9 +426,7 @@ fn parse_file_rotation_config( } ///| -fn parse_sink_config( - value : @json_parser.JsonValue, -) -> SinkConfig raise ConfigError { +fn parse_sink_config(value : Json) -> SinkConfig raise ConfigError { let obj = expect_object(value, "sink") let kind = parse_sink_kind(get_string(obj, "kind", default="console")) let formatter = match obj.get("text_formatter") { @@ -480,7 +458,7 @@ fn parse_sink_config( pub fn parse_logger_config_text( input : String, ) -> LoggerConfig raise ConfigError { - let root = @json_parser.parse(input) catch { + let root = @json.parse(input) catch { e => raise ConfigError::InvalidConfig("Invalid JSON: " + e.to_string()) } let obj = expect_object(root, "root") @@ -500,10 +478,10 @@ pub fn parse_logger_config_text( } ///| -pub fn queue_config_to_json(queue : QueueConfig) -> @json_parser.JsonValue { - @json_parser.JsonValue::Object({ - "max_pending": @json_parser.JsonValue::Number(queue.max_pending.to_double()), - "overflow": @json_parser.JsonValue::String( +pub fn queue_config_to_json(queue : QueueConfig) -> Json { + Json::object({ + "max_pending": Json::number(queue.max_pending.to_double()), + "overflow": Json::string( match queue.overflow { @queue_model.QueueOverflowPolicy::DropNewest => "DropNewest" @queue_model.QueueOverflowPolicy::DropOldest => "DropOldest" @@ -519,76 +497,70 @@ pub fn stringify_queue_config( ) -> String { let value = queue_config_to_json(queue) if pretty { - @json_parser.stringify_pretty(value, 2) + value.stringify(indent=2) } else { - @json_parser.stringify(value) + value.stringify() } } ///| -pub fn text_formatter_config_to_json( - config : TextFormatterConfig, -) -> @json_parser.JsonValue { - let obj : Map[String, @json_parser.JsonValue] = { - "show_timestamp": @json_parser.JsonValue::Bool(config.show_timestamp), - "show_level": @json_parser.JsonValue::Bool(config.show_level), - "show_target": @json_parser.JsonValue::Bool(config.show_target), - "show_fields": @json_parser.JsonValue::Bool(config.show_fields), - "separator": @json_parser.JsonValue::String(config.separator), - "field_separator": @json_parser.JsonValue::String(config.field_separator), - "template": @json_parser.JsonValue::String(config.template), - "color_mode": @json_parser.JsonValue::String( - @formatting.color_mode_label(config.color_mode), - ), - "color_support": @json_parser.JsonValue::String( +pub fn text_formatter_config_to_json(config : TextFormatterConfig) -> Json { + let obj : Map[String, Json] = { + "show_timestamp": Json::boolean(config.show_timestamp), + "show_level": Json::boolean(config.show_level), + "show_target": Json::boolean(config.show_target), + "show_fields": Json::boolean(config.show_fields), + "separator": Json::string(config.separator), + "field_separator": Json::string(config.field_separator), + "template": Json::string(config.template), + "color_mode": Json::string(@formatting.color_mode_label(config.color_mode)), + "color_support": Json::string( @formatting.color_support_label(config.color_support), ), - "style_markup": @json_parser.JsonValue::String( + "style_markup": Json::string( @formatting.style_markup_mode_label(config.style_markup), ), - "target_style_markup": @json_parser.JsonValue::String( + "target_style_markup": Json::string( @formatting.style_markup_mode_label(config.target_style_markup), ), - "fields_style_markup": @json_parser.JsonValue::String( + "fields_style_markup": Json::string( @formatting.style_markup_mode_label(config.fields_style_markup), ), } if config.style_tags.length() != 0 { obj["style_tags"] = style_tags_config_to_json(config.style_tags) } - @json_parser.JsonValue::Object(obj) + Json::object(obj) } ///| -fn text_style_config_to_json( - style : @formatting.TextStyle, -) -> @json_parser.JsonValue { - let obj : Map[String, @json_parser.JsonValue] = { - "bold": @json_parser.JsonValue::Bool(style.bold), - "dim": @json_parser.JsonValue::Bool(style.dim), - "italic": @json_parser.JsonValue::Bool(style.italic), - "underline": @json_parser.JsonValue::Bool(style.underline), +fn text_style_config_to_json(style : @formatting.TextStyle) -> Json { + let obj : Map[String, Json] = { + "bold": Json::boolean(style.bold), + "dim": Json::boolean(style.dim), + "italic": Json::boolean(style.italic), + "underline": Json::boolean(style.underline), } match style.fg { - Some(value) => obj["fg"] = @json_parser.JsonValue::String(value) + Some(value) => obj["fg"] = Json::string(value) None => () } match style.bg { - Some(value) => obj["bg"] = @json_parser.JsonValue::String(value) + Some(value) => obj["bg"] = Json::string(value) None => () } - @json_parser.JsonValue::Object(obj) + Json::object(obj) } ///| fn style_tags_config_to_json( style_tags : Map[String, @formatting.TextStyle], -) -> @json_parser.JsonValue { - let obj : Map[String, @json_parser.JsonValue] = Map([]) +) -> Json { + let obj : Map[String, Json] = Map([]) for name, style in style_tags { obj[name] = text_style_config_to_json(style) } - @json_parser.JsonValue::Object(obj) + Json::object(obj) } ///| @@ -598,19 +570,19 @@ pub fn stringify_text_formatter_config( ) -> String { let value = text_formatter_config_to_json(config) if pretty { - @json_parser.stringify_pretty(value, 2) + value.stringify(indent=2) } else { - @json_parser.stringify(value) + value.stringify() } } ///| -pub fn sink_config_to_json(config : SinkConfig) -> @json_parser.JsonValue { - let obj : Map[String, @json_parser.JsonValue] = { - "kind": @json_parser.JsonValue::String(sink_kind_label(config.kind)), - "path": @json_parser.JsonValue::String(config.path), - "append": @json_parser.JsonValue::Bool(config.append), - "auto_flush": @json_parser.JsonValue::Bool(config.auto_flush), +pub fn sink_config_to_json(config : SinkConfig) -> Json { + let obj : Map[String, Json] = { + "kind": Json::string(sink_kind_label(config.kind)), + "path": Json::string(config.path), + "append": Json::boolean(config.append), + "auto_flush": Json::boolean(config.auto_flush), "text_formatter": text_formatter_config_to_json(config.text_formatter), } match config.rotation { @@ -618,7 +590,7 @@ pub fn sink_config_to_json(config : SinkConfig) -> @json_parser.JsonValue { Some(rotation) => obj["rotation"] = @file_model.file_rotation_config_to_json(rotation) } - @json_parser.JsonValue::Object(obj) + Json::object(obj) } ///| @@ -628,25 +600,25 @@ pub fn stringify_sink_config( ) -> String { let value = sink_config_to_json(config) if pretty { - @json_parser.stringify_pretty(value, 2) + value.stringify(indent=2) } else { - @json_parser.stringify(value) + value.stringify() } } ///| -pub fn logger_config_to_json(config : LoggerConfig) -> @json_parser.JsonValue { - let obj : Map[String, @json_parser.JsonValue] = { - "min_level": @json_parser.JsonValue::String(config.min_level.label()), - "target": @json_parser.JsonValue::String(config.target), - "timestamp": @json_parser.JsonValue::Bool(config.timestamp), +pub fn logger_config_to_json(config : LoggerConfig) -> Json { + let obj : Map[String, Json] = { + "min_level": Json::string(config.min_level.label()), + "target": Json::string(config.target), + "timestamp": Json::boolean(config.timestamp), "sink": sink_config_to_json(config.sink), } match config.queue { None => () Some(queue) => obj["queue"] = queue_config_to_json(queue) } - @json_parser.JsonValue::Object(obj) + Json::object(obj) } ///| @@ -656,8 +628,8 @@ pub fn stringify_logger_config( ) -> String { let value = logger_config_to_json(config) if pretty { - @json_parser.stringify_pretty(value, 2) + value.stringify(indent=2) } else { - @json_parser.stringify(value) + value.stringify() } } diff --git a/src/config_model/moon.pkg b/src/config_model/moon.pkg index 36149ca..067177f 100644 --- a/src/config_model/moon.pkg +++ b/src/config_model/moon.pkg @@ -3,6 +3,6 @@ import { "Nanaloveyuki/BitLogger/src/file_model", "Nanaloveyuki/BitLogger/src/formatting", "Nanaloveyuki/BitLogger/src/queue_model", - "maria/json_parser", + "moonbitlang/core/json", "moonbitlang/core/string", } diff --git a/src/config_model/pkg.generated.mbti b/src/config_model/pkg.generated.mbti index d4b3cad..00eb9e9 100644 --- a/src/config_model/pkg.generated.mbti +++ b/src/config_model/pkg.generated.mbti @@ -6,7 +6,6 @@ import { "Nanaloveyuki/BitLogger/src/file_model", "Nanaloveyuki/BitLogger/src/formatting", "Nanaloveyuki/BitLogger/src/queue_model", - "maria/json_parser", } // Values @@ -16,13 +15,13 @@ pub fn default_sink_config() -> SinkConfig pub fn default_text_formatter_config() -> TextFormatterConfig -pub fn logger_config_to_json(LoggerConfig) -> @json_parser.JsonValue +pub fn logger_config_to_json(LoggerConfig) -> Json pub fn parse_logger_config_text(String) -> LoggerConfig raise ConfigError -pub fn queue_config_to_json(QueueConfig) -> @json_parser.JsonValue +pub fn queue_config_to_json(QueueConfig) -> Json -pub fn sink_config_to_json(SinkConfig) -> @json_parser.JsonValue +pub fn sink_config_to_json(SinkConfig) -> Json pub fn stringify_logger_config(LoggerConfig, pretty? : Bool) -> String @@ -32,7 +31,7 @@ pub fn stringify_sink_config(SinkConfig, pretty? : Bool) -> String pub fn stringify_text_formatter_config(TextFormatterConfig, pretty? : Bool) -> String -pub fn text_formatter_config_to_json(TextFormatterConfig) -> @json_parser.JsonValue +pub fn text_formatter_config_to_json(TextFormatterConfig) -> Json // Errors pub(all) suberror ConfigError { diff --git a/src/file_model/file_json.mbt b/src/file_model/file_json.mbt index 17d6550..e6a5b3f 100644 --- a/src/file_model/file_json.mbt +++ b/src/file_model/file_json.mbt @@ -1,34 +1,27 @@ ///| -pub fn file_rotation_config_to_json( - config : FileRotation, -) -> @json_parser.JsonValue { - let obj : Map[String, @json_parser.JsonValue] = { - "max_bytes": @json_parser.JsonValue::Number(config.max_bytes.to_double()), - "max_backups": @json_parser.JsonValue::Number( - config.max_backups.to_double(), - ), +pub fn file_rotation_config_to_json(config : FileRotation) -> Json { + let obj : Map[String, Json] = { + "max_bytes": Json::number(config.max_bytes.to_double()), + "max_backups": Json::number(config.max_backups.to_double()), } match config.native_wide_max_bytes { - Some(value) => - obj["max_bytes_i64"] = @json_parser.JsonValue::String(value.to_string()) + Some(value) => obj["max_bytes_i64"] = Json::string(value.to_string()) None => () } - @json_parser.JsonValue::Object(obj) + Json::object(obj) } ///| -pub fn file_sink_policy_to_json( - policy : FileSinkPolicy, -) -> @json_parser.JsonValue { - let obj : Map[String, @json_parser.JsonValue] = { - "append": @json_parser.JsonValue::Bool(policy.append), - "auto_flush": @json_parser.JsonValue::Bool(policy.auto_flush), +pub fn file_sink_policy_to_json(policy : FileSinkPolicy) -> Json { + let obj : Map[String, Json] = { + "append": Json::boolean(policy.append), + "auto_flush": Json::boolean(policy.auto_flush), } match policy.rotation { - None => obj["rotation"] = @json_parser.JsonValue::Null + None => obj["rotation"] = Json::null() Some(rotation) => obj["rotation"] = file_rotation_config_to_json(rotation) } - @json_parser.JsonValue::Object(obj) + Json::object(obj) } ///| @@ -38,37 +31,29 @@ pub fn stringify_file_sink_policy( ) -> String { let value = file_sink_policy_to_json(policy) if pretty { - @json_parser.stringify_pretty(value, 2) + value.stringify(indent=2) } else { - @json_parser.stringify(value) + value.stringify() } } ///| -pub fn file_sink_state_to_json(state : FileSinkState) -> @json_parser.JsonValue { - let obj : Map[String, @json_parser.JsonValue] = { - "path": @json_parser.JsonValue::String(state.path), - "available": @json_parser.JsonValue::Bool(state.available), - "append": @json_parser.JsonValue::Bool(state.append), - "auto_flush": @json_parser.JsonValue::Bool(state.auto_flush), - "open_failures": @json_parser.JsonValue::Number( - state.open_failures.to_double(), - ), - "write_failures": @json_parser.JsonValue::Number( - state.write_failures.to_double(), - ), - "flush_failures": @json_parser.JsonValue::Number( - state.flush_failures.to_double(), - ), - "rotation_failures": @json_parser.JsonValue::Number( - state.rotation_failures.to_double(), - ), +pub fn file_sink_state_to_json(state : FileSinkState) -> Json { + let obj : Map[String, Json] = { + "path": Json::string(state.path), + "available": Json::boolean(state.available), + "append": Json::boolean(state.append), + "auto_flush": Json::boolean(state.auto_flush), + "open_failures": Json::number(state.open_failures.to_double()), + "write_failures": Json::number(state.write_failures.to_double()), + "flush_failures": Json::number(state.flush_failures.to_double()), + "rotation_failures": Json::number(state.rotation_failures.to_double()), } match state.rotation { - None => obj["rotation"] = @json_parser.JsonValue::Null + None => obj["rotation"] = Json::null() Some(rotation) => obj["rotation"] = file_rotation_config_to_json(rotation) } - @json_parser.JsonValue::Object(obj) + Json::object(obj) } ///| @@ -78,8 +63,8 @@ pub fn stringify_file_sink_state( ) -> String { let value = file_sink_state_to_json(state) if pretty { - @json_parser.stringify_pretty(value, 2) + value.stringify(indent=2) } else { - @json_parser.stringify(value) + value.stringify() } } diff --git a/src/file_model/moon.pkg b/src/file_model/moon.pkg index 5ab2ccf..f9f1d1a 100644 --- a/src/file_model/moon.pkg +++ b/src/file_model/moon.pkg @@ -1,3 +1,3 @@ import { - "maria/json_parser", + "moonbitlang/core/json", } diff --git a/src/file_model/pkg.generated.mbti b/src/file_model/pkg.generated.mbti index c56ba27..ce21120 100644 --- a/src/file_model/pkg.generated.mbti +++ b/src/file_model/pkg.generated.mbti @@ -1,22 +1,18 @@ // Generated using `moon info`, DON'T EDIT IT package "Nanaloveyuki/BitLogger/src/file_model" -import { - "maria/json_parser", -} - // Values pub fn file_rotation(Int, max_backups? : Int) -> FileRotation -pub fn file_rotation_config_to_json(FileRotation) -> @json_parser.JsonValue +pub fn file_rotation_config_to_json(FileRotation) -> Json pub fn file_rotation_i64(Int64, max_backups? : Int) -> FileRotation -pub fn file_sink_policy_to_json(FileSinkPolicy) -> @json_parser.JsonValue +pub fn file_sink_policy_to_json(FileSinkPolicy) -> Json -pub fn file_sink_state_to_json(FileSinkState) -> @json_parser.JsonValue +pub fn file_sink_state_to_json(FileSinkState) -> Json -pub fn runtime_file_state_to_json(RuntimeFileState) -> @json_parser.JsonValue +pub fn runtime_file_state_to_json(RuntimeFileState) -> Json pub fn stringify_file_sink_policy(FileSinkPolicy, pretty? : Bool) -> String diff --git a/src/file_model/runtime_file_state.mbt b/src/file_model/runtime_file_state.mbt index a1231cf..2f3f2ba 100644 --- a/src/file_model/runtime_file_state.mbt +++ b/src/file_model/runtime_file_state.mbt @@ -17,18 +17,12 @@ pub fn RuntimeFileState::new( } ///| -pub fn runtime_file_state_to_json( - state : RuntimeFileState, -) -> @json_parser.JsonValue { - @json_parser.JsonValue::Object({ +pub fn runtime_file_state_to_json(state : RuntimeFileState) -> Json { + Json::object({ "file": file_sink_state_to_json(state.file), - "queued": @json_parser.JsonValue::Bool(state.queued), - "pending_count": @json_parser.JsonValue::Number( - state.pending_count.to_double(), - ), - "dropped_count": @json_parser.JsonValue::Number( - state.dropped_count.to_double(), - ), + "queued": Json::boolean(state.queued), + "pending_count": Json::number(state.pending_count.to_double()), + "dropped_count": Json::number(state.dropped_count.to_double()), }) } @@ -39,8 +33,8 @@ pub fn stringify_runtime_file_state( ) -> String { let value = runtime_file_state_to_json(state) if pretty { - @json_parser.stringify_pretty(value, 2) + value.stringify(indent=2) } else { - @json_parser.stringify(value) + value.stringify() } } diff --git a/src/json_test_helpers_test.mbt b/src/json_test_helpers_test.mbt new file mode 100644 index 0000000..fca2862 --- /dev/null +++ b/src/json_test_helpers_test.mbt @@ -0,0 +1,32 @@ +///| +fn Json::expect_json_object(self : Json) -> Map[String, Json] { + match self { + Json::Object(value) => value + _ => abort("Expected JSON object") + } +} + +///| +fn Json::expect_json_number(self : Json) -> Double { + match self { + Json::Number(value, ..) => value + _ => abort("Expected JSON number") + } +} + +///| +fn Json::expect_json_string(self : Json) -> String { + match self { + Json::String(value) => value + _ => abort("Expected JSON string") + } +} + +///| +fn Json::expect_json_bool(self : Json) -> Bool { + match self { + Json::True => true + Json::False => false + _ => abort("Expected JSON bool") + } +} diff --git a/src/logger_config_json_helpers_test.mbt b/src/logger_config_json_helpers_test.mbt index 87b8c33..3b1e15b 100644 --- a/src/logger_config_json_helpers_test.mbt +++ b/src/logger_config_json_helpers_test.mbt @@ -43,11 +43,7 @@ test "config subtype json helpers stringify stable shapes" { content="{\"kind\":\"file\",\"path\":\"logs/demo.log\",\"append\":false,\"auto_flush\":false,\"text_formatter\":{\"show_timestamp\":false,\"show_level\":true,\"show_target\":true,\"show_fields\":true,\"separator\":\" \",\"field_separator\":\" \",\"template\":\"\",\"color_mode\":\"auto\",\"color_support\":\"truecolor\",\"style_markup\":\"full\",\"target_style_markup\":\"disabled\",\"fields_style_markup\":\"disabled\"},\"rotation\":{\"max_bytes\":128,\"max_backups\":2}}", ) inspect( - @json_parser.stringify( - file_rotation_config_to_json( - file_rotation_i64(4294967296L, max_backups=2), - ), - ), + file_rotation_config_to_json(file_rotation_i64(4294967296L, max_backups=2)).stringify(), content="{\"max_bytes\":2147483647,\"max_backups\":2,\"max_bytes_i64\":\"4294967296\"}", ) } @@ -57,17 +53,17 @@ test "config json helpers export stable structured shapes" { let queue_json = queue_config_to_json( QueueConfig::new(8, overflow=QueueOverflowPolicy::DropOldest), ) - let queue_obj = queue_json.as_object().unwrap() + let queue_obj = queue_json.expect_json_object() inspect( - @json_parser.stringify(queue_json), + queue_json.stringify(), content="{\"max_pending\":8,\"overflow\":\"DropOldest\"}", ) inspect( - queue_obj.get("max_pending").unwrap().as_number().unwrap().to_int(), + queue_obj.get("max_pending").unwrap().expect_json_number().to_int(), content="8", ) inspect( - queue_obj.get("overflow").unwrap().as_string().unwrap(), + queue_obj.get("overflow").unwrap().expect_json_string(), content="DropOldest", ) @@ -88,55 +84,54 @@ test "config json helpers export stable structured shapes" { style_tags={ "accent": text_style(fg=Some("#4cc9f0"), bold=true) }, ), ) - let formatter_obj = formatter_json.as_object().unwrap() + let formatter_obj = formatter_json.expect_json_object() let style_tags_obj = formatter_obj .get("style_tags") .unwrap() - .as_object() - .unwrap() - let accent_obj = style_tags_obj.get("accent").unwrap().as_object().unwrap() + .expect_json_object() + let accent_obj = style_tags_obj.get("accent").unwrap().expect_json_object() inspect( - formatter_obj.get("show_timestamp").unwrap().as_bool().unwrap(), + formatter_obj.get("show_timestamp").unwrap().expect_json_bool(), content="false", ) inspect( - formatter_obj.get("show_target").unwrap().as_bool().unwrap(), + formatter_obj.get("show_target").unwrap().expect_json_bool(), content="false", ) inspect( - formatter_obj.get("separator").unwrap().as_string().unwrap(), + formatter_obj.get("separator").unwrap().expect_json_string(), content=" | ", ) inspect( - formatter_obj.get("field_separator").unwrap().as_string().unwrap(), + formatter_obj.get("field_separator").unwrap().expect_json_string(), content=",", ) inspect( - formatter_obj.get("template").unwrap().as_string().unwrap(), + formatter_obj.get("template").unwrap().expect_json_string(), content="[{level}] {message} :: {fields}", ) inspect( - formatter_obj.get("color_mode").unwrap().as_string().unwrap(), + formatter_obj.get("color_mode").unwrap().expect_json_string(), content="always", ) inspect( - formatter_obj.get("color_support").unwrap().as_string().unwrap(), + formatter_obj.get("color_support").unwrap().expect_json_string(), content="basic", ) inspect( - formatter_obj.get("style_markup").unwrap().as_string().unwrap(), + formatter_obj.get("style_markup").unwrap().expect_json_string(), content="builtin", ) inspect( - formatter_obj.get("target_style_markup").unwrap().as_string().unwrap(), + formatter_obj.get("target_style_markup").unwrap().expect_json_string(), content="builtin", ) inspect( - formatter_obj.get("fields_style_markup").unwrap().as_string().unwrap(), + formatter_obj.get("fields_style_markup").unwrap().expect_json_string(), content="disabled", ) - inspect(accent_obj.get("bold").unwrap().as_bool().unwrap(), content="true") - inspect(accent_obj.get("fg").unwrap().as_string().unwrap(), content="#4cc9f0") + inspect(accent_obj.get("bold").unwrap().expect_json_bool(), content="true") + inspect(accent_obj.get("fg").unwrap().expect_json_string(), content="#4cc9f0") let sink_json = sink_config_to_json( SinkConfig::new( @@ -151,37 +146,36 @@ test "config json helpers export stable structured shapes" { ), ), ) - let sink_obj = sink_json.as_object().unwrap() + let sink_obj = sink_json.expect_json_object() let sink_formatter_obj = sink_obj .get("text_formatter") .unwrap() - .as_object() - .unwrap() - let rotation_obj = sink_obj.get("rotation").unwrap().as_object().unwrap() - inspect(sink_obj.get("kind").unwrap().as_string().unwrap(), content="file") + .expect_json_object() + let rotation_obj = sink_obj.get("rotation").unwrap().expect_json_object() + inspect(sink_obj.get("kind").unwrap().expect_json_string(), content="file") inspect( - sink_obj.get("path").unwrap().as_string().unwrap(), + sink_obj.get("path").unwrap().expect_json_string(), content="logs/demo.log", ) - inspect(sink_obj.get("append").unwrap().as_bool().unwrap(), content="false") + inspect(sink_obj.get("append").unwrap().expect_json_bool(), content="false") inspect( - sink_obj.get("auto_flush").unwrap().as_bool().unwrap(), + sink_obj.get("auto_flush").unwrap().expect_json_bool(), content="false", ) inspect( - sink_formatter_obj.get("show_timestamp").unwrap().as_bool().unwrap(), + sink_formatter_obj.get("show_timestamp").unwrap().expect_json_bool(), content="false", ) inspect( - sink_formatter_obj.get("color_mode").unwrap().as_string().unwrap(), + sink_formatter_obj.get("color_mode").unwrap().expect_json_string(), content="auto", ) inspect( - rotation_obj.get("max_bytes").unwrap().as_number().unwrap().to_int(), + rotation_obj.get("max_bytes").unwrap().expect_json_number().to_int(), content="128", ) inspect( - rotation_obj.get("max_backups").unwrap().as_number().unwrap().to_int(), + rotation_obj.get("max_backups").unwrap().expect_json_number().to_int(), content="2", ) inspect(rotation_obj.get("max_bytes_i64") is None, content="true") @@ -189,17 +183,17 @@ test "config json helpers export stable structured shapes" { let wide_rotation_json = file_rotation_config_to_json( file_rotation_i64(4294967296L, max_backups=3), ) - let wide_rotation_obj = wide_rotation_json.as_object().unwrap() + let wide_rotation_obj = wide_rotation_json.expect_json_object() inspect( - wide_rotation_obj.get("max_bytes").unwrap().as_number().unwrap().to_int(), + wide_rotation_obj.get("max_bytes").unwrap().expect_json_number().to_int(), content="2147483647", ) inspect( - wide_rotation_obj.get("max_backups").unwrap().as_number().unwrap().to_int(), + wide_rotation_obj.get("max_backups").unwrap().expect_json_number().to_int(), content="3", ) inspect( - wide_rotation_obj.get("max_bytes_i64").unwrap().as_string().unwrap(), + wide_rotation_obj.get("max_bytes_i64").unwrap().expect_json_string(), content="4294967296", ) @@ -212,28 +206,28 @@ test "config json helpers export stable structured shapes" { queue=Some(QueueConfig::new(8, overflow=QueueOverflowPolicy::DropNewest)), ), ) - let logger_obj = logger_json.as_object().unwrap() - let logger_sink_obj = logger_obj.get("sink").unwrap().as_object().unwrap() - let logger_queue_obj = logger_obj.get("queue").unwrap().as_object().unwrap() + let logger_obj = logger_json.expect_json_object() + let logger_sink_obj = logger_obj.get("sink").unwrap().expect_json_object() + let logger_queue_obj = logger_obj.get("queue").unwrap().expect_json_object() inspect( - logger_obj.get("min_level").unwrap().as_string().unwrap(), + logger_obj.get("min_level").unwrap().expect_json_string(), content="WARN", ) - inspect(logger_obj.get("target").unwrap().as_string().unwrap(), content="api") + inspect(logger_obj.get("target").unwrap().expect_json_string(), content="api") inspect( - logger_obj.get("timestamp").unwrap().as_bool().unwrap(), + logger_obj.get("timestamp").unwrap().expect_json_bool(), content="true", ) inspect( - logger_sink_obj.get("kind").unwrap().as_string().unwrap(), + logger_sink_obj.get("kind").unwrap().expect_json_string(), content="text_console", ) inspect( - logger_queue_obj.get("max_pending").unwrap().as_number().unwrap().to_int(), + logger_queue_obj.get("max_pending").unwrap().expect_json_number().to_int(), content="8", ) inspect( - logger_queue_obj.get("overflow").unwrap().as_string().unwrap(), + logger_queue_obj.get("overflow").unwrap().expect_json_string(), content="DropNewest", ) } @@ -242,81 +236,80 @@ test "config json helpers export stable structured shapes" { test "logger config json export materializes parsed omitted defaults" { let parsed = parse_logger_config_text("{}") let logger_json = logger_config_to_json(parsed) - let logger_obj = logger_json.as_object().unwrap() - let sink_obj = logger_obj.get("sink").unwrap().as_object().unwrap() + let logger_obj = logger_json.expect_json_object() + let sink_obj = logger_obj.get("sink").unwrap().expect_json_object() let formatter_obj = sink_obj .get("text_formatter") .unwrap() - .as_object() - .unwrap() + .expect_json_object() inspect( - @json_parser.stringify(logger_json), + logger_json.stringify(), content="{\"min_level\":\"INFO\",\"target\":\"\",\"timestamp\":false,\"sink\":{\"kind\":\"console\",\"path\":\"\",\"append\":true,\"auto_flush\":true,\"text_formatter\":{\"show_timestamp\":true,\"show_level\":true,\"show_target\":true,\"show_fields\":true,\"separator\":\" \",\"field_separator\":\" \",\"template\":\"\",\"color_mode\":\"never\",\"color_support\":\"truecolor\",\"style_markup\":\"full\",\"target_style_markup\":\"disabled\",\"fields_style_markup\":\"disabled\"}}}", ) inspect( - logger_obj.get("min_level").unwrap().as_string().unwrap(), + logger_obj.get("min_level").unwrap().expect_json_string(), content="INFO", ) - inspect(logger_obj.get("target").unwrap().as_string().unwrap(), content="") + inspect(logger_obj.get("target").unwrap().expect_json_string(), content="") inspect( - logger_obj.get("timestamp").unwrap().as_bool().unwrap(), + logger_obj.get("timestamp").unwrap().expect_json_bool(), content="false", ) inspect(logger_obj.get("queue") is None, content="true") - inspect(sink_obj.get("kind").unwrap().as_string().unwrap(), content="console") - inspect(sink_obj.get("path").unwrap().as_string().unwrap(), content="") - inspect(sink_obj.get("append").unwrap().as_bool().unwrap(), content="true") + inspect(sink_obj.get("kind").unwrap().expect_json_string(), content="console") + inspect(sink_obj.get("path").unwrap().expect_json_string(), content="") + inspect(sink_obj.get("append").unwrap().expect_json_bool(), content="true") inspect( - sink_obj.get("auto_flush").unwrap().as_bool().unwrap(), + sink_obj.get("auto_flush").unwrap().expect_json_bool(), content="true", ) inspect( - formatter_obj.get("show_timestamp").unwrap().as_bool().unwrap(), + formatter_obj.get("show_timestamp").unwrap().expect_json_bool(), content="true", ) inspect( - formatter_obj.get("show_level").unwrap().as_bool().unwrap(), + formatter_obj.get("show_level").unwrap().expect_json_bool(), content="true", ) inspect( - formatter_obj.get("show_target").unwrap().as_bool().unwrap(), + formatter_obj.get("show_target").unwrap().expect_json_bool(), content="true", ) inspect( - formatter_obj.get("show_fields").unwrap().as_bool().unwrap(), + formatter_obj.get("show_fields").unwrap().expect_json_bool(), content="true", ) inspect( - formatter_obj.get("separator").unwrap().as_string().unwrap(), + formatter_obj.get("separator").unwrap().expect_json_string(), content=" ", ) inspect( - formatter_obj.get("field_separator").unwrap().as_string().unwrap(), + formatter_obj.get("field_separator").unwrap().expect_json_string(), content=" ", ) inspect( - formatter_obj.get("template").unwrap().as_string().unwrap(), + formatter_obj.get("template").unwrap().expect_json_string(), content="", ) inspect( - formatter_obj.get("color_mode").unwrap().as_string().unwrap(), + formatter_obj.get("color_mode").unwrap().expect_json_string(), content="never", ) inspect( - formatter_obj.get("color_support").unwrap().as_string().unwrap(), + formatter_obj.get("color_support").unwrap().expect_json_string(), content="truecolor", ) inspect( - formatter_obj.get("style_markup").unwrap().as_string().unwrap(), + formatter_obj.get("style_markup").unwrap().expect_json_string(), content="full", ) inspect( - formatter_obj.get("target_style_markup").unwrap().as_string().unwrap(), + formatter_obj.get("target_style_markup").unwrap().expect_json_string(), content="disabled", ) inspect( - formatter_obj.get("fields_style_markup").unwrap().as_string().unwrap(), + formatter_obj.get("fields_style_markup").unwrap().expect_json_string(), content="disabled", ) inspect(formatter_obj.get("style_tags") is None, content="true") diff --git a/src/moon.pkg b/src/moon.pkg index 29a2749..0a58450 100644 --- a/src/moon.pkg +++ b/src/moon.pkg @@ -9,7 +9,6 @@ import { "Nanaloveyuki/BitLogger/src/queue_model", "Nanaloveyuki/BitLogger/src/runtime", "Nanaloveyuki/BitLogger/src/sink_graph", - "maria/json_parser", "moonbitlang/core/array", "moonbitlang/core/builtin", "moonbitlang/core/env", diff --git a/src/pkg.generated.mbti b/src/pkg.generated.mbti index 89539f4..9899a2e 100644 --- a/src/pkg.generated.mbti +++ b/src/pkg.generated.mbti @@ -10,7 +10,6 @@ import { "Nanaloveyuki/BitLogger/src/queue_model", "Nanaloveyuki/BitLogger/src/runtime", "Nanaloveyuki/BitLogger/src/sink_graph", - "maria/json_parser", } // Values @@ -70,15 +69,15 @@ pub fn file(String, min_level? : @core.Level, target? : String, timestamp? : Boo pub fn file_rotation(Int, max_backups? : Int) -> @file_model.FileRotation -pub fn file_rotation_config_to_json(@file_model.FileRotation) -> @json_parser.JsonValue +pub fn file_rotation_config_to_json(@file_model.FileRotation) -> Json pub fn file_rotation_i64(Int64, max_backups? : Int) -> @file_model.FileRotation pub fn file_sink(String, append? : Bool, auto_flush? : Bool, rotation? : @file_model.FileRotation?, formatter? : (@core.Record) -> String) -> @file_runtime.FileSink -pub fn file_sink_policy_to_json(@file_model.FileSinkPolicy) -> @json_parser.JsonValue +pub fn file_sink_policy_to_json(@file_model.FileSinkPolicy) -> Json -pub fn file_sink_state_to_json(@file_model.FileSinkState) -> @json_parser.JsonValue +pub fn file_sink_state_to_json(@file_model.FileSinkState) -> Json pub fn[S] filter_sink(S, (@core.Record) -> Bool) -> @sink_graph.FilterSink[S] @@ -106,7 +105,7 @@ pub fn level_at_least(@core.Level) -> (@core.Record) -> Bool pub fn log(@core.Level, String, fields? : Array[@core.Field]) -> Unit -pub fn logger_config_to_json(@config_model.LoggerConfig) -> @json_parser.JsonValue +pub fn logger_config_to_json(@config_model.LoggerConfig) -> Json pub fn message_contains(String) -> (@core.Record) -> Bool @@ -126,7 +125,7 @@ pub fn[S] patch_sink(S, (@core.Record) -> @core.Record) -> @sink_graph.PatchSink pub fn prefix_message(String) -> (@core.Record) -> @core.Record -pub fn queue_config_to_json(@config_model.QueueConfig) -> @json_parser.JsonValue +pub fn queue_config_to_json(@config_model.QueueConfig) -> Json pub fn[S] queued_sink(S, max_pending? : Int, overflow? : @queue_model.QueueOverflowPolicy) -> @sink_graph.QueuedSink[S] @@ -136,7 +135,7 @@ pub fn redact_fields(Array[String], placeholder? : String) -> (@core.Record) -> pub fn reset_global_style_tag_registry() -> Unit -pub fn runtime_file_state_to_json(@file_model.RuntimeFileState) -> @json_parser.JsonValue +pub fn runtime_file_state_to_json(@file_model.RuntimeFileState) -> Json pub fn set_default_min_level(@core.Level) -> Unit @@ -146,7 +145,7 @@ pub fn set_global_style_tag_registry(@formatting.StyleTagRegistry) -> Unit pub fn set_target(String) -> (@core.Record) -> @core.Record -pub fn sink_config_to_json(@config_model.SinkConfig) -> @json_parser.JsonValue +pub fn sink_config_to_json(@config_model.SinkConfig) -> Json pub fn[A, B] split_by_level(A, B, min_level? : @core.Level) -> @sink_graph.SplitSink[A, B] @@ -182,7 +181,7 @@ pub fn text_console_sink(@formatting.TextFormatter) -> @sink_graph.FormattedCons pub fn text_formatter(show_timestamp? : Bool, show_level? : Bool, show_target? : Bool, show_fields? : Bool, separator? : String, field_separator? : String, template? : String, color_mode? : @formatting.ColorMode, color_support? : @formatting.ColorSupport, style_markup? : @formatting.StyleMarkupMode, target_style_markup? : @formatting.StyleMarkupMode, fields_style_markup? : @formatting.StyleMarkupMode, style_tags? : @formatting.StyleTagRegistry?) -> @formatting.TextFormatter -pub fn text_formatter_config_to_json(@config_model.TextFormatterConfig) -> @json_parser.JsonValue +pub fn text_formatter_config_to_json(@config_model.TextFormatterConfig) -> Json pub fn text_style(fg? : String?, bg? : String?, bold? : Bool, dim? : Bool, italic? : Bool, underline? : Bool) -> @formatting.TextStyle diff --git a/src/runtime/moon.pkg b/src/runtime/moon.pkg index 2583153..8787948 100644 --- a/src/runtime/moon.pkg +++ b/src/runtime/moon.pkg @@ -4,5 +4,5 @@ import { "Nanaloveyuki/BitLogger/src/file_model", "Nanaloveyuki/BitLogger/src/formatting", "Nanaloveyuki/BitLogger/src/sink_graph", - "maria/json_parser", + "moonbitlang/core/json", } diff --git a/src/runtime/pkg.generated.mbti b/src/runtime/pkg.generated.mbti index ed865ab..a36e184 100644 --- a/src/runtime/pkg.generated.mbti +++ b/src/runtime/pkg.generated.mbti @@ -7,7 +7,6 @@ import { "Nanaloveyuki/BitLogger/src/file_model", "Nanaloveyuki/BitLogger/src/file_runtime", "Nanaloveyuki/BitLogger/src/sink_graph", - "maria/json_parser", } // Values @@ -15,7 +14,7 @@ pub fn apply_queue_config(RuntimeSink, @config_model.QueueConfig) -> RuntimeSink pub fn build_runtime_sink(@config_model.SinkConfig) -> RuntimeSink -pub fn runtime_file_state_to_json(@file_model.RuntimeFileState) -> @json_parser.JsonValue +pub fn runtime_file_state_to_json(@file_model.RuntimeFileState) -> Json pub fn stringify_runtime_file_state(@file_model.RuntimeFileState, pretty? : Bool) -> String diff --git a/src/runtime/runtime_logger.mbt b/src/runtime/runtime_logger.mbt index 515357c..b759d86 100644 --- a/src/runtime/runtime_logger.mbt +++ b/src/runtime/runtime_logger.mbt @@ -55,9 +55,7 @@ pub struct RuntimeSinkProgress { } ///| -pub fn runtime_file_state_to_json( - state : RuntimeFileState, -) -> @json_parser.JsonValue { +pub fn runtime_file_state_to_json(state : RuntimeFileState) -> Json { @file_model.runtime_file_state_to_json(state) } diff --git a/src/runtime_file_json_test.mbt b/src/runtime_file_json_test.mbt index a3e7741..0797b86 100644 --- a/src/runtime_file_json_test.mbt +++ b/src/runtime_file_json_test.mbt @@ -3,17 +3,17 @@ test "file state json helpers stringify stable snapshots" { let rotation_json = file_rotation_config_to_json( file_rotation(64, max_backups=2), ) - let rotation_obj = rotation_json.as_object().unwrap() + let rotation_obj = rotation_json.expect_json_object() inspect( - @json_parser.stringify(rotation_json), + rotation_json.stringify(), content="{\"max_bytes\":64,\"max_backups\":2}", ) inspect( - rotation_obj.get("max_bytes").unwrap().as_number().unwrap().to_int(), + rotation_obj.get("max_bytes").unwrap().expect_json_number().to_int(), content="64", ) inspect( - rotation_obj.get("max_backups").unwrap().as_number().unwrap().to_int(), + rotation_obj.get("max_backups").unwrap().expect_json_number().to_int(), content="2", ) inspect(rotation_obj.get("max_bytes_i64") is None, content="true") @@ -21,9 +21,9 @@ test "file state json helpers stringify stable snapshots" { let wide_rotation_json = file_rotation_config_to_json( file_rotation_i64(4294967296L, max_backups=2), ) - let wide_rotation_obj = wide_rotation_json.as_object().unwrap() + let wide_rotation_obj = wide_rotation_json.expect_json_object() inspect( - wide_rotation_obj.get("max_bytes_i64").unwrap().as_string().unwrap(), + wide_rotation_obj.get("max_bytes_i64").unwrap().expect_json_string(), content="4294967296", ) @@ -40,51 +40,50 @@ test "file state json helpers stringify stable snapshots" { rotation_failures=4, ), ) - let plain_obj = plain.as_object().unwrap() + let plain_obj = plain.expect_json_object() let plain_rotation_obj = plain_obj .get("rotation") .unwrap() - .as_object() - .unwrap() + .expect_json_object() inspect( - @json_parser.stringify(plain), + plain.stringify(), content="{\"path\":\"logs/demo.log\",\"available\":true,\"append\":false,\"auto_flush\":true,\"open_failures\":1,\"write_failures\":2,\"flush_failures\":3,\"rotation_failures\":4,\"rotation\":{\"max_bytes\":64,\"max_backups\":2}}", ) inspect( - plain_obj.get("path").unwrap().as_string().unwrap(), + plain_obj.get("path").unwrap().expect_json_string(), content="logs/demo.log", ) inspect( - plain_obj.get("available").unwrap().as_bool().unwrap(), + plain_obj.get("available").unwrap().expect_json_bool(), content="true", ) - inspect(plain_obj.get("append").unwrap().as_bool().unwrap(), content="false") + inspect(plain_obj.get("append").unwrap().expect_json_bool(), content="false") inspect( - plain_obj.get("auto_flush").unwrap().as_bool().unwrap(), + plain_obj.get("auto_flush").unwrap().expect_json_bool(), content="true", ) inspect( - plain_obj.get("open_failures").unwrap().as_number().unwrap().to_int(), + plain_obj.get("open_failures").unwrap().expect_json_number().to_int(), content="1", ) inspect( - plain_obj.get("write_failures").unwrap().as_number().unwrap().to_int(), + plain_obj.get("write_failures").unwrap().expect_json_number().to_int(), content="2", ) inspect( - plain_obj.get("flush_failures").unwrap().as_number().unwrap().to_int(), + plain_obj.get("flush_failures").unwrap().expect_json_number().to_int(), content="3", ) inspect( - plain_obj.get("rotation_failures").unwrap().as_number().unwrap().to_int(), + plain_obj.get("rotation_failures").unwrap().expect_json_number().to_int(), content="4", ) inspect( - plain_rotation_obj.get("max_bytes").unwrap().as_number().unwrap().to_int(), + plain_rotation_obj.get("max_bytes").unwrap().expect_json_number().to_int(), content="64", ) inspect( - plain_rotation_obj.get("max_backups").unwrap().as_number().unwrap().to_int(), + plain_rotation_obj.get("max_backups").unwrap().expect_json_number().to_int(), content="2", ) @@ -101,18 +100,16 @@ test "file state json helpers stringify stable snapshots" { rotation_failures=0, ), ) - let wide_obj = wide.as_object().unwrap() + let wide_obj = wide.expect_json_object() let wide_state_rotation_obj = wide_obj .get("rotation") .unwrap() - .as_object() - .unwrap() + .expect_json_object() inspect( wide_state_rotation_obj .get("max_bytes") .unwrap() - .as_number() - .unwrap() + .expect_json_number() .to_int(), content="2147483647", ) @@ -120,13 +117,12 @@ test "file state json helpers stringify stable snapshots" { wide_state_rotation_obj .get("max_backups") .unwrap() - .as_number() - .unwrap() + .expect_json_number() .to_int(), content="3", ) inspect( - wide_state_rotation_obj.get("max_bytes_i64").unwrap().as_string().unwrap(), + wide_state_rotation_obj.get("max_bytes_i64").unwrap().expect_json_string(), content="4294967296", ) inspect( @@ -167,27 +163,27 @@ test "runtime file state json helpers stringify queue snapshots" { dropped_count=5, ), ) - let runtime_obj = runtime_json.as_object().unwrap() - let runtime_file_obj = runtime_obj.get("file").unwrap().as_object().unwrap() - inspect(runtime_obj.get("queued").unwrap().as_bool().unwrap(), content="true") + let runtime_obj = runtime_json.expect_json_object() + let runtime_file_obj = runtime_obj.get("file").unwrap().expect_json_object() + inspect(runtime_obj.get("queued").unwrap().expect_json_bool(), content="true") inspect( - runtime_obj.get("pending_count").unwrap().as_number().unwrap().to_int(), + runtime_obj.get("pending_count").unwrap().expect_json_number().to_int(), content="7", ) inspect( - runtime_obj.get("dropped_count").unwrap().as_number().unwrap().to_int(), + runtime_obj.get("dropped_count").unwrap().expect_json_number().to_int(), content="5", ) inspect( - runtime_file_obj.get("path").unwrap().as_string().unwrap(), + runtime_file_obj.get("path").unwrap().expect_json_string(), content="logs/queue.log", ) inspect( - runtime_file_obj.get("available").unwrap().as_bool().unwrap(), + runtime_file_obj.get("available").unwrap().expect_json_bool(), content="true", ) inspect( - runtime_file_obj.get("rotation").unwrap() is @json_parser.JsonValue::Null, + runtime_file_obj.get("rotation").unwrap() is Json::Null, content="true", ) @@ -232,23 +228,20 @@ test "runtime file state json helpers stringify queue snapshots" { dropped_count=0, ), ) - let wide_runtime_obj = wide_runtime_json.as_object().unwrap() + let wide_runtime_obj = wide_runtime_json.expect_json_object() let wide_runtime_file_obj = wide_runtime_obj .get("file") .unwrap() - .as_object() - .unwrap() + .expect_json_object() let wide_runtime_rotation_obj = wide_runtime_file_obj .get("rotation") .unwrap() - .as_object() - .unwrap() + .expect_json_object() inspect( wide_runtime_rotation_obj .get("max_bytes") .unwrap() - .as_number() - .unwrap() + .expect_json_number() .to_int(), content="2147483647", ) @@ -256,13 +249,12 @@ test "runtime file state json helpers stringify queue snapshots" { wide_runtime_rotation_obj .get("max_backups") .unwrap() - .as_number() - .unwrap() + .expect_json_number() .to_int(), content="2", ) inspect( - wide_runtime_rotation_obj.get("max_bytes_i64").unwrap().as_string().unwrap(), + wide_runtime_rotation_obj.get("max_bytes_i64").unwrap().expect_json_string(), content="4294967296", ) } @@ -276,31 +268,29 @@ test "file sink policy json helpers stringify stable policies" { rotation=Some(file_rotation(96, max_backups=3)), ), ) - let policy_obj = policy_json.as_object().unwrap() + let policy_obj = policy_json.expect_json_object() let policy_rotation_obj = policy_obj .get("rotation") .unwrap() - .as_object() - .unwrap() + .expect_json_object() inspect( - @json_parser.stringify(policy_json), + policy_json.stringify(), content="{\"append\":false,\"auto_flush\":true,\"rotation\":{\"max_bytes\":96,\"max_backups\":3}}", ) - inspect(policy_obj.get("append").unwrap().as_bool().unwrap(), content="false") + inspect(policy_obj.get("append").unwrap().expect_json_bool(), content="false") inspect( - policy_obj.get("auto_flush").unwrap().as_bool().unwrap(), + policy_obj.get("auto_flush").unwrap().expect_json_bool(), content="true", ) inspect( - policy_rotation_obj.get("max_bytes").unwrap().as_number().unwrap().to_int(), + policy_rotation_obj.get("max_bytes").unwrap().expect_json_number().to_int(), content="96", ) inspect( policy_rotation_obj .get("max_backups") .unwrap() - .as_number() - .unwrap() + .expect_json_number() .to_int(), content="3", ) @@ -312,18 +302,16 @@ test "file sink policy json helpers stringify stable policies" { rotation=Some(file_rotation_i64(4294967296L, max_backups=4)), ), ) - let wide_policy_obj = wide_policy_json.as_object().unwrap() + let wide_policy_obj = wide_policy_json.expect_json_object() let wide_policy_rotation_obj = wide_policy_obj .get("rotation") .unwrap() - .as_object() - .unwrap() + .expect_json_object() inspect( wide_policy_rotation_obj .get("max_bytes") .unwrap() - .as_number() - .unwrap() + .expect_json_number() .to_int(), content="2147483647", ) @@ -331,13 +319,12 @@ test "file sink policy json helpers stringify stable policies" { wide_policy_rotation_obj .get("max_backups") .unwrap() - .as_number() - .unwrap() + .expect_json_number() .to_int(), content="4", ) inspect( - wide_policy_rotation_obj.get("max_bytes_i64").unwrap().as_string().unwrap(), + wide_policy_rotation_obj.get("max_bytes_i64").unwrap().expect_json_string(), content="4294967296", ) inspect( diff --git a/src/runtime_logger.mbt b/src/runtime_logger.mbt index dd5f499..ebd3ee0 100644 --- a/src/runtime_logger.mbt +++ b/src/runtime_logger.mbt @@ -8,9 +8,7 @@ pub type RuntimeFileState = @file_model.RuntimeFileState pub type RuntimeSinkProgress = @runtime.RuntimeSinkProgress ///| -pub fn runtime_file_state_to_json( - state : RuntimeFileState, -) -> @json_parser.JsonValue { +pub fn runtime_file_state_to_json(state : RuntimeFileState) -> Json { @file_model.runtime_file_state_to_json(state) } diff --git a/src/sinks_file.mbt b/src/sinks_file.mbt index d70722f..d467724 100644 --- a/src/sinks_file.mbt +++ b/src/sinks_file.mbt @@ -29,9 +29,7 @@ pub fn native_files_supported() -> Bool { } ///| -pub fn file_sink_policy_to_json( - policy : FileSinkPolicy, -) -> @json_parser.JsonValue { +pub fn file_sink_policy_to_json(policy : FileSinkPolicy) -> Json { @file_model.file_sink_policy_to_json(policy) } @@ -44,7 +42,7 @@ pub fn stringify_file_sink_policy( } ///| -pub fn file_sink_state_to_json(state : FileSinkState) -> @json_parser.JsonValue { +pub fn file_sink_state_to_json(state : FileSinkState) -> Json { @file_model.file_sink_state_to_json(state) }