@@ -46,6 +46,14 @@ def __init__(self, *, content_encoding: bool = True) -> None:
4646 super ().__init__ ()
4747 self .content_encoding = content_encoding
4848
49+ @classmethod
50+ def from_config (
51+ cls : type [JSONSchemaToPostgres ],
52+ config : dict ,
53+ ) -> JSONSchemaToPostgres :
54+ """Create a JSONSchemaToPostgres instance from a configuration."""
55+ return cls (content_encoding = config .get ("interpret_content_encoding" , False ))
56+
4957 def handle_raw_string (self , schema ):
5058 """Handle a raw string type."""
5159 if self .content_encoding and schema .get ("contentEncoding" ) == "base16" :
@@ -63,6 +71,8 @@ class PostgresConnector(SQLConnector):
6371 allow_merge_upsert : bool = True # Whether MERGE UPSERT is supported.
6472 allow_temp_tables : bool = True # Whether temp tables are supported.
6573
74+ jsonschema_to_sql_converter = JSONSchemaToPostgres
75+
6676 def __init__ (self , config : dict ) -> None :
6777 """Initialize a connector to a Postgres database.
6878
@@ -100,18 +110,6 @@ def __init__(self, config: dict) -> None:
100110 sqlalchemy_url = url .render_as_string (hide_password = False ),
101111 )
102112
103- @cached_property
104- def interpret_content_encoding (self ) -> bool :
105- """Whether to interpret schema contentEncoding to set the column type.
106-
107- It is an opt-in feature because it might result in data loss if the
108- actual data does not match the schema's advertised encoding.
109-
110- Returns:
111- True if the feature is enabled, False otherwise.
112- """
113- return self .config .get ("interpret_content_encoding" , False )
114-
115113 def prepare_table ( # type: ignore[override] # noqa: PLR0913
116114 self ,
117115 full_table_name : str | FullyQualifiedName ,
@@ -258,7 +256,7 @@ def _handle_array_type(self, jsonschema: dict) -> ARRAY | JSONB:
258256 @cached_property
259257 def jsonschema_to_sql (self ) -> JSONSchemaToSQL :
260258 """Return a JSONSchemaToSQL instance with custom type handling."""
261- to_sql = JSONSchemaToPostgres ( content_encoding = self .interpret_content_encoding )
259+ to_sql = JSONSchemaToPostgres . from_config ( self .config )
262260 to_sql .fallback_type = TEXT
263261 to_sql .register_type_handler ("integer" , BIGINT )
264262 to_sql .register_type_handler ("object" , JSONB )
0 commit comments