Skip to content

Commit 3e9d5fe

Browse files
author
Katrina Owen
committed
Rename Exercise field on metadata type
We are going to want to move some behavior onto the ExerciseMetadata type, and the naming is going to crash. ExerciseSlug, while more verbose, is also more correct.
1 parent b649e75 commit 3e9d5fe

File tree

6 files changed

+78
-78
lines changed

6 files changed

+78
-78
lines changed

cmd/download.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,14 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
130130
}
131131

132132
metadata := workspace.ExerciseMetadata{
133-
AutoApprove: payload.Solution.Exercise.AutoApprove,
134-
Track: payload.Solution.Exercise.Track.ID,
135-
Team: payload.Solution.Team.Slug,
136-
Exercise: payload.Solution.Exercise.ID,
137-
ID: payload.Solution.ID,
138-
URL: payload.Solution.URL,
139-
Handle: payload.Solution.User.Handle,
140-
IsRequester: payload.Solution.User.IsRequester,
133+
AutoApprove: payload.Solution.Exercise.AutoApprove,
134+
Track: payload.Solution.Exercise.Track.ID,
135+
Team: payload.Solution.Team.Slug,
136+
ExerciseSlug: payload.Solution.Exercise.ID,
137+
ID: payload.Solution.ID,
138+
URL: payload.Solution.URL,
139+
Handle: payload.Solution.User.Handle,
140+
IsRequester: payload.Solution.User.IsRequester,
141141
}
142142

143143
root := usrCfg.GetString("workspace")
@@ -151,7 +151,7 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
151151
exercise := workspace.Exercise{
152152
Root: root,
153153
Track: metadata.Track,
154-
Slug: metadata.Exercise,
154+
Slug: metadata.ExerciseSlug,
155155
}
156156

157157
dir := exercise.MetadataDir()
@@ -201,7 +201,7 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
201201
// Work around a path bug due to an early design decision (later reversed) to
202202
// allow numeric suffixes for exercise directories, allowing people to have
203203
// multiple parallel versions of an exercise.
204-
pattern := fmt.Sprintf(`\A.*[/\\]%s-\d*/`, metadata.Exercise)
204+
pattern := fmt.Sprintf(`\A.*[/\\]%s-\d*/`, metadata.ExerciseSlug)
205205
rgxNumericSuffix := regexp.MustCompile(pattern)
206206
if rgxNumericSuffix.MatchString(file) {
207207
file = string(rgxNumericSuffix.ReplaceAll([]byte(file), []byte("")))

cmd/download_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func TestDownload(t *testing.T) {
144144
assert.NoError(t, err)
145145

146146
assert.Equal(t, "bogus-track", metadata.Track)
147-
assert.Equal(t, "bogus-exercise", metadata.Exercise)
147+
assert.Equal(t, "bogus-exercise", metadata.ExerciseSlug)
148148
assert.Equal(t, tc.requester, metadata.IsRequester)
149149
}
150150
}

