Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
The initial implementation of the SPDX go bindings
  • Loading branch information
JPEWdev committed Jan 13, 2025
1 parent 8b5669e commit fc6d5d0
Show file tree
Hide file tree
Showing 25 changed files with 18,749 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
name: Test SPDX Go Bindings
on:
- push
- pull_request
- workflow_call

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version:
- "1.18"
- "1.19"
- "1.20"
- "1.21"
- "1.22"
- "1.23"
steps:
- uses: actions/checkout@v4
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Install dependencies
run: go get .
- name: Run tests
run: go test
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
# spdx-go-model-3

Generated Golang code for the SPDX Spec version 3

All bindings in this repository are generated using
[shacl2code](https://github.com/JPEWdev/shacl2code) at the time the package is
built with the `generate-bindings` script. The binding should not be manually
edited.

**NOTE:** The bindings are pretty low level, intended for more directly
manipulating SPDX files. While they are fully functions, they lack higher level
helper functions that may be useful for creating SPDX documents. If you want a
higher level approach, please see the
[SPDX Golang Tools](https://github.com/spdx/tools-golang) (however this repo
doesn't yet support SPDX 3)

## Usage

Each version of the SPDX spec has a module named `v{MAJOR}_{MINOR}_{MICRO}`
that contains the bindings for that version under the `github.com/spdx/spdx-go-model`
top level modules. It is recommend you import this with an alias like:

```golang
import (
Spdx3_0_1 "github.com/spdx/spdx-go-model/v3_0_1"
)

...
p := Spdx3_0_1.MakePerson()
...
```

## Testing

This repository has support for running tests against the generated bindings using
`go test`.
36 changes: 36 additions & 0 deletions generate-bindings
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#! /bin/sh
#
# SPDX-License-Identifier: Apache-2.0

SPDX_VERSIONS="3.0.1"
SHACL2CODE_VERSION="0.0.18"

THIS_DIR="$(dirname "$0")"
VENV_DIR="$(mktemp -d)"

cd $THIS_DIR

# Install shacl2code in a virtual environment
python3 -m venv "$VENV_DIR"
"$VENV_DIR/bin/pip" install -q "shacl2code==$SHACL2CODE_VERSION"

# Remove existing go files in subdirectories
find . -path './*/*.go' -delete

for v in $SPDX_VERSIONS; do
MODNAME="spdx_v$(echo "$v" | sed 's/[^a-zA-Z0-9_]/_/g')"
rm -rf "$MODNAME"
mkdir -p "$MODNAME"

"$VENV_DIR/bin/shacl2code" generate \
--input https://spdx.org/rdf/$v/spdx-model.ttl \
--input https://spdx.org/rdf/$v/spdx-json-serialize-annotations.ttl \
--context https://spdx.org/rdf/$v/spdx-context.jsonld \
--license Apache-2.0 \
golang \
-o "$MODNAME" -p "$MODNAME"
done

go mod tidy

rm -rf "$VENV_DIR"
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/spdx/spdx-go-model

go 1.18

require github.com/ncruces/go-strftime v0.1.9
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
12 changes: 12 additions & 0 deletions import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package spdx_go_test

import (
"testing"

"github.com/spdx/spdx-go-model/spdx_v3_0_1"
)


func TestImport(t *testing.T) {
spdx_v3_0_1.MakePerson()
}
Loading

0 comments on commit fc6d5d0

Please sign in to comment.