package data import ( "errors" "regexp" ) type SourceType struct { Name string Regex regexp.Regexp } var SourceTypes = []SourceType{ {Name: "Youtube", Regex: *regexp.MustCompile(`(?i)\b(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})\b`)}, } func GetSourceType(name string) *SourceType { for i, st := range SourceTypes { if st.Name == name { return &SourceTypes[i] } } panic("couldn't find source type for " + name) } type Source struct { Url string Type *SourceType } func (st *SourceType) NewSource(url string) (Source, error) { var err error = nil if !st.Regex.MatchString(url) { err = errors.New("url " + url + " didn't match source " + st.Name) } return Source{ Url: url, Type: st, }, err } func NewSource(url string) (Source, error) { var st *SourceType = nil for i, source := range SourceTypes { if source.Regex.MatchString(url) { st = &SourceTypes[i] break } } if st == nil { return Source{}, errors.New("couldn't find a source type for the url " + url) } return Source{ Url: url, Type: st, }, nil }