cmd/submit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ func (s submitValidator) submissionNotEmpty(docs []workspace.Document) error {
389389

390390
// metadataMatchesExercise checks that the metadata refers to the exercise being submitted.
391391
func (s submitValidator) metadataMatchesExercise(metadata *workspace.ExerciseMetadata, exercise workspace.Exercise) error {
392-
if metadata.Exercise != exercise.Slug {
392+
if metadata.ExerciseSlug != exercise.Slug {
393393
// TODO: error msg should suggest running future doctor command
394394
msg := `
395395
@@ -400,7 +400,7 @@ func (s submitValidator) metadataMatchesExercise(metadata *workspace.ExerciseMet
400400
Please rename the directory '%[1]s' to '%[2]s' and try again.
401401
402402
`
403-
return fmt.Errorf(msg, exercise.Slug, metadata.Exercise)
403+
return fmt.Errorf(msg, exercise.Slug, metadata.ExerciseSlug)
404404
}
405405
return nil
406406
}
@@ -416,7 +416,7 @@ func (s submitValidator) isRequestor(metadata *workspace.ExerciseMetadata) error
416416
%s download --exercise=%s --track=%s
417417
418418
`
419-
return fmt.Errorf(msg, BinaryName, metadata.Exercise, metadata.Track)
419+
return fmt.Errorf(msg, BinaryName, metadata.ExerciseSlug, metadata.Track)
420420
}
421421
return nil
422422
}

cmd/submit_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,11 @@ func TestLegacyMetadataMigration(t *testing.T) {
218218
os.MkdirAll(dir, os.FileMode(0755))
219219

220220
metadata := &workspace.ExerciseMetadata{
221-
ID: "bogus-solution-uuid",
222-
Track: "bogus-track",
223-
Exercise: "bogus-exercise",
224-
URL: "http://example.com/bogus-url",
225-
IsRequester: true,
221+
ID: "bogus-solution-uuid",
222+
Track: "bogus-track",
223+
ExerciseSlug: "bogus-exercise",
224+
URL: "http://example.com/bogus-url",
225+
IsRequester: true,
226226
}
227227
b, err := json.Marshal(metadata)
228228
assert.NoError(t, err)
@@ -585,11 +585,11 @@ func TestSubmissionNotConnectedToRequesterAccount(t *testing.T) {
585585
os.MkdirAll(filepath.Join(dir, "subdir"), os.FileMode(0755))
586586

587587
metadata := &workspace.ExerciseMetadata{
588-
ID: "bogus-solution-uuid",
589-
Track: "bogus-track",
590-
Exercise: "bogus-exercise",
591-
URL: "http://example.com/bogus-url",
592-
IsRequester: false,
588+
ID: "bogus-solution-uuid",
589+
Track: "bogus-track",
590+
ExerciseSlug: "bogus-exercise",
591+
URL: "http://example.com/bogus-url",
592+
IsRequester: false,
593593
}
594594
err = metadata.Write(dir)
595595
assert.NoError(t, err)
@@ -651,11 +651,11 @@ func TestExerciseDirnameMatchesMetadataSlug(t *testing.T) {
651651

652652
func writeFakeMetadata(t *testing.T, dir, trackID, exerciseSlug string) {
653653
metadata := &workspace.ExerciseMetadata{
654-
ID: "bogus-solution-uuid",
655-
Track: trackID,
656-
Exercise: exerciseSlug,
657-
URL: "http://example.com/bogus-url",
658-
IsRequester: true,
654+
ID: "bogus-solution-uuid",
655+
Track: trackID,
656+
ExerciseSlug: exerciseSlug,
657+
URL: "http://example.com/bogus-url",
658+
IsRequester: true,
659659
}
660660
err := metadata.Write(dir)
661661
assert.NoError(t, err)

workspace/exercise_metadata.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ var metadataFilepath = filepath.Join(ignoreSubdir, metadataFilename)
1818

1919
// ExerciseMetadata contains metadata about a user's exercise.
2020
type ExerciseMetadata struct {
21-
Track string `json:"track"`
22-
Exercise string `json:"exercise"`
23-
ID string `json:"id"`
24-
Team string `json:"team,omitempty"`
25-
URL string `json:"url"`
26-
Handle string `json:"handle"`
27-
IsRequester bool `json:"is_requester"`
28-
SubmittedAt *time.Time `json:"submitted_at,omitempty"`
29-
Dir string `json:"-"`
30-
AutoApprove bool `json:"auto_approve"`
21+
Track string `json:"track"`
22+
ExerciseSlug string `json:"exercise"`
23+
ID string `json:"id"`
24+
Team string `json:"team,omitempty"`
25+
URL string `json:"url"`
26+
Handle string `json:"handle"`
27+
IsRequester bool `json:"is_requester"`
28+
SubmittedAt *time.Time `json:"submitted_at,omitempty"`
29+
Dir string `json:"-"`
30+
AutoApprove bool `json:"auto_approve"`
3131
}
3232

3333
// NewExerciseMetadata reads exercise metadata from a file in the given directory.
@@ -48,11 +48,11 @@ func NewExerciseMetadata(dir string) (*ExerciseMetadata, error) {
4848
// This is appended to avoid name conflicts, and does not indicate a particular
4949
// iteration.
5050
func (em *ExerciseMetadata) Suffix() string {
51-
return strings.Trim(strings.Replace(filepath.Base(em.Dir), em.Exercise, "", 1), "-.")
51+
return strings.Trim(strings.Replace(filepath.Base(em.Dir), em.ExerciseSlug, "", 1), "-.")
5252
}
5353

5454
func (em *ExerciseMetadata) String() string {
55-
str := fmt.Sprintf("%s/%s", em.Track, em.Exercise)
55+
str := fmt.Sprintf("%s/%s", em.Track, em.ExerciseSlug)
5656
if em.Suffix() != "" {
5757
str = fmt.Sprintf("%s (%s)", str, em.Suffix())
5858
}

workspace/exercise_metadata_test.go

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ func TestExerciseMetadata(t *testing.T) {
1515
defer os.RemoveAll(dir)
1616

1717
em1 := &ExerciseMetadata{
18-
Track: "a-track",
19-
Exercise: "bogus-exercise",
20-
ID: "abc",
21-
URL: "http://example.com",
22-
Handle: "alice",
23-
IsRequester: true,
24-
Dir: dir,
18+
Track: "a-track",
19+
ExerciseSlug: "bogus-exercise",
20+
ID: "abc",
21+
URL: "http://example.com",
22+
Handle: "alice",
23+
IsRequester: true,
24+
Dir: dir,
2525
}
2626
err = em1.Write(dir)
2727
assert.NoError(t, err)
@@ -49,29 +49,29 @@ func TestSuffix(t *testing.T) {
4949
}{
5050
{
5151
metadata: ExerciseMetadata{
52-
Exercise: "bat",
53-
Dir: "",
52+
ExerciseSlug: "bat",
53+
Dir: "",
5454
},
5555
suffix: "",
5656
},
5757
{
5858
metadata: ExerciseMetadata{
59-
Exercise: "bat",
60-
Dir: "/path/to/bat",
59+
ExerciseSlug: "bat",
60+
Dir: "/path/to/bat",
6161
},
6262
suffix: "",
6363
},
6464
{
6565
metadata: ExerciseMetadata{
66-
Exercise: "bat",
67-
Dir: "/path/to/bat-2",
66+
ExerciseSlug: "bat",
67+
Dir: "/path/to/bat-2",
6868
},
6969
suffix: "2",
7070
},
7171
{
7272
metadata: ExerciseMetadata{
73-
Exercise: "bat",
74-
Dir: "/path/to/bat-200",
73+
ExerciseSlug: "bat",
74+
Dir: "/path/to/bat-200",
7575
},
7676
suffix: "200",
7777
},
@@ -92,48 +92,48 @@ func TestExerciseMetadataString(t *testing.T) {
9292
}{
9393
{
9494
metadata: ExerciseMetadata{
95-
Track: "elixir",
96-
Exercise: "secret-handshake",
97-
Handle: "",
98-
Dir: "",
95+
Track: "elixir",
96+
ExerciseSlug: "secret-handshake",
97+
Handle: "",
98+
Dir: "",
9999
},
100100
desc: "elixir/secret-handshake",
101101
},
102102
{
103103
metadata: ExerciseMetadata{
104-
Track: "cpp",
105-
Exercise: "clock",
106-
Handle: "alice",
107-
IsRequester: true,
104+
Track: "cpp",
105+
ExerciseSlug: "clock",
106+
Handle: "alice",
107+
IsRequester: true,
108108
},
109109
desc: "cpp/clock",
110110
},
111111
{
112112
metadata: ExerciseMetadata{
113-
Track: "cpp",
114-
Exercise: "clock",
115-
Handle: "alice",
116-
IsRequester: true,
117-
Dir: "/path/to/clock-2",
113+
Track: "cpp",
114+
ExerciseSlug: "clock",
115+
Handle: "alice",
116+
IsRequester: true,
117+
Dir: "/path/to/clock-2",
118118
},
119119
desc: "cpp/clock (2)",
120120
},
121121
{
122122
metadata: ExerciseMetadata{
123-
Track: "fsharp",
124-
Exercise: "hello-world",
125-
Handle: "bob",
126-
IsRequester: false,
123+
Track: "fsharp",
124+
ExerciseSlug: "hello-world",
125+
Handle: "bob",
126+
IsRequester: false,
127127
},
128128
desc: "fsharp/hello-world by @bob",
129129
},
130130
{
131131
metadata: ExerciseMetadata{
132-
Track: "haskell",
133-
Exercise: "allergies",
134-
Handle: "charlie",
135-
IsRequester: false,
136-
Dir: "/path/to/allergies-2",
132+
Track: "haskell",
133+
ExerciseSlug: "allergies",
134+
Handle: "charlie",
135+
IsRequester: false,
136+
Dir: "/path/to/allergies-2",
137137
},
138138
desc: "haskell/allergies (2) by @charlie",
139139
},

0 commit comments

Comments
 (0)