• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

spdx / tools-golang / 10193235292

01 Aug 2024 06:28AM UTC coverage: 54.995% (-40.5%) from 95.483%
10193235292

Pull #247

github

kzantow
chore: require go 1.20

Signed-off-by: Keith Zantow <kzantow@gmail.com>
Pull Request #247: feat: prototype v3_0 model

504 of 6474 new or added lines in 3 files covered. (7.78%)

4 existing lines in 1 file now uncovered.

7712 of 14023 relevant lines covered (55.0%)

12.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.77
/spdx/v3/v3_0/spdx.go
1
package v3_0
2

3
import (
4
        "encoding/json"
5
        "fmt"
6
        "io"
7
        "reflect"
8
        "time"
9
)
10

11
/*
12
SPDX 3 models and serialization code is generated from prototype golang support for shacl2code,
13
https://github.com/kzantow-anchore/shacl2code/tree/golang-bindings (with contributions from Nisha and Keith)
14

15
To regenerate, use something like this command:
16
.venv/bin/python -m 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.org/rdf/3.0.0/spdx-context.jsonld golang --package v3_0 --license MIT --output $HOME/projects/tools-golang/spdx/v3/v3_0/model.go --remap-props element=elements,extension=extensions,externalIdentifier=externalIdentifiers,externalRef=externalRefs,rootElement=rootElements
17
*/
18

19
type Document struct {
20
        creationInfo *CreationInfo
21
        document     *SpdxDocument
22
        graph        []any
23
        ldc          ldContext
24
}
25

26
func NewDocument(creator IAgent) *Document {
4✔
27
        ci := &CreationInfo{
4✔
28
                Created: time.Now().Format(time.RFC3339),
4✔
29
                CreatedBy: []IAgent{
4✔
30
                        creator,
4✔
31
                },
4✔
32
        }
4✔
33
        creator.SetCreationInfo(ci)
4✔
34
        return &Document{
4✔
35
                creationInfo: ci,
4✔
36
                document: &SpdxDocument{
4✔
37
                        CreationInfo: ci,
4✔
38
                },
4✔
39
                graph: []any{ci, creator},
4✔
40
                ldc:   ldGlobal,
4✔
41
        }
4✔
42
}
4✔
43

44
func (d *Document) CreationInfo() ICreationInfo {
1✔
45
        return d.creationInfo
1✔
46
}
1✔
47

48
func (d *Document) AddElement(e ...IElement) {
5✔
49
        d.document.RootElements = append(d.document.RootElements, e...)
5✔
50
        d.document.Elements = append(d.document.Elements, e...)
5✔
51
}
5✔
52

53
func (d *Document) GetElements() []IElement {
1✔
54
        return d.document.RootElements
1✔
55
}
1✔
56

57
func (d *Document) Document() *SpdxDocument {
1✔
58
        return d.document
1✔
59
}
1✔
60

NEW
61
func (d *Document) Packages() []IPackage {
×
NEW
62
        return get[IPackage](d)
×
NEW
63
}
×
64

NEW
65
func (d *Document) Relationships() Relationships {
×
NEW
66
        return Relationships{get[IRelationship](d)}
×
NEW
67
}
×
68

NEW
69
func (d *Document) Files() []IFile {
×
NEW
70
        return get[IFile](d)
×
NEW
71
}
×
72

73
func (d *Document) ToJSON(writer io.Writer) error {
4✔
74
        // all elements need to have creationInfo set...
4✔
75
        if d.creationInfo != nil {
8✔
76
                d.setCreationInfo(map[reflect.Value]struct{}{}, reflect.ValueOf(d.creationInfo), reflect.ValueOf(d.document))
4✔
77
        }
4✔
78
        maps, err := d.ldc.toMaps(d.document)
4✔
79
        if err != nil {
4✔
NEW
80
                return err
×
NEW
81
        }
×
82
        enc := json.NewEncoder(writer)
4✔
83
        enc.SetEscapeHTML(false)
4✔
84
        enc.SetIndent("", "  ")
4✔
85
        return enc.Encode(maps)
4✔
86
}
87

88
var iCreationInfoType = reflect.TypeOf((*ICreationInfo)(nil)).Elem()
89

90
func (d *Document) setCreationInfo(visited map[reflect.Value]struct{}, creationInfo, v reflect.Value) {
723✔
91
        if _, ok := visited[v]; ok {
756✔
92
                return
33✔
93
        }
33✔
94
        visited[v] = struct{}{}
690✔
95
        switch v.Kind() {
690✔
96
        case reflect.Interface:
82✔
97
                if v.IsNil() {
101✔
98
                        if v.Type().Implements(iCreationInfoType) {
26✔
99
                                v.Set(creationInfo)
7✔
100
                        }
7✔
101
                } else {
63✔
102
                        d.setCreationInfo(visited, creationInfo, v.Elem())
63✔
103
                }
63✔
104
        case reflect.Pointer:
34✔
105
                if v.IsNil() {
34✔
NEW
106
                        return
×
NEW
107
                }
×
108
                d.setCreationInfo(visited, creationInfo, v.Elem())
34✔
109
        case reflect.Struct:
128✔
110
                for i := 0; i < v.NumField(); i++ {
706✔
111
                        d.setCreationInfo(visited, creationInfo, v.Field(i))
578✔
112
                }
578✔
113
        case reflect.Slice:
192✔
114
                for i := 0; i < v.Len(); i++ {
236✔
115
                        d.setCreationInfo(visited, creationInfo, v.Index(i))
44✔
116
                }
44✔
117
        default:
254✔
118
        }
119
}
120

121
func (d *Document) FromJSON(reader io.Reader) error {
2✔
122
        graph, err := d.ldc.FromJSON(reader)
2✔
123
        if err != nil {
2✔
NEW
124
                return err
×
NEW
125
        }
×
126
        d.graph = append(d.graph, graph)
2✔
127
        for _, e := range graph {
19✔
128
                if doc, ok := e.(*SpdxDocument); ok {
19✔
129
                        d.document = doc
2✔
130
                        return nil
2✔
131
                }
2✔
132
        }
NEW
133
        return fmt.Errorf("no document found")
×
134
}
135

NEW
136
func get[T any](ctx *Document) []T {
×
NEW
137
        var out []T
×
NEW
138
        for _, i := range ctx.graph {
×
NEW
139
                if i, ok := i.(T); ok {
×
NEW
140
                        out = append(out, i)
×
NEW
141
                }
×
142
        }
NEW
143
        return out
×
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc