Skip to content

Instantly share code, notes, and snippets.

@viiftw
Forked from PumpkinSeed/contains.go
Created February 25, 2022 04:45
Show Gist options
  • Save viiftw/1a1beea8dcefbd81ba67fd615237859d to your computer and use it in GitHub Desktop.
Save viiftw/1a1beea8dcefbd81ba67fd615237859d to your computer and use it in GitHub Desktop.

Revisions

  1. @PumpkinSeed PumpkinSeed created this gist Sep 22, 2017.
    127 changes: 127 additions & 0 deletions contains.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,127 @@
    func Contains(s, value interface{}) bool {
    v := reflect.ValueOf(s)
    for i := 0; i < v.NumField(); i++ {
    field := v.Field(i)
    typeOfField := field.Type()
    switch typeOfField {
    case stringType:
    if containsString(field.String(), value) {
    return true
    }
    case intType, int8Type, int16Type, int32Type, int64Type:
    if containsInt(field.Int(), value) {
    return true
    }
    case boolType:
    if containsBool(field.Bool(), value) {
    return true
    }
    case float32Type:
    if containsFloat32(field.Interface().(float32), value) {
    return true
    }
    case float64Type:
    if containsFloat64(field.Interface().(float64), value) {
    return true
    }
    case complex64Type:
    if containsComplex64(field.Interface().(complex64), value) {
    return true
    }
    case complex128Type:
    if containsComplex128(field.Interface().(complex128), value) {
    return true
    }
    }
    }
    return false
    }

    /*
    Contains helper functions
    */
    func containsString(s string, v interface{}) bool {
    switch v.(type) {
    case string:
    if v.(string) == s {
    return true
    }
    }
    return false
    }

    func containsInt(s int64, v interface{}) bool {
    switch v.(type) {
    case int64:
    if v.(int64) == s {
    return true
    }
    case int32:
    if int64(v.(int32)) == s {
    return true
    }
    case int16:
    if int64(v.(int16)) == s {
    return true
    }
    case int8:
    if int64(v.(int8)) == s {
    return true
    }
    case int:
    if int64(v.(int)) == s {
    return true
    }
    }
    return false
    }

    func containsBool(s bool, v interface{}) bool {
    switch v.(type) {
    case bool:
    if v.(bool) == s {
    return true
    }
    }
    return false
    }

    func containsFloat32(s float32, v interface{}) bool {
    switch v.(type) {
    case float32:
    if v.(float32) == s {
    return true
    }
    }
    return false
    }

    func containsFloat64(s float64, v interface{}) bool {
    switch v.(type) {
    case float64:
    if v.(float64) == s {
    return true
    }
    }
    return false
    }

    func containsComplex64(s complex64, v interface{}) bool {
    switch v.(type) {
    case complex64:
    if v.(complex64) == s {
    return true
    }
    }
    return false
    }

    func containsComplex128(s complex128, v interface{}) bool {
    switch v.(type) {
    case complex128:
    if v.(complex128) == s {
    return true
    }
    }
    return false
    }