Skip to content

Commit

Permalink
mod/modfile: add FixLegacy function
Browse files Browse the repository at this point in the history
This implements the core functionality required
to migrate legacy module.cue files to the new syntax.

For #3137

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I27cf3e7241d64d56398289c1400d51ef79b0f813
  • Loading branch information
rogpeppe committed May 15, 2024
1 parent 443106f commit 47e0097
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
69 changes: 69 additions & 0 deletions mod/modfile/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,75 @@ func ParseNonStrict(modfile []byte, filename string) (*File, error) {
return parse(modfile, filename, false)
}

// FixLegacy converts a legacy module.cue file as parsed by [ParseLegacy]
// into a format suitable for parsing with [Parse]. It adds a language.version
// field and moves all unrecognized fields into custom.legacy.
//
// If there is no module field or it is empty, it sets it to "test.example@v0".
//
// If the file already parses OK with [ParseNonStrict], it returns the
// result of that.
func FixLegacy(modfile []byte, filename string) (*File, error) {
f, err := ParseNonStrict(modfile, filename)
if err == nil {
// It parses OK so it doesn't need fixing.
return f, nil
}
ctx := cuecontext.New()
file, err := parseDataOnlyCUE(ctx, modfile, filename)
if err != nil {
return nil, errors.Wrapf(err, token.NoPos, "invalid module.cue file syntax")
}
v := ctx.BuildFile(file)
if err := v.Validate(cue.Concrete(true)); err != nil {
return nil, errors.Wrapf(err, token.NoPos, "invalid module.cue file value")
}
var allFields map[string]any
if err := v.Decode(&allFields); err != nil {
return nil, err
}
mpath := "test.example@v0"
if m, ok := allFields["module"]; ok {
if mpath1, ok := m.(string); ok && mpath1 != "" {
mpath = mpath1
} else if !ok {
return nil, fmt.Errorf("module field has unexpected type %T", m)
}
// What to do if the module path isn't OK according to the new rules?
}
customLegacy := make(map[string]any)
for k, v := range allFields {
if k != "module" {
customLegacy[k] = v
}
}
var custom map[string]map[string]any
if len(customLegacy) > 0 {
custom = map[string]map[string]any{
"legacy": customLegacy,
}
}
f = &File{
Module: mpath,
Language: &Language{
Version: cueversion.LanguageVersion(),
},
Custom: custom,
}
// Round-trip through [Parse] so that we get exactly the same
// result as a later parse of the same data will. This also
// adds a major version to the module path if needed.
data, err := f.Format()
if err != nil {
return nil, fmt.Errorf("cannot format fixed file: %v", err)
}
f, err = ParseNonStrict(data, "fixed-"+filename)
if err != nil {
return nil, fmt.Errorf("cannot round-trip fixed module file %q: %v", data, err)
}
return f, nil
}

func parse(modfile []byte, filename string, strict bool) (*File, error) {
// Unfortunately we need a new context. See the note inside [moduleSchemaDo].
ctx := cuecontext.New()
Expand Down
59 changes: 59 additions & 0 deletions mod/modfile/modfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,65 @@ custom: "somewhere.com": foo: true
wantDefaults: map[string]string{
"foo.com/bar": "v0",
},
}, {
testName: "FixLegacyWithModulePath",
parse: FixLegacy,
data: `
module: "foo.com/bar"
`,
want: &File{
Module: "foo.com/bar@v0",
Language: &Language{Version: "v0.9.0"},
},
wantDefaults: map[string]string{
"foo.com/bar": "v0",
},
}, {
testName: "FixLegacyWithoutModulePath",
parse: FixLegacy,
data: `
`,
want: &File{
Module: "test.example@v0",
Language: &Language{Version: "v0.9.0"},
},
wantDefaults: map[string]string{
"test.example": "v0",
},
}, {
testName: "FixLegacyWithEmptyModulePath",
parse: FixLegacy,
data: `
module: ""
`,
want: &File{
Module: "test.example@v0",
Language: &Language{Version: "v0.9.0"},
},
wantDefaults: map[string]string{
"test.example": "v0",
},
}, {
testName: "FixLegacyWithCustomFields",
parse: FixLegacy,
data: `
module: "foo.com"
some: true
other: field: 123
`,
want: &File{
Module: "foo.com@v0",
Language: &Language{Version: "v0.9.0"},
Custom: map[string]map[string]any{
"legacy": {
"some": true,
"other": map[string]any{"field": 123},
},
},
},
wantDefaults: map[string]string{
"foo.com": "v0",
},
}}

func TestParse(t *testing.T) {
Expand Down

0 comments on commit 47e0097

Please sign in to comment.