@@ -22,8 +22,9 @@ import (
22
22
urlutil "github.com/italia/publiccode-parser-go/v4/internal"
23
23
)
24
24
25
+ // ParserConfig defines the configuration options for the Parser
25
26
type ParserConfig struct {
26
- // DisableNetwork disables all network tests (URL existence and Oembed). This
27
+ // DisableNetwork disables all network tests (fe. URL existence and Oembed). This
27
28
// results in much faster parsing.
28
29
DisableNetwork bool
29
30
@@ -35,7 +36,9 @@ type ParserConfig struct {
35
36
// in the publiccode.yml
36
37
Branch string
37
38
38
- // TODO: doc
39
+ // The path or URL used as base of relative files in publiccode.yml (eg. logo). If
40
+ // given, it will be used for existence checks and such, instead of the `url` key
41
+ // in the publiccode.yml file
39
42
BaseURL string
40
43
}
41
44
@@ -44,10 +47,7 @@ type Parser struct {
44
47
disableNetwork bool
45
48
domain Domain
46
49
branch string
47
-
48
- // The URL used as base of relative files in publiccode.yml (eg. authorsFile)
49
- // It can be a local file with the 'file' scheme.
50
- baseURL * url.URL
50
+ baseURL * url.URL
51
51
}
52
52
53
53
// Domain is a single code hosting service.
@@ -58,8 +58,21 @@ type Domain struct {
58
58
BasicAuth []string `yaml:"basic-auth"`
59
59
}
60
60
61
- // NewParser initializes a new Parser object and returns it.
62
- // TODO
61
+ // NewParser creates and returns a new Parser instance based on the provided ParserConfig.
62
+ //
63
+ // It returns a pointer to the newly created Parser, if successful, and an error.
64
+ // The error is non-nil if there was an issue initializing the Parser.
65
+ //
66
+ // Example usage:
67
+ //
68
+ // config := ParserConfig{
69
+ // DisableNetwork: true
70
+ // }
71
+ // parser, err := NewParser(config)
72
+ // if err != nil {
73
+ // log.Fatalf("Failed to create parser: %v", err)
74
+ // }
75
+ // // Use parser...
63
76
func NewParser (config ParserConfig ) (* Parser , error ) {
64
77
parser := Parser {
65
78
disableNetwork : config .DisableNetwork ,
@@ -77,11 +90,57 @@ func NewParser(config ParserConfig) (*Parser, error) {
77
90
return & parser , nil
78
91
}
79
92
93
+ // NewDefaultParser creates and returns a new Parser instance, using the default config.
94
+ //
95
+ // It returns a pointer to the newly created Parser, if successful, and an error.
96
+ // The error is non-nil if there was an issue initializing the Parser.
97
+ //
98
+ // The default config is ParserConfig's fields zero values.
99
+ //
100
+ // Example usage:
101
+ //
102
+ // parser, err := NewDefaultParser()
103
+ // if err != nil {
104
+ // log.Fatalf("Failed to create parser: %v", err)
105
+ // }
106
+ // // Use parser...
80
107
func NewDefaultParser () (* Parser , error ) {
81
108
return NewParser (ParserConfig {})
82
109
}
83
110
84
- // ParseStream reads the data and tries to parse it. Returns an error if fails.
111
+ // ParseStream reads from the provided io.Reader and attempts to parse the input
112
+ // stream into a PublicCode object.
113
+ //
114
+ // Returns a non-nil error if there is an issue with the parsing process and a
115
+ // a struct implementing the Publiccode interface, depending on the version
116
+ // of the publiccode.yml file parsed.
117
+ //
118
+ // The only possible type returned is V0, representing v0.* files.
119
+ //
120
+ // PublicCode can be nil when there are major parsing issues.
121
+ // It can also be non-nil even in presence of errors: in that case the returned struct
122
+ // is filled as much as possible, based on what is successful parsed.
123
+ //
124
+ // Example usage:
125
+ //
126
+ // file, err := os.Open("publiccode.yml")
127
+ // if err != nil {
128
+ // log.Fatalf("Failed to open file: %v", err)
129
+ // }
130
+ // defer file.Close()
131
+ //
132
+ // publicCode, err := parser.ParseStream(file)
133
+ // if err != nil {
134
+ // log.Printf("Parsing errors: %v", err)
135
+ // }
136
+ // if publicCode != nil {
137
+ // switch pc := publicCode.(type) {
138
+ // case *publiccode.V0:
139
+ // fmt.Println(pc)
140
+ // default:
141
+ // log.Println("PublicCode parsed, but unexpected type found.")
142
+ // }
143
+ // }
85
144
func (p * Parser ) ParseStream (in io.Reader ) (PublicCode , error ) {
86
145
b , err := io .ReadAll (in )
87
146
if err != nil {
@@ -146,13 +205,13 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
146
205
var decodeResults ValidationResults
147
206
148
207
if version .Value [0 ] == '0' {
149
- v0 := PublicCodeV0 {}
208
+ v0 := V0 {}
150
209
validateFields = validateFieldsV0
151
210
152
211
decodeResults = decode (b , & v0 , node )
153
212
publiccode = v0
154
213
} else {
155
- v1 := PublicCodeV1 {}
214
+ v1 := V1 {}
156
215
validateFields = validateFieldsV1
157
216
158
217
decodeResults = decode (b , & v1 , node )
@@ -200,7 +259,7 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
200
259
// TODO: find a cleaner way
201
260
key := strings .Replace (
202
261
err .Namespace (),
203
- fmt .Sprintf ("PublicCodeV %d." , publiccode .Version ()),
262
+ fmt .Sprintf ("V %d." , publiccode .Version ()),
204
263
"" ,
205
264
1 ,
206
265
)
@@ -262,6 +321,34 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
262
321
return publiccode , ve
263
322
}
264
323
324
+ // Parse reads from the provided uri and attempts to parse it into
325
+ // a PublicCode object.
326
+ //
327
+ // Returns a non-nil error if there is an issue with the parsing process and a
328
+ // a struct implementing the Publiccode interface, depending on the version
329
+ // of the publiccode.yml file parsed.
330
+ //
331
+ // The only possible type returned is V0, representing v0.* files.
332
+ //
333
+ // PublicCode can be nil when there are major parsing issues.
334
+ // It can also be non-nil even in presence of errors: in that case the returned struct
335
+ // is filled as much as possible, based on what is successful parsed.
336
+ //
337
+ // Example usage:
338
+ //
339
+ // // publicCode, err := parser.Parse("file:///app/publiccode.yml")
340
+ // publicCode, err := parser.Parse("https://foobar.example.org/publiccode.yml")
341
+ // if err != nil {
342
+ // log.Printf("Parsing errors: %v", err)
343
+ // }
344
+ // if publicCode != nil {
345
+ // switch pc := publicCode.(type) {
346
+ // case *publiccode.V0:
347
+ // fmt.Println(pc)
348
+ // default:
349
+ // log.Println("PublicCode parsed, but unexpected type found.")
350
+ // }
351
+ // }
265
352
func (p * Parser ) Parse (uri string ) (PublicCode , error ) {
266
353
var stream io.Reader
267
354
@@ -410,7 +497,7 @@ func decode[T any](data []byte, publiccode *T, node yaml.Node) ValidationResults
410
497
return ve
411
498
}
412
499
413
- // TODO doc
500
+ // Turn the input into a valid URL
414
501
func toURL (file string ) (* url.URL , error ) {
415
502
if _ , u := urlutil .IsValidURL (file ); u != nil {
416
503
return u , nil
0 commit comments