Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/coverage-generate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ jobs:
sudo apt install -y build-essential cppcheck doxygen graphviz
npm install -g jsonld-cli
npm --prefix scripts ci
- name: Install Go static analysis tools
run: |
echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH"
go install honnef.co/go/tools/cmd/staticcheck@latest
- name: Install Rust static analysis tools
run: |
rustup component add clippy
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ jobs:
run: |
brew update
brew install cppcheck doxygen gcc go graphviz
- name: Install Go static analysis tools
run: |
echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH"
go install honnef.co/go/tools/cmd/staticcheck@latest
- name: Install Rust static analysis tools
run: |
rustup component add clippy
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dev = [
"mypy >= 1.19.1", # latest version for Python 3.9 is 1.19.x
"pyrefly >= 0.55.0",
"pyright >= 1.1.403",
"pyshacl >= 0.25.0",
"pyshacl >= 0.25.0, < 0.40.0", # 0.40.0 uses `str | X` at runtime, breaks on Python < 3.10
"pytest >= 7.4",
"pytest-cov >= 4.1",
"pytest-xdist >= 3.5",
Expand Down
2 changes: 1 addition & 1 deletion src/shacl2code/lang/golang.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ def __init__(self, args):
# template
"go_package": textwrap.dedent(f"""
*/

package {args.package}
/*"""),
"package_name": args.package,
}

