Skip to content

Instantly share code, notes, and snippets.

@BransonGitomeh
Created March 16, 2018 17:08
Show Gist options
  • Save BransonGitomeh/e42640e167256afef06cb33a4394a882 to your computer and use it in GitHub Desktop.
Save BransonGitomeh/e42640e167256afef06cb33a4394a882 to your computer and use it in GitHub Desktop.
func (r *Router) convertToPatternSet(p interface{}) *PatternSet {
fields := structs.Fields(p)
ps := &PatternSet{}
ps.Fields = make(PatternFields)
ps.Pattern = p
ps.Weight = 0
for _, field := range fields {
if !strings.HasSuffix(field.Name(), "_") && !field.IsZero() {
fieldKind := field.Kind()
switch fieldKind {
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
fallthrough
case reflect.Int:
fallthrough
case reflect.String:
fallthrough
case reflect.Bool:
fallthrough
case reflect.Float32:
fallthrough
case reflect.Float64:
ps.Fields[field.Name()] = field.Value()
ps.Weight++
}
}
}
// sort by insertion order
if !r.IsDeep {
r.insertCount++
ps.Weight = r.insertCount
}
return ps
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment