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.

helper.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "encoding/binary"
  7. "code.gitea.io/gitea/modules/json"
  8. )
  9. func keysInt64(m map[int64]struct{}) []int64 {
  10. keys := make([]int64, 0, len(m))
  11. for k := range m {
  12. keys = append(keys, k)
  13. }
  14. return keys
  15. }
  16. func valuesRepository(m map[int64]*Repository) []*Repository {
  17. values := make([]*Repository, 0, len(m))
  18. for _, v := range m {
  19. values = append(values, v)
  20. }
  21. return values
  22. }
  23. func valuesUser(m map[int64]*User) []*User {
  24. values := make([]*User, 0, len(m))
  25. for _, v := range m {
  26. values = append(values, v)
  27. }
  28. return values
  29. }
  30. // JSONUnmarshalHandleDoubleEncode - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
  31. // possible that a Blob may be double encoded or gain an unwanted prefix of 0xff 0xfe.
  32. func JSONUnmarshalHandleDoubleEncode(bs []byte, v interface{}) error {
  33. err := json.Unmarshal(bs, v)
  34. if err != nil {
  35. ok := true
  36. rs := []byte{}
  37. temp := make([]byte, 2)
  38. for _, rn := range string(bs) {
  39. if rn > 0xffff {
  40. ok = false
  41. break
  42. }
  43. binary.LittleEndian.PutUint16(temp, uint16(rn))
  44. rs = append(rs, temp...)
  45. }
  46. if ok {
  47. if rs[0] == 0xff && rs[1] == 0xfe {
  48. rs = rs[2:]
  49. }
  50. err = json.Unmarshal(rs, v)
  51. }
  52. }
  53. if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
  54. err = json.Unmarshal(bs[2:], v)
  55. }
  56. return err
  57. }