@classmethod
Expand Down
31 changes: 23 additions & 8 deletions src/shacl2code/lang/templates/golang/classes.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand All @@ -14,10 +15,17 @@ import (

/*{{ '*' }}{{ '/' }}
{% for class in classes %}
{%- for l in class.comment.splitlines() %}
{{ ("// " + l).rstrip() }}
{% endfor -%}
{%- set class_comment_lines = class.comment.splitlines() %}
{%- if class_comment_lines %}
// {{ (struct_name(class) + " " + class_comment_lines[0]).rstrip() }}
{%- for l in class_comment_lines[1:] %}
// {{ l }}
{%- endfor %}
{%- else %}
// {{ struct_name(class) }} is a generated SHACL object type.
{%- endif %}
{%- if class.deprecated %}
//
// Deprecated: This class is deprecated
{%- endif %}
{% set is_public = not class.is_abstract or class.is_extensible -%}
Expand All @@ -44,10 +52,17 @@ type {{ struct_name(class) }} struct {
{%- endfor %}
}
{% for member in class.named_individuals %}
{%- for l in member.comment.splitlines() %}
{{ ("// " + l).rstrip() }}
{% endfor -%}
const {{ varname(*class.clsname) }}{{ varname(member.varname) }} = "{{ member._id }}"
{%- set constname = varname(*class.clsname) + varname(member.varname) %}
{%- set comment_lines = member.comment.splitlines() %}
{%- if comment_lines %}
// {{ (constname + " " + comment_lines[0]).rstrip() }}
{%- for l in comment_lines[1:] %}
// {{ l }}
{%- endfor %}
{% else %}
// {{ constname }} is a generated named individual value.
{% endif -%}
const {{ constname }} = "{{ member._id }}"
{%- endfor %}

type {{ struct_name(class) }}Type struct {
Expand Down Expand Up @@ -184,7 +199,7 @@ func Make{{ varname(*class.clsname) }}Ref() Ref[{{ interface_name(class) }}] {
{%- endif %}

func (self *{{ struct_name(class) }}) Validate(path Path, handler ErrorHandler) bool {
var valid bool = true
var valid = true
{%- if class.parent_ids %}
{%- for p in class.parent_ids %}
if ! self.{{ struct_name(classes.get(p)) }}.Validate(path, handler) {
Expand Down
14 changes: 7 additions & 7 deletions src/shacl2code/lang/templates/golang/decode.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down Expand Up @@ -230,11 +231,10 @@ func DecodeInteger(data any, path Path, context map[string]string, check DecodeC
return 0, err
}

switch data.(type) {
switch v := data.(type) {
case int:
return data.(int), nil
return v, nil
case float64:
v := data.(float64)
if v == float64(int64(v)) {
return int(v), nil
}
Expand All @@ -249,15 +249,15 @@ func DecodeFloat(data any, path Path, context map[string]string, check DecodeChe
return 0, err
}

switch data.(type) {
switch v := data.(type) {
case float64:
return data.(float64), nil
return v, nil
case string:
v, err := strconv.ParseFloat(data.(string), 64)
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return 0, err
}
return v, nil
return f, nil
default:
return 0, &DecodeError{path, "Float expected. Got " + reflect.TypeOf(data).Name()}
}
Expand Down
1 change: 1 addition & 0 deletions src/shacl2code/lang/templates/golang/encode.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down
1 change: 1 addition & 0 deletions src/shacl2code/lang/templates/golang/errorhandler.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down
7 changes: 4 additions & 3 deletions src/shacl2code/lang/templates/golang/errors.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
{{ go_package }} */

// Validation Error
// ValidationError reports a SHACL validation failure.
type ValidationError struct {
Property string
Err string
}

func (e *ValidationError) Error() string { return e.Property + ": " + e.Err }

// Conversion Error
// ConversionError reports a failed reference conversion.
type ConversionError struct {
From string
To string
Expand All @@ -26,7 +27,7 @@ func (e *ConversionError) Error() string {
return "Unable to convert from " + e.From + " to " + e.To
}

// Decode Error
// DecodeError reports a SHACL decode failure.
type DecodeError struct {
Path Path
Err string
Expand Down
5 changes: 2 additions & 3 deletions src/shacl2code/lang/templates/golang/extensible.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down Expand Up @@ -34,9 +35,7 @@ func (self *SHACLExtensibleBase) EncodeExtProperties(data map[string]any, path P
}

lst := []any{}
for _, v := range values {
lst = append(lst, v)
}
lst = append(lst, values...)
data[state.CompactIRI(k)] = lst
}
return nil
Expand Down
1 change: 1 addition & 0 deletions src/shacl2code/lang/templates/golang/linkstate.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down
3 changes: 2 additions & 1 deletion src/shacl2code/lang/templates/golang/listproperty.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down Expand Up @@ -70,7 +71,7 @@ func (self *ListProperty[T]) Delete() {
}

func (self *ListProperty[T]) IsSet() bool {
return self.value != nil && len(self.value) > 0
return len(self.value) > 0
}

func (self *ListProperty[T]) Check(path Path, handler ErrorHandler) bool {
Expand Down
1 change: 1 addition & 0 deletions src/shacl2code/lang/templates/golang/optional.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down
1 change: 1 addition & 0 deletions src/shacl2code/lang/templates/golang/path.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down
1 change: 1 addition & 0 deletions src/shacl2code/lang/templates/golang/property.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down
8 changes: 5 additions & 3 deletions src/shacl2code/lang/templates/golang/ref.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand All @@ -12,7 +13,7 @@ import (
"reflect"
)

// Reference
// Ref is a reference to a SHACL object.
type Ref[T SHACLObject] interface {
GetIRI() string
GetObj() T
Expand Down Expand Up @@ -79,8 +80,9 @@ func MakeIRIRef[T SHACLObject](iri string) Ref[T] {
return &ref[T]{nil, iri}
}

// Convert one reference to another. Note that the output type is first so it
// can be specified, while the input type is generally inferred from the argument
// ConvertRef converts a reference from one type to another.
// Note that the output type is first so it can be specified,
// while the input type is generally inferred from the argument.
func ConvertRef[TO SHACLObject, FROM SHACLObject](in Ref[FROM]) (Ref[TO], error) {
if in.IsObj() {
out_obj, ok := any(in.GetObj()).(TO)
Expand Down
1 change: 1 addition & 0 deletions src/shacl2code/lang/templates/golang/reflistproperty.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down
1 change: 1 addition & 0 deletions src/shacl2code/lang/templates/golang/refproperty.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down
5 changes: 3 additions & 2 deletions src/shacl2code/lang/templates/golang/shaclobject.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
{{ go_package }} */

type Visit func(Path, any)

// Base SHACL Object
// SHACLObjectBase is the base type embedded by all SHACL objects.
type SHACLObjectBase struct {
// Object ID
id Property[string]
Expand All @@ -21,7 +22,7 @@ type SHACLObjectBase struct {
func (self *SHACLObjectBase) ID() PropertyInterface[string] { return &self.id }

func (self *SHACLObjectBase) Validate(path Path, handler ErrorHandler) bool {
var valid bool = true
var valid = true

switch self.typ.GetNodeKind() {
case NodeKindBlankNode:
Expand Down
8 changes: 4 additions & 4 deletions src/shacl2code/lang/templates/golang/shaclobjectset.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down Expand Up @@ -173,7 +174,7 @@ func (self *SHACLObjectSetObject) Decode(decoder *json.Decoder) error {
}

// Check context strings. Using maps.Equal() would be much better, but requires go >= 1.21
sort.Sort(sort.StringSlice(context_urls))
sort.Strings(context_urls)

if !reflect.DeepEqual(context_urls, CONTEXT_URLS) {
return &DecodeError{sub_path, "Wrong context URL(s)"}
Expand Down Expand Up @@ -304,7 +305,7 @@ func (self *SHACLObjectSetObject) Encode(encoder *json.Encoder) error {

// Convert to a list and sort
top_list := []SHACLObject{}
for o, _ := range top_objects {
for o := range top_objects {
top_list = append(top_list, o)
}

Expand Down Expand Up @@ -348,9 +349,8 @@ func (self *SHACLObjectSetObject) Walk(visit Visit) {
visited := map[SHACLObject]bool{}

visit_proxy := func(path Path, v any) {
switch v.(type) {
switch r := v.(type) {
case Ref[SHACLObject]:
r := v.(Ref[SHACLObject])
if !r.IsObj() {
visit(path, v)
return
Expand Down
1 change: 1 addition & 0 deletions src/shacl2code/lang/templates/golang/shacltype.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand Down
3 changes: 2 additions & 1 deletion src/shacl2code/lang/templates/golang/util.go.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vim: ft=go
*/ // This line keeps gofmt happy
package shacl2code /*
#}
Package {{ package_name }}:
{{ disclaimer }}

SPDX-License-Identifier: {{ spdx_license }}
Expand All @@ -12,7 +13,7 @@ import (
"strings"
)

// IRI Validation
// IsIRI reports whether iri is a valid IRI, not a blank node.
func IsIRI(iri string) bool {
if strings.HasPrefix(iri, "_:") {
return false
Expand Down
Loading
Loading