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 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2020 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 queue
  5. import (
  6. "reflect"
  7. jsoniter "github.com/json-iterator/go"
  8. )
  9. // Mappable represents an interface that can MapTo another interface
  10. type Mappable interface {
  11. MapTo(v interface{}) error
  12. }
  13. // toConfig will attempt to convert a given configuration cfg into the provided exemplar type.
  14. //
  15. // It will tolerate the cfg being passed as a []byte or string of a json representation of the
  16. // exemplar or the correct type of the exemplar itself
  17. func toConfig(exemplar, cfg interface{}) (interface{}, error) {
  18. json := jsoniter.ConfigCompatibleWithStandardLibrary
  19. // First of all check if we've got the same type as the exemplar - if so it's all fine.
  20. if reflect.TypeOf(cfg).AssignableTo(reflect.TypeOf(exemplar)) {
  21. return cfg, nil
  22. }
  23. // Now if not - does it provide a MapTo function we can try?
  24. if mappable, ok := cfg.(Mappable); ok {
  25. newVal := reflect.New(reflect.TypeOf(exemplar))
  26. if err := mappable.MapTo(newVal.Interface()); err == nil {
  27. return newVal.Elem().Interface(), nil
  28. }
  29. // MapTo has failed us ... let's try the json route ...
  30. }
  31. // OK we've been passed a byte array right?
  32. configBytes, ok := cfg.([]byte)
  33. if !ok {
  34. // oh ... it's a string then?
  35. var configStr string
  36. configStr, ok = cfg.(string)
  37. configBytes = []byte(configStr)
  38. }
  39. if !ok {
  40. // hmm ... can we marshal it to json?
  41. var err error
  42. configBytes, err = json.Marshal(cfg)
  43. ok = err == nil
  44. }
  45. if !ok {
  46. // no ... we've tried hard enough at this point - throw an error!
  47. return nil, ErrInvalidConfiguration{cfg: cfg}
  48. }
  49. // OK unmarshal the byte array into a new copy of the exemplar
  50. newVal := reflect.New(reflect.TypeOf(exemplar))
  51. if err := json.Unmarshal(configBytes, newVal.Interface()); err != nil {
  52. // If we can't unmarshal it then return an error!
  53. return nil, ErrInvalidConfiguration{cfg: cfg, err: err}
  54. }
  55. return newVal.Elem().Interface(), nil
  56. }
  57. // unmarshalAs will attempt to unmarshal provided bytes as the provided exemplar
  58. func unmarshalAs(bs []byte, exemplar interface{}) (data Data, err error) {
  59. json := jsoniter.ConfigCompatibleWithStandardLibrary
  60. if exemplar != nil {
  61. t := reflect.TypeOf(exemplar)
  62. n := reflect.New(t)
  63. ne := n.Elem()
  64. err = json.Unmarshal(bs, ne.Addr().Interface())
  65. data = ne.Interface().(Data)
  66. } else {
  67. err = json.Unmarshal(bs, &data)
  68. }
  69. return
  70. }
  71. // assignableTo will check if provided data is assignable to the same type as the exemplar
  72. // if the provided exemplar is nil then it will always return true
  73. func assignableTo(data Data, exemplar interface{}) bool {
  74. if exemplar == nil {
  75. return true
  76. }
  77. // Assert data is of same type as exemplar
  78. t := reflect.TypeOf(data)
  79. exemplarType := reflect.TypeOf(exemplar)
  80. return t.AssignableTo(exemplarType) && data != nil
  81. }