code.gitea.io/gitea@v1.22.3/modules/templates/util_slice.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package templates 5 6 import ( 7 "fmt" 8 "reflect" 9 ) 10 11 type SliceUtils struct{} 12 13 func NewSliceUtils() *SliceUtils { 14 return &SliceUtils{} 15 } 16 17 func (su *SliceUtils) Contains(s, v any) bool { 18 if s == nil { 19 return false 20 } 21 sv := reflect.ValueOf(s) 22 if sv.Kind() != reflect.Slice && sv.Kind() != reflect.Array { 23 panic(fmt.Sprintf("invalid type, expected slice or array, but got: %T", s)) 24 } 25 for i := 0; i < sv.Len(); i++ { 26 it := sv.Index(i) 27 if !it.CanInterface() { 28 continue 29 } 30 if it.Interface() == v { 31 return true 32 } 33 } 34 return false 35 }