-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Go Bindings generation #9
base: main
Are you sure you want to change the base?
Conversation
src/shacl2code/model.py
Outdated
@@ -84,6 +84,7 @@ class Class: | |||
id_property: str = "" | |||
node_kind: str = None | |||
is_extensible: bool = False | |||
is_abstract: bool = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should pull out the abstract handling to a different PR (and we can merge it before go support). I can do that if you want
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me figure out if I can make all the classes interfaces. In this case, I won't need this.
} | ||
{%- endmacro %} | ||
|
||
package spdx_v3_0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of hardcoding, this should be specified as a command line argument in GolangRender
src/shacl2code/lang/golang.py
Outdated
# export converts the shacl name into an exportable go type name | ||
name_list = name.split('_') | ||
for i in range(0,len(name_list)): | ||
name_list[i] = name_list[i][0].upper() + name_list[i][1:] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name = "".join(n[0].upper() + n[1:] for n in name.split("_"))
// {{ export(class.clsname) }} provides the {{ export(class.clsname) }} class in the Model | ||
type {{ export(class.clsname) }} struct { | ||
{%- if class.parent_ids %} | ||
{%- for parent in class.parent_ids %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just exploring the possibilities here -- it looks like jinja supports recursive macros, so we do have a fairly easy option to output all properties inline here, as well as all properties in constructor types as I've suggested in one of the golang variants.
@classmethod | ||
def get_arguments(cls, parser): | ||
super().get_arguments(parser) | ||
parser.add_argument("--module", help="Go module name") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably want required=True
to make this option required, since the option is required to generate correct bindings
@@ -86,7 +86,8 @@ type {{ export(class.clsname) }} struct { | |||
{%- endfor %} | |||
{%- endmacro %} | |||
|
|||
package spdx_v3_0 | |||
{# go file content #} | |||
package spdx_v{{ module }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
package {{ module }}
Remember that shacl2code is not (intended) to be specific to SPDX :)
- Added lang/golang.py which has the boilerplate python code that uses the BasicJinjaRender to render 1 go file for the whole SPDX model. - Added lang/templates/golang.j2 which is the jinja template for go code. - Modified the lang/__init__.py file to register the new template called "golang". - Modified the model.py to deal with shacl2code specific annotation to identify which classes are abstract or not. The generated go code does not compile yet. Signed-off-by: Nisha Kumar <nisha.kumar@oracle.com>
Signed-off-by: Keith Zantow <kzantow@gmail.com>
The "module" variable is passed via command line argument `--module`. The incantation to use it currently is: ``` shacl2code generate -i https://spdx.org/rdf/3.0.0/spdx-model.ttl -i https://spdx.org/rdf/3.0.0/spdx-json-serialize-annotations.ttl -x https://spdx.github.io/spdx-spec/v3.0/model/spdx-context.jsonld golang -o ../spdx3/spdx.go --module 3_0_0 ``` Signed-off-by: Nisha Kumar <nisha.kumar@oracle.com>
- Use the golang.py DATATYPES instead of the one in the jinja template. - Organized the macros. - Added checks for abstract vs concrete classes Signed-off-by: Nisha Kumar <nisha.kumar@oracle.com>
The generated go code does not compile yet.