From 6b417297d11b2a2bfe7a2eb00c77b0ad9aef7da1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20Roch=C3=A9?= Date: Tue, 27 Feb 2024 14:48:16 +0100 Subject: [PATCH] turn description into atd doc --- lib/generator.ml | 17 +++++- tests/grok.t | 140 +++++++++++++++++++++++------------------------ tests/one_of.ml | 1 + tests/smoke.t | 18 +++--- 4 files changed, 94 insertions(+), 82 deletions(-) diff --git a/lib/generator.ml b/lib/generator.ml index 7e9aab4..8b0d0fe 100644 --- a/lib/generator.ml +++ b/lib/generator.ml @@ -6,7 +6,13 @@ let record_field_name str = let cleaned_field_name = Utils.sanitize_name str in if String.equal str cleaned_field_name then str else sprintf {|%s |} cleaned_field_name str -let define_type name type_ = sprintf "type %s = %s\n" (type_name name) type_ +let define_type ?doc name type_ = + let doc = + match doc with + | None -> "" + | Some doc -> sprintf {||} doc + in + sprintf "type %s = %s %s\n" (type_name name) type_ doc let process_int_type schema = match schema.format with @@ -149,7 +155,7 @@ and process_nested_schema_type ~ancestors schema = match merge_all_of schema with | { one_of = Some _; _ } | { typ = Some Object; properties = Some _; _ } | { enum = Some _; _ } -> let nested_type_name = concat_camelCase (List.rev ancestors) in - let nested = define_type nested_type_name (process_schema_type ~ancestors schema) in + let nested = define_type nested_type_name (process_schema_type ~ancestors schema) ?doc:schema.description in Buffer.add_string output (nested ^ "\n"); type_name nested_type_name | _ -> process_schema_type ~ancestors schema @@ -200,7 +206,12 @@ and process_enums enums = let process_schemas (schemas : (string * schema or_ref) list) = List.fold_left (fun acc (name, schema_or_ref) -> - define_type name (make_type_from_schema_or_ref ~ancestors:[ name ] schema_or_ref) :: acc + let doc = + match schema_or_ref with + | Ref _ -> None + | Obj schema -> schema.description + in + define_type ?doc name (make_type_from_schema_or_ref ~ancestors:[ name ] schema_or_ref) :: acc ) [] schemas diff --git a/tests/grok.t b/tests/grok.t index d4b4ccf..111230b 100644 --- a/tests/grok.t +++ b/tests/grok.t @@ -7,83 +7,83 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types type fieldConfigSourceOverrides = { matcher: matcherConfig; properties: dynamicConfigValue list; - } + } type graphPanelType = [ | Graph - ] + ] type graphPanelLegend = { ~show : bool; ?sort: string option; ?sortDesc: bool option; - } + } type heatmapPanelType = [ | Heatmap - ] + ] type panelRepeatDirection = [ | H | V - ] + ] type rangeMapType = [ | Range - ] + ] type rangeMapOptions = { from: float; to_ : float; result: valueMappingResult; - } + } type regexMapType = [ | Regex - ] + ] type regexMapOptions = { pattern: string; result: valueMappingResult; - } + } type rowPanelType = [ | Row - ] + ] type rowPanelPanels = [ | Panel of panel | GraphPanel of graphPanel | HeatmapPanel of heatmapPanel - ] + ] type specialValueMapType = [ | Special - ] + ] type specialValueMapOptions = { match_ : specialValueMatch; result: valueMappingResult; - } + } type valueMapType = [ | Value - ] + ] type variableModelQuery = [ | String of string | Json of json - ] + ] type variableOptionText = [ | String of string | StringList of string list - ] + ] type variableOptionValue = [ | String of string | StringList of string list - ] + ] type dashboardMetadata = { updateTimestamp: string; @@ -96,35 +96,35 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types finalizers: string list; resourceVersion: string; labels: json; - } + } type dashboardSpecTime = { ~from : string; ~to_ : string; - } + } type dashboardSpecTimepicker = { ~hidden : bool; ~refresh_intervals : string list; ~collapse : bool; ~time_options : string list; - } + } type dashboardSpecRefresh = [ | Bool of bool | String of string - ] + ] type dashboardSpecPanels = [ | Panel of panel | RowPanel of rowPanel | GraphPanel of graphPanel | HeatmapPanel of heatmapPanel - ] + ] type dashboardSpecTemplating = { ?list: variableModel list option; - } + } type dashboardSpec = { ?id: int option; @@ -150,31 +150,31 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types ?annotations: annotationContainer option; ?links: dashboardLink list option; ?snapshot: snapshot option; - } + } type dashboardStatus = { ?operatorStates: json option; ?additionalFields: json option; - } + } type statusOperatorStateState = [ | Success | In_progress | Failed - ] + ] type statusOperatorState = { lastEvaluation: string; state: statusOperatorStateState; ?descriptiveState: string option; ?details: json option; - } + } type dashboard = { metadata: dashboardMetadata; spec: dashboardSpec; status: dashboardStatus; - } + } type _kubeObjectMetadata = { uid: string; @@ -183,7 +183,7 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types finalizers: string list; resourceVersion: string; labels: json; - } + } type variableType = [ | Query @@ -194,17 +194,17 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types | Textbox | Custom | System - ] + ] - type variableSort = int + type variableSort = int - type variableRefresh = int + type variableRefresh = int type variableOption = { ?selected: bool option; text: variableOptionText; value: variableOptionValue; - } + } type variableModel = { type_ : variableType; @@ -220,45 +220,45 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types ?options: variableOption list option; ?refresh: variableRefresh option; ?sort: variableSort option; - } + } - type variableHide = int + type variableHide = int type valueMappingResult = { ?text: string option; ?color: string option; ?icon: string option; ?index: int option; - } + } type valueMapping = [ | ValueMap of valueMap | RangeMap of rangeMap | RegexMap of regexMap | SpecialValueMap of specialValueMap - ] + ] type valueMap = { type_ : valueMapType; options: json; - } + } type thresholdsMode = [ | Absolute | Percentage - ] + ] type thresholdsConfig = { mode: thresholdsMode; steps: threshold list; - } + } type threshold = { value: float; color: string; - } + } - type target = json + type target = json type specialValueMatch = [ | True_ @@ -267,12 +267,12 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types | Nan | Nullnan | Empty - ] + ] type specialValueMap = { type_ : specialValueMapType; options: specialValueMapOptions; - } + } type snapshot = { created: string; @@ -286,7 +286,7 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types updated: string; ?url: string option; userId: int; - } + } type rowPanel = { type_ : rowPanelType; @@ -297,17 +297,17 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types id: int; panels: rowPanelPanels list; ?repeat: string option; - } + } type regexMap = { type_ : regexMapType; options: regexMapOptions; - } + } type rangeMap = { type_ : rangeMapType; options: rangeMapOptions; - } + } type panel = { type_ : string; @@ -333,28 +333,28 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types ?libraryPanel: libraryPanelRef option; ?options: json option; ?fieldConfig: fieldConfigSource option; - } + } type matcherConfig = { ~id : string; ?options: json option; - } + } type mappingType = [ | Value | Range | Regex | Special - ] + ] type libraryPanelRef = { name: string; uid: string; - } + } type heatmapPanel = { type_ : heatmapPanelType; - } + } type gridPos = { ~h : int; @@ -362,17 +362,17 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types ~x : int; ~y : int; ?static: bool option; - } + } type graphPanel = { type_ : graphPanelType; ?legend: graphPanelLegend option; - } + } type fieldConfigSource = { defaults: fieldConfig; overrides: fieldConfigSourceOverrides list; - } + } type fieldConfig = { ?displayName: string option; @@ -391,13 +391,13 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types ?links: json list option; ?noValue: string option; ?custom: json option; - } + } type fieldColorSeriesByMode = [ | Min | Max | Last - ] + ] type fieldColorModeId = [ | Thresholds @@ -415,35 +415,35 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types | Continuouspurples | Fixed | Shades - ] + ] type fieldColor = { mode: fieldColorModeId; ?fixedColor: string option; ?seriesBy: fieldColorSeriesByMode option; - } + } type dynamicConfigValue = { ~id : string; ?value: json option; - } + } type dataTransformerConfig = { id: string; ?disabled: bool option; ?filter: matcherConfig option; options: json; - } + } type dataSourceRef = { ?type_ : string option; ?uid: string option; - } + } type dashboardLinkType = [ | Link | Dashboards - ] + ] type dashboardLink = { title: string; @@ -456,16 +456,16 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types ~targetBlank : bool; ~includeVars : bool; ~keepTime : bool; - } + } - type dashboardCursorSync = int + type dashboardCursorSync = int type annotationTarget = { limit: int64; matchAny: bool; tags: string list; type_ : string; - } + } type annotationQuery = { name: string; @@ -477,13 +477,13 @@ Generate ATD types from grok (Grafana Object Development Kit) dashboard types ?target: annotationTarget option; ?type_ : string option; ~builtIn : float; - } + } type annotationPanelFilter = { ~exclude : bool; ids: int list; - } + } type annotationContainer = { ?list: annotationQuery list option; - } + } diff --git a/tests/one_of.ml b/tests/one_of.ml index 506a12c..2a420a7 100644 --- a/tests/one_of.ml +++ b/tests/one_of.ml @@ -65,6 +65,7 @@ let nested_one_of_test _ = | String of string | Json of variableModelQueryJson ] + type variableModel = { ?query: variableModelQuery option; diff --git a/tests/smoke.t b/tests/smoke.t index 30d6344..3a681e4 100644 --- a/tests/smoke.t +++ b/tests/smoke.t @@ -6,7 +6,7 @@ Generate ATD out of JSON Scheme type product = { ?productId: int option; - } + } Generate ATD out of JSON Scheme with --format attribute $ jsonschema2atd --format jsonschema ./mocks/simple_jsonschema.json (* Generated by jsonschema2atd *) @@ -15,7 +15,7 @@ Generate ATD out of JSON Scheme with --format attribute type product = { ?productId: int option; - } + } Generate ATD out of JSON Scheme with -f attribute $ jsonschema2atd -f jsonschema ./mocks/simple_jsonschema.json (* Generated by jsonschema2atd *) @@ -24,7 +24,7 @@ Generate ATD out of JSON Scheme with -f attribute type product = { ?productId: int option; - } + } Generate ATD out of OpenAPI doc with --format attribute $ jsonschema2atd --format openapi ./mocks/simple_openapi.json (* Generated by jsonschema2atd *) @@ -33,7 +33,7 @@ Generate ATD out of OpenAPI doc with --format attribute type product = { ?productId: int option; - } + } Generate ATD out of OpenAPI doc with -f attribute $ jsonschema2atd -f openapi ./mocks/simple_openapi.json (* Generated by jsonschema2atd *) @@ -42,7 +42,7 @@ Generate ATD out of OpenAPI doc with -f attribute type product = { ?productId: int option; - } + } Generate ATD out of JSON Schema that contains defs $ jsonschema2atd --format=jsonschema ./mocks/jsonchema_defs.json @@ -50,12 +50,12 @@ Generate ATD out of JSON Schema that contains defs type json = abstract type int64 = int - type name = string + type name = string type root = { first_name: name; last_name: name; - } + } Generate ATD out of JSON Schema that contains definitions (legacy support) $ jsonschema2atd --format=jsonschema ./mocks/jsonchema_definitions.json @@ -63,9 +63,9 @@ Generate ATD out of JSON Schema that contains definitions (legacy support) type json = abstract type int64 = int - type name = string + type name = string type root = { first_name: name; last_name: name; - } + }