package parsers import ( "regexp" "strconv" ) var ( vipGroupsRE = regexp.MustCompile(`(?i)(|s)erver\s+vip-group\s+([0-9]+)((.|\n)*?)(\n\S|!)`) vipsRE = regexp.MustCompile(`(?i)vip\s+(\S+)`) ) type ( // VipGroups type defines a listing of vip-groups VipGroups struct { VipGroups []VipGroup `json:"vip-groups"` } // VipGroup type describes all the attributes on a single vip-group VipGroup struct { GroupNumber int `json:"vip_group_number"` VIPs []string `json:"vips"` } ) // Parse method is used to implement type population of a given string // using parsers to populate the method receiver. func (v *VipGroups) Parse(s *string) { for _, vg := range vipGroupsRE.FindAllStringSubmatch(*s, -1) { v.VipGroups = append(v.VipGroups, VipGroup{ GroupNumber: func(s string) int { i, err := strconv.Atoi(s) if err != nil { return 0 } return i }(vg[2]), VIPs: func(s *string) (vips []string) { for _, vip := range vipsRE.FindAllStringSubmatch(*s, -1) { vips = append(vips, vip[1]) } return }(&vg[3]), }) } }