Skip to content

Commit

Permalink
Feat: add documents
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmgr committed Nov 20, 2024
1 parent 6a2aaa9 commit 4b0b884
Showing 1 changed file with 122 additions and 9 deletions.
131 changes: 122 additions & 9 deletions nada_dsl/nada_types/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@ def new(cls, values: List[NadaType]) -> "NTuple":
),
)

def setValues(self, values: List[NadaType]):
child = (
NTupleNew(
child=values,
source_ref=SourceRef.back_frame(),
),
)
self.__init__(child, values)

def __getitem__(self, index: int) -> NadaType:
if index >= len(self.values):
raise IndexError(f"Invalid index {index} for NTuple.")
Expand Down Expand Up @@ -347,8 +356,17 @@ def new(cls, values: Dict[str, NadaType]) -> "Object":
),
)

def setValues(self, values: Dict[str, NadaType]):
child = (
ObjectNew(
child=values,
source_ref=SourceRef.back_frame(),
),
)
self.__init__(child, values)

def __getattr__(self, attr: str) -> NadaType:
if attr not in self.values:
if "values" not in self.__dict__ or attr not in self.values:
raise AttributeError(
f"'{self.__class__.__name__}' object has no attribute '{attr}'"
)
Expand Down Expand Up @@ -392,31 +410,124 @@ def store_in_ast(self, ty: object):
)


# def _process_schema(self: NadaType, index, key, schema_node):
# if "type" not in schema_node:
# raise TypeError("Missing 'type' in schema node")

# match schema_node["type"]:
# case "integer":
# if type(self) == NTuple:
# accessor = NTupleAccessor(
# index=index,
# child=self,
# source_ref=SourceRef.back_frame(),
# )
# elif type(self) == Object:
# accessor = ObjectAccessor(
# key=key,
# child=self,
# source_ref=SourceRef.back_frame(),
# )
# else:
# raise TypeError(f"Unsupported 'self': {type(self)}")
# return PublicInteger(child=accessor)
# case "boolean":
# if type(self) == NTuple:
# accessor = NTupleAccessor(
# index=index,
# child=self,
# source_ref=SourceRef.back_frame(),
# )
# elif type(self) == Object:
# accessor = ObjectAccessor(
# key=key,
# child=self,
# source_ref=SourceRef.back_frame(),
# )
# else:
# raise TypeError(f"Unsupported 'self': {type(self)}")
# return PublicBoolean(child=accessor)
# case "array":
# items_schema = schema_node.get("items")
# if items_schema is None:
# raise TypeError("Array schema missing 'items'")
# ntuple = object.__new__(NTuple)
# values = []
# for index, item in enumerate(items_schema):
# values.append(_process_schema(ntuple, index, 0, item))
# print(f"**** values: {values}")
# ntuple.setValues(values)
# return ntuple
# case "object":
# properties = schema_node.get("properties", {})
# values = {}
# obj = object.__new__(Object)
# for prop_name, prop_schema in properties.items():
# values[prop_name] = _process_schema(obj, 0, prop_name, prop_schema)
# print(f"**** values: {values}")
# obj.setValues(values)
# return obj
# case _:
# raise TypeError(f"Unsupported type in schema: {schema_node['type']}")


def _process_schema(schema_node):
if "type" not in schema_node:
raise TypeError("Missing 'type' in schema node")

match schema_node["type"]:
case "integer":
return PublicInteger(child=None)
return PublicInteger(child=None) # Placeholder for now
case "boolean":
return PublicBoolean(child=None)
return PublicBoolean(child=None) # Placeholder for now
case "array":
items_schema = schema_node.get("items")
if items_schema is None:
raise TypeError("Array schema missing 'items'")
items_object = _process_schema(items_schema)
return NTuple.new(values=items_object)
ntuple = NTuple.__new__(NTuple)
ntuple.values = [] # Placeholder
ntuple.child = None # Placeholder
for index, item_schema in enumerate(items_schema):
value = _process_schema(item_schema)
ntuple.values.append(value)
return ntuple
case "object":
properties = schema_node.get("properties", {})
obj_values = {}
for prop_name, prop_schema in properties.items():
obj_values[prop_name] = _process_schema(prop_schema)
return Object.new(values=obj_values)
obj = Object.__new__(Object)
obj.values = {} # Placeholder
obj.child = None # Placeholder
for key, prop_schema in properties.items():
value = _process_schema(prop_schema)
obj.values[key] = value
return obj
case _:
raise TypeError(f"Unsupported type in schema: {schema_node['type']}")


def _assign_accessors(parent):
if isinstance(parent, NTuple):
for index, value in enumerate(parent.values):
accessor = NTupleAccessor(
index=index,
child=parent,
source_ref=SourceRef.back_frame(),
)
value.child = accessor
_assign_accessors(value)
elif isinstance(parent, Object):
for key, value in parent.values.items():
accessor = ObjectAccessor(
key=key,
child=parent,
source_ref=SourceRef.back_frame(),
)
value.child = accessor
_assign_accessors(value)
else:
# Base case: PublicInteger or PublicBoolean
pass


class Document(Object):
"""The Document type"""

Expand All @@ -432,7 +543,9 @@ def __init__(self, child, filepath: str):
if schema["type"] != "object":
raise TypeError("Only objects are supported at the root")

# result = _process_schema(self, 0, 0, schema)
result = _process_schema(schema)
_assign_accessors(result)

super().__init__(child=child, values=result)

Expand Down

0 comments on commit 4b0b884

Please sign in to comment.