You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util_slice.go 637B

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