Skip to content

Commit 2db998f

Browse files
authored
feat: improve retrocompatibility (#273)
* Add OrganisationV0 type * Autofill organisation.uri if it.riuso.codiceIPA. This makes things easier for consumer as the only field to check is organisation.uri * Don't serialize it., IT. is always guaranteed to be present and have the same data as it.
1 parent 24a704b commit 2db998f

3 files changed

Lines changed: 36 additions & 15 deletions

File tree

fields.go

Lines changed: 1 addition & 1 deletion
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 { //nolint:maintidx
19-
publiccodev0 := publiccode.(PublicCodeV0)
19+
publiccodev0 := publiccode.(*PublicCodeV0)
2020

2121
var vr ValidationResults
2222

parser.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) { //nolint:mainti
179179
var decodeResults ValidationResults
180180

181181
if version.Value[0] == '0' {
182-
v0 := PublicCodeV0{}
182+
v0 := &PublicCodeV0{}
183183
validateFields = validateFieldsV0
184184

185-
decodeResults = decode(b, &v0, node)
185+
decodeResults = decode(b, v0, node)
186186
publiccode = v0
187187
}
188188

@@ -301,10 +301,24 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) { //nolint:mainti
301301
}
302302
}
303303

304-
// If the deprecated 'it' was used instead of 'IT', copy the data
305-
// in the canonical 'IT' field.
306-
if v0, ok := publiccode.(PublicCodeV0); ok {
307-
v0.IT = v0.It
304+
// v0: Copy data from deprecated fields to the canonical ones, where possible
305+
if v0, ok := publiccode.(*PublicCodeV0); ok {
306+
// Auto-copy the deprecated field into the new one, so we can guarantee
307+
// to the user that `IT` is always the field to check
308+
if v0.It != nil && v0.IT == nil {
309+
v0.IT = v0.It
310+
}
311+
312+
it := v0.IT
313+
314+
// Auto-copy the deprecated field into the new one, so we can guarantee
315+
// to the user that `organisation` is always the field to check
316+
if it != nil && it.Riuso.CodiceIPA != "" && v0.Organisation == nil {
317+
v0.Organisation = &OrganisationV0{}
318+
319+
v0.Organisation.URI = "urn:x-italian-pa:" + it.Riuso.CodiceIPA
320+
v0.Organisation.Name = v0.Legal.RepoOwner
321+
}
308322
}
309323

310324
if len(ve) == 0 {

v0.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ type PublicCodeV0 struct {
2020
Logo *string `json:"logo,omitempty" yaml:"logo,omitempty"`
2121
MonochromeLogo *string `json:"monochromeLogo,omitempty" yaml:"monochromeLogo,omitempty"`
2222

23-
Organisation *struct {
24-
Name *string `json:"name,omitempty" yaml:"name,omitempty"`
25-
URI string `json:"uri" validate:"required,organisation_uri" yaml:"uri"`
26-
} `json:"organisation,omitempty" yaml:"organisation,omitempty"`
23+
Organisation *OrganisationV0 `json:"organisation,omitempty" yaml:"organisation,omitempty"`
2724

2825
InputTypes *[]string `json:"inputTypes,omitempty" validate:"omitempty,dive,is_mime_type" yaml:"inputTypes,omitempty"`
2926
OutputTypes *[]string `json:"outputTypes,omitempty" validate:"omitempty,dive,is_mime_type" yaml:"outputTypes,omitempty"`
@@ -34,10 +31,7 @@ type PublicCodeV0 struct {
3431

3532
UsedBy *[]string `json:"usedBy,omitempty" yaml:"usedBy,omitempty"`
3633

37-
FundedBy *[]struct {
38-
Name *string `json:"name,omitempty" yaml:"name,omitempty"`
39-
URI string `json:"uri" validate:"required,organisation_uri" yaml:"uri"`
40-
} `json:"fundedBy,omitempty" validate:"omitempty,dive" yaml:"fundedBy,omitempty"`
34+
FundedBy *[]OrganisationV0 `json:"fundedBy,omitempty" validate:"omitempty,dive" yaml:"fundedBy,omitempty"`
4135

4236
Roadmap *URL `json:"roadmap,omitempty" validate:"omitnil,url_http_url" yaml:"roadmap,omitempty"`
4337

@@ -123,6 +117,12 @@ type DependencyV0 struct {
123117
Version *string `json:"version,omitempty" yaml:"version,omitempty"`
124118
}
125119

120+
// OrganisationV0 describes a real world organisation.
121+
type OrganisationV0 struct {
122+
Name *string `json:"name,omitempty" yaml:"name,omitempty"`
123+
URI string `json:"uri" validate:"required,organisation_uri" yaml:"uri"`
124+
}
125+
126126
// Country-specific sections
127127
//
128128
// While the standard is structured to be meaningful on an international level,
@@ -157,6 +157,13 @@ func (p PublicCodeV0) Version() uint {
157157
}
158158

159159
func (p PublicCodeV0) ToYAML() ([]byte, error) {
160+
// Don't marshal lowercase country section.
161+
// The uppercase one is always guaranteed to have the same data
162+
// because we copy it for backwards compatibility
163+
if p.It != nil {
164+
p.It = nil
165+
}
166+
160167
return yaml.Marshal(p)
161168
}
162169

0 commit comments

Comments
 (0)