Skip to content

Commit 9b246e0

Browse files
committed
squashme: add godoc, update README, rename struct
1 parent c569ff4 commit 9b246e0

File tree

6 files changed

+135
-36
lines changed

6 files changed

+135
-36
lines changed

README.md

+24-11
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,35 @@ This parser performs syntactic and semantic validation according to the
1616

1717
- Go library and CLI tool (`publiccode-parser`)
1818
- Supports the latest version of the `publiccode.yml` Standard
19-
- `publiccode-parser` can output validation errors as JSON or in [errorformat](https://vim-jp.org/vimdoc-en/quickfix.html#error-file-format) friendly way
20-
- Verifies the existence of URLs by checking the response for URL fields (can be disabled)
19+
- `publiccode-parser` can output validation errors as JSON or in a
20+
[errorformat](https://vim-jp.org/vimdoc-en/quickfix.html#error-file-format)
21+
friendly way
22+
- Verifies the existence of URLs by checking the response for URL fields
23+
(can be disabled)
2124

2225
## Example
2326

2427
```go
25-
parser := publiccode.NewParser("file:///path/to/local/dir/publiccode.yml")
26-
// OR
27-
// parser := publiccode.NewParser("https://github.com/example/example/publiccode.yml")
28-
29-
// all these settings are optional:
30-
parser.DisableNetwork = true
31-
parser.Branch = "mybranch"
28+
parser, err := NewDefaultParser()
29+
if err != nil {
30+
// handle errors
31+
}
3232

33-
err := parser.Parse()
34-
publiccode := parser.PublicCode
33+
publicCode, err := parser.Parse("https://foobar.example.org/publiccode.yml")
34+
// OR
35+
// publicCode, err := parser.Parse("file:///path/to/local/dir/publiccode.yml")
36+
if err != nil {
37+
// handle errors
38+
}
39+
40+
if publicCode != nil {
41+
switch pc := publicCode.(type) {
42+
case *publiccode.V0:
43+
fmt.Println(pc)
44+
default:
45+
// handle error
46+
}
47+
}
3548
```
3649

3750
## Validation from command line

fields.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type validateFn func (publiccode PublicCode, parser Parser, network bool) error
1616
// with a simple YAML schema.
1717
// It returns any error encountered as ValidationResults.
1818
func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error {
19-
publiccodev0 := publiccode.(PublicCodeV0)
19+
publiccodev0 := publiccode.(V0)
2020

2121
var vr ValidationResults
2222
var err error

parser.go

+100-13
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import (
2222
urlutil "github.com/italia/publiccode-parser-go/v4/internal"
2323
)
2424

25+
// ParserConfig defines the configuration options for the Parser
2526
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
2728
// results in much faster parsing.
2829
DisableNetwork bool
2930

@@ -35,7 +36,9 @@ type ParserConfig struct {
3536
// in the publiccode.yml
3637
Branch string
3738

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
3942
BaseURL string
4043
}
4144

@@ -44,10 +47,7 @@ type Parser struct {
4447
disableNetwork bool
4548
domain Domain
4649
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
5151
}
5252

5353
// Domain is a single code hosting service.
@@ -58,8 +58,21 @@ type Domain struct {
5858
BasicAuth []string `yaml:"basic-auth"`
5959
}
6060

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...
6376
func NewParser(config ParserConfig) (*Parser, error) {
6477
parser := Parser{
6578
disableNetwork: config.DisableNetwork,
@@ -77,11 +90,57 @@ func NewParser(config ParserConfig) (*Parser, error) {
7790
return &parser, nil
7891
}
7992

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...
80107
func NewDefaultParser() (*Parser, error) {
81108
return NewParser(ParserConfig{})
82109
}
83110

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+
// }
85144
func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
86145
b, err := io.ReadAll(in)
87146
if err != nil {
@@ -146,13 +205,13 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
146205
var decodeResults ValidationResults
147206

148207
if version.Value[0] == '0' {
149-
v0 := PublicCodeV0{}
208+
v0 := V0{}
150209
validateFields = validateFieldsV0
151210

152211
decodeResults = decode(b, &v0, node)
153212
publiccode = v0
154213
} else {
155-
v1 := PublicCodeV1{}
214+
v1 := V1{}
156215
validateFields = validateFieldsV1
157216

158217
decodeResults = decode(b, &v1, node)
@@ -200,7 +259,7 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
200259
// TODO: find a cleaner way
201260
key := strings.Replace(
202261
err.Namespace(),
203-
fmt.Sprintf("PublicCodeV%d.", publiccode.Version()),
262+
fmt.Sprintf("V%d.", publiccode.Version()),
204263
"",
205264
1,
206265
)
@@ -262,6 +321,34 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
262321
return publiccode, ve
263322
}
264323

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+
// }
265352
func (p *Parser) Parse(uri string) (PublicCode, error) {
266353
var stream io.Reader
267354

@@ -410,7 +497,7 @@ func decode[T any](data []byte, publiccode *T, node yaml.Node) ValidationResults
410497
return ve
411498
}
412499

413-
// TODO doc
500+
// Turn the input into a valid URL
414501
func toURL(file string) (*url.URL, error) {
415502
if _, u := urlutil.IsValidURL(file); u != nil {
416503
return u, nil

publiccode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package publiccode
22

3-
// SupportedVersions lists the publiccode.yml versions this parser supports.
3+
// SupportedVersions contains the publiccode.yml versions this parser supports.
44
var SupportedVersions = []string{"0.2", "0.2.0", "0.2.1", "0.2.2", "0.3", "0.3.0"}
55

66
type PublicCode interface {

v0.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
urlutil "github.com/italia/publiccode-parser-go/v4/internal"
77
)
88

9-
// PublicCode is a publiccode.yml file definition.
10-
type PublicCodeV0 struct {
9+
// V0 represents a publiccode.yml v0.*
10+
type V0 struct {
1111
PubliccodeYamlVersion string `yaml:"publiccodeYmlVersion" validate:"required,oneof=0.2 0.2.0 0.2.1 0.2.2 0.3 0.3.0"`
1212

1313
Name string `yaml:"name" validate:"required"`
@@ -142,15 +142,15 @@ type ITSectionV0 struct {
142142
} `yaml:"piattaforme"`
143143
}
144144

145-
func (p PublicCodeV0) Version() uint {
145+
func (p V0) Version() uint {
146146
return 0;
147147
}
148148

149-
func (p PublicCodeV0) ToYAML() ([]byte, error) {
149+
func (p V0) ToYAML() ([]byte, error) {
150150
return yaml.Marshal(p)
151151
}
152152

153-
func (p PublicCodeV0) Url() *URL {
153+
func (p V0) Url() *URL {
154154
if p.URL == nil {
155155
return nil
156156
}

v1.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66
urlutil "github.com/italia/publiccode-parser-go/v4/internal"
77
)
88

9-
// PublicCode is a publiccode.yml file definition.
10-
type PublicCodeV1 struct {
9+
type V1 struct {
1110
PubliccodeYamlVersion string `yaml:"publiccodeYmlVersion" validate:"required,oneof=1.0 1.0.0"`
1211

1312
Name string `yaml:"name" validate:"required"`
@@ -138,15 +137,15 @@ type ITSectionV1 struct {
138137
} `yaml:"piattaforme"`
139138
}
140139

141-
func (p PublicCodeV1) Version() uint {
140+
func (p V1) Version() uint {
142141
return 1;
143142

144143
}
145-
func (p PublicCodeV1) ToYAML() ([]byte, error) {
144+
func (p V1) ToYAML() ([]byte, error) {
146145
return yaml.Marshal(p)
147146
}
148147

149-
func (p PublicCodeV1) Url() *URL {
148+
func (p V1) Url() *URL {
150149
if p.URL == nil {
151150
return nil
152151
}

0 commit comments

Comments
 (0)