Last active
March 24, 2022 11:11
-
-
Save syedadeel2/520360827cd3bd7c586f5741970f3364 to your computer and use it in GitHub Desktop.
GOA Design Spec Type Generator - Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AUTHOR : ADEEL RIZVI | |
# DATE : 24/03/2022 | |
class GoaAttribute: | |
def __init__(self, name, desc, type, required): | |
self.name = name.replace( | |
'-', '_').replace('.', '_').replace(' ', '_') | |
self.desc = desc | |
self.type = type | |
self.required = required | |
def __str__(self): | |
return f'Attribute("{self.name}", {self.type}, func() {{ Description("{self.desc}") }})\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AUTHOR : ADEEL RIZVI | |
# DATE : 24/03/2022 | |
from attribute import GoaAttribute | |
class GoaType: | |
def __init__(self, name, type, desc=''): | |
self.name = name | |
self.type = type | |
self.desc = desc | |
self.attributes = [] | |
def add_attribute(self, attr: GoaAttribute): | |
if attr is None: | |
return | |
if len([a for a in self.attributes if a.name == attr.name]) > 0: | |
return | |
self.attributes.append(attr) | |
def generate(self): | |
self.name = self.name.replace( | |
'-', '_').replace('.', '_').replace(' ', '_') | |
class_str = f'var {self.name} = Type("{self.name}", func() {{ \n\n' | |
if self.desc is not None and self.desc != '': | |
class_str += f'Description("{self.desc}")\n' | |
for attr in self.attributes: | |
class_str += f'{attr.__str__()}\n' | |
for attr in self.attributes: | |
if attr.required: | |
class_str += f'Required("{attr.name}")\n' | |
class_str += '})\n' | |
return class_str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GOA SPEC TYPES GENERATOR | |
# PURPOSE : TO CREATE GOA DESIGN SPEC FILE FROM JSON PAYLOAD | |
# INPUT : JSON PAYLOAD | |
# OUTPUT : GOA DESIGN SPEC TYPES | |
# NOTE : THIS IS A PYTHON 3 SCRIPT | |
# AUTHOR : ADEEL RIZVI | |
# DATE : 24/03/2022 | |
# VERSION : 1.0 | |
# LICENSE : MIT | |
# FOR LIVE RUN USE THIS LINK : https://trinket.io/python3/c21093dd40?runOption=run | |
import json | |
import attribute | |
from goa_type import GoaType | |
# Enter the JSON String below | |
json_str = '' | |
# Enter the Root type name and Description (No Spaces In Name) | |
type_name = 'DefaultType' | |
type_desc = 'This is default type description' | |
list_types = [] | |
root = GoaType(type_name, type_desc) | |
list_types.append(root) | |
def scan_json(obj, parent_type): | |
if obj is None: | |
return | |
# scan each property | |
for prop in obj: | |
value = obj[prop] | |
# if value is null then make it a empty string, so it will pick as string type | |
if value is None: | |
value = '' | |
if isinstance(value, str): | |
parent_type.add_attribute( | |
attribute.GoaAttribute(prop, f'{prop} description goes here', 'String', False)) | |
elif isinstance(value, int): | |
parent_type.add_attribute( | |
attribute.GoaAttribute(prop, f'{prop} description goes here', 'Int64', False)) | |
elif isinstance(value, float): | |
parent_type.add_attribute( | |
attribute.GoaAttribute(prop, f'{prop} description goes here', 'float64', False)) | |
elif isinstance(value, bool): | |
parent_type.add_attribute( | |
attribute.GoaAttribute(prop, f'{prop} description goes here', 'Boolean', False)) | |
elif isinstance(value, list): | |
if len(value) > 0: | |
parent_type.add_attribute( | |
attribute.GoaAttribute(prop, f'{prop} description goes here', f'ArrayOf({prop})', False)) | |
tmp = GoaType(prop, f'This is {prop} type description') | |
list_types.append(tmp) | |
for item in value: | |
scan_json(item, tmp) | |
elif isinstance(value, dict): | |
parent_type.add_attribute( | |
attribute.GoaAttribute(prop, f'{prop} description goes here', prop, False)) | |
tmp = GoaType(prop, f'This is {prop} type description') | |
list_types.append(tmp) | |
scan_json(value, tmp) | |
if json_str is None or json_str == '': | |
print('No Json Present to parse.') | |
else: | |
scan_json(json.loads(json_str), root) | |
for type in list_types: | |
print(type.generate()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment