Skip to content
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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Conversation

nishakm
Copy link

@nishakm nishakm commented May 8, 2024

  • 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.

@nishakm
Copy link
Author

nishakm commented May 8, 2024

@JPEWdev @kzantow

@nishakm nishakm marked this pull request as draft May 8, 2024 16:26
@@ -84,6 +84,7 @@ class Class:
id_property: str = ""
node_kind: str = None
is_extensible: bool = False
is_abstract: bool = False
Copy link
Owner

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

Copy link
Author

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
Copy link
Owner

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

# 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:]
Copy link
Owner

@JPEWdev JPEWdev May 8, 2024

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 %}
Copy link

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")
Copy link
Owner

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 }}
Copy link
Owner

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 :)

nishakm and others added 4 commits May 21, 2024 15:07
- 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants