github.com/dannyzhou2015/migrate/v4@v4.15.2/source/parse.go (about) 1 package source 2 3 import ( 4 "fmt" 5 "regexp" 6 "strconv" 7 ) 8 9 var ( 10 ErrParse = fmt.Errorf("no match") 11 ) 12 13 var ( 14 DefaultParse = Parse 15 DefaultRegex = Regex 16 ) 17 18 // Regex matches the following pattern: 19 // 123_name.up.ext 20 // 123_name.down.ext 21 var Regex = regexp.MustCompile(`^([0-9]+)_(.*)\.(` + string(Down) + `|` + string(Up) + `)\.(.*)$`) 22 23 // Parse returns Migration for matching Regex pattern. 24 func Parse(raw string) (*Migration, error) { 25 m := Regex.FindStringSubmatch(raw) 26 if len(m) == 5 { 27 versionUint64, err := strconv.ParseUint(m[1], 10, 64) 28 if err != nil { 29 return nil, err 30 } 31 return &Migration{ 32 Version: uint(versionUint64), 33 Identifier: m[2], 34 Direction: Direction(m[3]), 35 Raw: raw, 36 }, nil 37 } 38 return nil, ErrParse 39 }