Class: StructuredDataToSql::Json::JsonSchemaLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/structured_data_to_sql/json/json_schema_loader.rb

Overview

Builds table definitions directly from a Draft-07 JSON Schema file, walking the same flattening/unwrap rules as the Shredder (and registering every name in the shared NameRegistry so the emit pass produces identical names). x-pii annotations become column comments plus rows for the dump-wide _json_meta manifest.

Defined Under Namespace

Classes: PiiEntry, Result, TablePlan

Constant Summary collapse

MIN_ENUM_VARCHAR =
32

Instance Method Summary collapse

Constructor Details

#initialize(registry:, max_depth: 5, graphql_unwrap: true, raw_dates: false) ⇒ JsonSchemaLoader

Returns a new instance of JsonSchemaLoader.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/structured_data_to_sql/json/json_schema_loader.rb', line 21

def initialize(
  registry:,
  max_depth: 5,
  graphql_unwrap: true,
  raw_dates: false
)
  @registry = registry
  @max_depth = max_depth
  @graphql_unwrap = graphql_unwrap
  @raw_dates = raw_dates
end

Instance Method Details

#load(schema_path, records_path: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/structured_data_to_sql/json/json_schema_loader.rb', line 33

def load(schema_path, records_path: nil)
  begin
    @schema = Oj.load_file(schema_path.to_s, mode: :strict)
  rescue Oj::ParseError, EncodingError => e
    raise UsageError,
          "Could not parse JSON Schema #{schema_path}: #{e.message}"
  end
  unless @schema.is_a?(Hash)
    raise UsageError, "JSON Schema #{schema_path} is not an object schema"
  end

  @plans = {}
  @pii = []
  @forced_json = Set.new
  record_schema = detect_record_schema(records_path)
  walk_object(resolve(record_schema), [], "", parent_required: true)
  Result.new(
    plans: @plans,
    root_name: root_name,
    pii: @pii,
    forced_json: @forced_json
  )
end