示例#1
0
 def visit_slot(self, slot_name: str, slot: SlotDefinition) -> None:
     # Don't emit redefined slots unless we are inlining
     if slot_name == slot.name or self.inline:
         defn = JsonObj(type="array", items=self.type_or_ref(slot.range)) if slot.multivalued \
                else self.type_or_ref(slot.range)
         if slot.description:
             defn.description = slot.description
         self.schemaobj.definitions[underscore(slot.name)] = defn
 def visit_class_slot(self, cls: ClassDefinition, aliased_slot_name: str, slot: SlotDefinition) -> None:
     if self.inline:
         # If inline we have to include redefined slots
         prop = JsonObj(type=JsonObj())
         prop.type['$ref'] = self.jref(underscore(slot.name))
     elif slot.multivalued:
         prop = JsonObj(type="array", items=self.type_or_ref(slot.range))
     else:
         prop = JsonObj(type="string")
     if slot.description:
         prop.description = slot.description
     self.clsobj.properties[underscore(aliased_slot_name)] = prop
    def visit_class_slot(self, cls: ClassDefinition, aliased_slot_name: str,
                         slot: SlotDefinition) -> None:
        if slot.range in self.schema.classes and slot.inlined:
            rng = f"#/definitions/{camelcase(slot.range)}"
        elif slot.range in self.schema.types:
            rng = self.schema.types[slot.range].base
        else:
            # note we assume string for non-lined complex objects
            rng = "string"

        # translate to json-schema builtins
        if rng == 'int':
            rng = 'integer'
        elif rng == 'Bool':
            rng = 'boolean'
        elif rng == 'str':
            rng = 'string'
        elif rng == 'float' or rng == 'double':
            rng = 'number'
        elif not rng.startswith('#'):
            # URIorCURIE, etc
            rng = 'string'

        if slot.inlined:
            # If inline we have to include redefined slots
            ref = JsonObj()
            ref['$ref'] = rng
            if slot.multivalued:
                prop = JsonObj(type="array", items=ref)
            else:
                prop = ref
        else:
            if slot.multivalued:
                prop = JsonObj(type="array", items={'type': rng})
            else:
                prop = JsonObj(type=rng)
        if slot.description:
            prop.description = slot.description
        if slot.required:
            self.clsobj.required.append(underscore(aliased_slot_name))

        self.clsobj.properties[underscore(aliased_slot_name)] = prop
        if self.topCls is not None and camelcase(self.topCls) == camelcase(cls.name) or \
                self.topCls is None and cls.tree_root:
            self.schemaobj.properties[underscore(aliased_slot_name)] = prop