-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathiteration.go
More file actions
53 lines (45 loc) · 1.22 KB
/
iteration.go
File metadata and controls
53 lines (45 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package api
import (
"fmt"
"path/filepath"
"strings"
)
const (
msgUnidentifiable = "unable to identify track and problem"
)
// Iteration represents a version of a particular exercise.
// This gets submitted to the API.
type Iteration struct {
Key string `json:"key"`
Code string `json:"code"`
Path string `json:"path"`
Dir string `json:"dir"`
File string `json:"-"`
Language string `json:"-"`
Problem string `json:"-"`
}
// RelativePath returns the path relative to the exercism dir.
func (iter *Iteration) RelativePath() string {
if iter.Path != "" {
return iter.Path
}
if len(iter.Dir) > len(iter.File) {
return ""
}
iter.Path = iter.File[len(iter.Dir):]
return iter.Path
}
// Identify attempts to determine the track and problem of an iteration.
func (iter *Iteration) Identify() error {
if !strings.HasPrefix(strings.ToLower(iter.File), strings.ToLower(iter.Dir)) {
return fmt.Errorf(msgUnidentifiable)
}
segments := strings.Split(iter.RelativePath(), string(filepath.Separator))
// file is always the absolute path, so the first segment will be empty
if len(segments) < 4 {
return fmt.Errorf(msgUnidentifiable)
}
iter.Language = segments[1]
iter.Problem = segments[2]
return nil
}