Skip to content

Commit

Permalink
mod/modfile: add support for "source" field
Browse files Browse the repository at this point in the history
This adds experimental support for the source field
as described in this proposal:
https://github.com/cue-lang/proposal/blob/main/designs/modules.v3/3017-module-files.md

For #3017.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: If6d440be9b63c33800b41881b9c87f5a00a5d17f
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1192905
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
rogpeppe committed Apr 18, 2024
1 parent 4c7fe24 commit 8d043e7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mod/modfile/modfile.go
Expand Up @@ -45,13 +45,20 @@ var moduleSchemaData []byte
type File struct {
Module string `json:"module"`
Language *Language `json:"language,omitempty"`
Source *Source `json:"source,omitempty"`
Deps map[string]*Dep `json:"deps,omitempty"`
versions []module.Version
// defaultMajorVersions maps from module base path to the
// major version default for that path.
defaultMajorVersions map[string]string
}

// Source represents how to transform from a module's
// source to its actual contents.
type Source struct {
Kind string `json:"kind"`
}

// Format returns a formatted representation of f
// in CUE syntax.
func (f *File) Format() ([]byte, error) {
Expand Down
57 changes: 57 additions & 0 deletions mod/modfile/modfile_test.go
Expand Up @@ -78,6 +78,63 @@ deps: "other.com/something@v0": v: "v0.2.3"
"foo.com/bar": "v0",
"example.com": "v1",
},
}, {
testName: "WithSource",
parse: Parse,
data: `
language: version: "v0.4.3"
module: "foo.com/bar@v0"
source: kind: "git"
`,
want: &File{
Language: &Language{
Version: "v0.4.3",
},
Module: "foo.com/bar@v0",
Source: &Source{
Kind: "git",
},
},
wantDefaults: map[string]string{
"foo.com/bar": "v0",
},
}, {
testName: "WithExplicitSource",
parse: Parse,
data: `
language: version: "v0.4.3"
module: "foo.com/bar@v0"
source: kind: "self"
`,
want: &File{
Language: &Language{
Version: "v0.4.3",
},
Module: "foo.com/bar@v0",
Source: &Source{
Kind: "self",
},
},
wantDefaults: map[string]string{
"foo.com/bar": "v0",
},
}, {
testName: "WithUnknownSourceKind",
parse: Parse,
data: `
language: version: "v0.4.3"
module: "foo.com/bar@v0"
source: kind: "bad"
`,
wantError: `source.kind: 2 errors in empty disjunction:
source.kind: conflicting values "git" and "bad":
cuelang.org/go/mod/modfile/schema.cue:45:11
cuelang.org/go/mod/modfile/schema.cue:166:18
module.cue:4:15
source.kind: conflicting values "self" and "bad":
cuelang.org/go/mod/modfile/schema.cue:45:11
cuelang.org/go/mod/modfile/schema.cue:166:9
module.cue:4:15`,
}, {
testName: "AmbiguousDefaults",
parse: Parse,
Expand Down
17 changes: 17 additions & 0 deletions mod/modfile/schema.cue
Expand Up @@ -40,6 +40,10 @@ let unimplemented = 1&2
// module indicates the module's path.
module?: #Module | ""

// source holds information about the source of the files within the
// module. This field is mandatory at publish time.
source?: #Source

// version indicates the language version used by the code
// in this module - the minimum version of CUE required
// to evaluate the code in this module. When a later version of CUE
Expand Down Expand Up @@ -151,3 +155,16 @@ let unimplemented = 1&2
// TODO require the CUE version?
// language!: version!: _
}

// #Source describes a source of truth for a module's content.
#Source: {
// kind specifies the kind of source.
//
// The special value "none" signifies that the module that is
// standalone, associated with no particular source other than
// the contents of the module itself.
kind!: "self" | "git"

// TODO support for other VCSs:
// kind!: "self" | "git" | "bzr" | "hg" | "svn"
}

0 comments on commit 8d043e7

Please sign in to comment.