vss_24.0.0_b4d0be7c/tools/json_schema/json_schema-writers.ads

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
--
--  Copyright (C) 2022, AdaCore
--
--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--

with Ada.Containers.Hashed_Sets;
with VSS.Strings.Hash;
with JSON_Schema.Readers;

package JSON_Schema.Writers is

   subtype Schema_Name is VSS.Strings.Virtual_String;
   --  Name of the schema. It looks like "#/definitions/Response"

   package String_Sets is new Ada.Containers.Hashed_Sets
     (VSS.Strings.Virtual_String,
      VSS.Strings.Hash,
      VSS.Strings."=",
      VSS.Strings."=");

   type Declaration_Kind is (Specification, Implemenetation);

   procedure Put (Text : VSS.Strings.Virtual_String);
   --  Write Text to stdout

   procedure New_Line;
   --  Write a line terminator to stdout

   function Escape_Keywords (Name : VSS.Strings.Virtual_String)
     return VSS.Strings.Virtual_String;
   --  Add a prefix when Name is in the Reserved_Words list

   procedure Each_Enumeration_Type
     (Map      : JSON_Schema.Readers.Schema_Map;
      Optional : String_Sets.Set;
      Action   : access procedure
        (Name     : Schema_Name;
         Property : VSS.Strings.Virtual_String;
         Schema   : Schema_Access;
         Optional : Boolean));
   --  Find enumeration schemas and call Action for them. Optional is set of
   --  schema name those are used in not-required properties. The Action
   --  procedure takes next arguments:
   --  * Name - name of toppest named schema containing enumeration Schema
   --  * Property - name of property if enumeration schema is nameless (when
   --    schema declaration is embedded into a property declaration).
   --  * Schema - corresponding enumeration schema
   --  * Optional - True if enumeration schema is used in not-required property

   procedure Each_Property
     (Map    : JSON_Schema.Readers.Schema_Map;
      Name   : Schema_Name;
      Schema : Schema_Access;
      Action : access procedure
        (Enclosing : Schema_Name;
         Property  : JSON_Schema.Property;
         Required  : Boolean));
   --  For given Schema traverse allOf items, collect all properties and call
   --  the Action procedure for each such property. Name is the Schema name.
   --  Enclosing is the name of schema for the Property.

   procedure Each_Anonymous_Schema
     (Map    : JSON_Schema.Readers.Schema_Map;
      Schema : Schema_Access;
      Action : access procedure (Property : JSON_Schema.Property));
   --  Call Action for each schema nested in given Schema

   procedure Each_Union_Type
     (Map      : JSON_Schema.Readers.Schema_Map;
      Optional : String_Sets.Set;
      Action   : access procedure
        (Name     : Schema_Name;
         Property : VSS.Strings.Virtual_String;
         Schema   : Schema_Access;
         Optional : Boolean));
   --  Find `anyOf` schemas and call Action for them. Optional is set of
   --  schema name those are used in not-required properties. The Action
   --  procedure takes next arguments:
   --  * Name - name of toppest named schema containing anyOf Schema
   --  * Property - name of property if anyOf schema is nameless (when
   --    schema declaration is embedded into a property declaration).
   --  * Schema - corresponding anyOf schema
   --  * Optional - True if anyOf schema is used in not-required property

   procedure Each_Holder_Type
     (Map      : JSON_Schema.Readers.Schema_Map;
      Holders  : VSS.String_Vectors.Virtual_String_Vector;
      Action   : access procedure (Name : Schema_Name));
   --  Execute Action on each schema used in Holders

   function Ref_To_Type_Name (Subschema : Schema_Name)
     return VSS.Strings.Virtual_String;
   --  Convert $ref to a type name

   procedure Get_Field_Type
     (Map       : JSON_Schema.Readers.Schema_Map;
      Schema    : Schema_Access;
      Required  : Boolean;
      Fallback  : VSS.Strings.Virtual_String;
      Type_Name : out VSS.Strings.Virtual_String;
      Prefix    : out VSS.Strings.Virtual_String);
   --  Return an Ada type name for given Schema. Fallback if a type name for
   --  properties with nested schema declaration.
   --  Return an empty string for string properties with just one enumeration
   --  literal defined, so we can skip such properties in the type declaration.
   --  If type is defined in a package, then return package name in Prefix.

   procedure Get_Element_Type
     (Map       : JSON_Schema.Readers.Schema_Map;
      Schema    : Schema_Access;
      Type_Name : out VSS.Strings.Virtual_String;
      Prefix    : out VSS.Strings.Virtual_String);

   procedure Print_Vector (Header : VSS.String_Vectors.Virtual_String_Vector);

   function Variant_Name
     (Map       : JSON_Schema.Readers.Schema_Map;
      Schema    : Schema_Access) return VSS.Strings.Virtual_String;
   --  Return a variant name for given Schema when schema is an element of
   --  anyOf schema

   function Is_Holder_Field
     (Name     : Schema_Name;
      Property : VSS.Strings.Virtual_String;
      Holders  : VSS.String_Vectors.Virtual_String_Vector) return Boolean;
   --  Check if given Property in the schema with Name should be presented as a
   --  holder type

end JSON_Schema.Writers;