Skip to content

Commit

Permalink
Split gosnappi files into smaller files for better pars ability (#469)
Browse files Browse the repository at this point in the history
* initial commit

* lint python files

* cleaning up code
  • Loading branch information
Vibaswan authored Jul 25, 2024
1 parent 0f40e91 commit 206ae33
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 10 deletions.
13 changes: 5 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ __pycache__
/**/art
/**/art_go
.local
/pkg/coverage.txt
/pkg/coverage.out
/pkg/sanity
/pkg/go.mod
/pkg/go.sum
/pkg/openapiart.go
/pkg/httpapi
/pkg/log.txt
/pkg
!/pkg/.*_test.go
!/pkg/common.go
!/pkg/severtests
!/pkg/new_tests
_debug_bin
.env*
19 changes: 19 additions & 0 deletions artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
import sys
import os
import shutil
import importlib


Expand Down Expand Up @@ -53,10 +54,28 @@ def create_openapi_artifacts(openapiart_class, sdk=None):
open_api.GeneratePythonSdk(package_name="sanity")

if sdk == "go" or sdk is None or sdk == "all":
# delete all the previous files
pkg_dir = "./pkg/"
persistent_dirs = ["servertests"]
persistent_files = ["common.go", "expected.json"]
for (root, dirs, files) in os.walk(pkg_dir, topdown=True):
if root == pkg_dir:
# delete directories that are generated
for dir in dirs:
if dir not in persistent_dirs:
print("deleting directory ", os.path.join(pkg_dir, dir))
shutil.rmtree(os.path.join(pkg_dir, dir))
# delete all files that are generated
for file in files:
if file not in persistent_files and not file.endswith("_test.go"):
print("deleting file ", os.path.join(pkg_dir, file))
os.remove(os.path.join(pkg_dir, file))
break
open_api.GenerateGoSdk(
package_dir="github.com/open-traffic-generator/openapiart/pkg",
package_name="openapiart",
sdk_version="0.0.1",
split=True,
)
open_api.GenerateGoServer(
module_path="github.com/open-traffic-generator/openapiart/pkg",
Expand Down
7 changes: 6 additions & 1 deletion openapiart/openapiart.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,17 @@ def GeneratePythonSdk(self, package_name, sdk_version=""):
)
return self

def GenerateGoSdk(self, package_dir, package_name, sdk_version=""):
def GenerateGoSdk(
self, package_dir, package_name, sdk_version="", split=True
):
"""Generates a Go UX Sdk
Args
----
- package_dir: Go mod package dir published under go.mod
- package_name: Name of the Go package to generate
- sdk_version: If we want to generate SDk with a custom version
- split: true by default, it creates go files for each interfaces, otherwise it creates a single go file containing all interfaces
Example
-------
Expand Down Expand Up @@ -298,6 +302,7 @@ def GenerateGoSdk(self, package_dir, package_name, sdk_version=""):
"generate_version_api": self._generate_version_api,
"api_version": self._api_version,
"sdk_version": self._go_sdk_version,
"split": split,
}
)
print("Generating go ux sdk: {}".format(" ".join(process_args)))
Expand Down
44 changes: 43 additions & 1 deletion openapiart/openapiartgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ def __init__(self, **kwargs):
"numberdouble": "float64",
"stringbinary": "[]byte",
}
self._interface_count = 0
self._split_file = kwargs.get("split")

def generate(self, openapi):
self._base_url = ""
Expand Down Expand Up @@ -241,7 +243,9 @@ def _write_go_file(self):
self._build_api_interface()
self._build_request_interfaces()
self._write_component_interfaces()
self._close_fp()
if not self._split_file:
self._close_fp()
print("Total no of files created: %s" % str(self._interface_count + 1))

def _write_package_docstring(self, info_object):
"""Write the header of the generated go code file which consists of:
Expand Down Expand Up @@ -302,6 +306,18 @@ def _write_common_code(self):
self._fp = go_pkg_fp
self._filename = go_pkg_filename

def _write_interface_imports(self):
line = 'import {pb_pkg_name} "{go_sdk_pkg_dir}/{pb_pkg_name}"'.format(
pb_pkg_name=self._protobuf_package_name,
go_sdk_pkg_dir=self._go_sdk_package_dir,
)
self._write(line)
self._write('import "google.golang.org/protobuf/types/known/emptypb"')
self._write('import "github.com/ghodss/yaml"')
self._write('import "google.golang.org/protobuf/encoding/protojson"')
self._write('import "google.golang.org/protobuf/proto"')
self._write()

def _write_types(self):
for _, go_type in self._oapi_go_types.items():
if go_type.startswith("String"):
Expand Down Expand Up @@ -979,6 +995,11 @@ def _build_api_interface(self):
)
)

if self._split_file:
# we need to close the original gosnappi file for splitting it.
# Rest of the interfaces will be created in different sub files.
self._close_fp()

def _build_request_interfaces(self):
for new in self._api.external_new_methods:
self._write_interface(new)
Expand Down Expand Up @@ -1142,6 +1163,22 @@ def _write_interface(self, new):
else:
new.generated = True

if self._split_file:
# creating file for each interface
# this change got introduced for splitting up a gosdk into multiple sub-files
fp_name = self._get_file_name(new.interface)
fp_name = os.path.normpath(
os.path.join(self._ux_path, "{}.go".format(fp_name))
)
self._init_fp(fp_name)
self._interface_count += 1
print(
"creating file %s for interface %s, interface-count: %s"
% (fp_name, new.interface, str(self._interface_count))
)
self._write_package()
self._write_interface_imports()

self._build_setters_getters(new)
internal_items = []
internal_items_nil = []
Expand Down Expand Up @@ -1528,6 +1565,11 @@ def _write_interface(self, new):
self._write_validate_method(new)
self._write_default_method(new)

# closing file for interface
if self._split_file:
# need to close the file after each interface
self._close_fp()

def _escaped_str(self, val):
val = val.replace("{", "{{")
return val.replace("}", "}}")
Expand Down
29 changes: 29 additions & 0 deletions openapiart/openapiartplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ def _justify_desc(self, text, indent=0, use_multi=False):
)
return "{}// ".format(indent) + "\n{}// ".format(indent).join(lines)

def _get_file_name(self, name):
file_name = ""
i = 0
while i < len(name):
if name[i].isupper():
j = i
while j < len(name):
if name[j].islower():
break
else:
j += 1
str_ = name[i:j].lower()
if j == len(name):
file_name += "_" + str_
break
elif len(str_) > 1:
file_name += "_" + str_[:-1] + "_" + str_[-1]
i = j - 1
else:
file_name += "_" + name[i].lower()
else:
file_name += name[i].lower()
i += 1
if file_name.startswith("_"):
file_name = file_name[1:]
if file_name.endswith("test"):
file_name = file_name + "_intf"
return file_name


class type_limits(object):
limits = {
Expand Down

0 comments on commit 206ae33

Please sign in to comment.