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.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package storage
  4. import (
  5. "fmt"
  6. "io"
  7. "net/url"
  8. "os"
  9. "reflect"
  10. "code.gitea.io/gitea/modules/json"
  11. )
  12. // Mappable represents an interface that can MapTo another interface
  13. type Mappable interface {
  14. MapTo(v interface{}) error
  15. }
  16. // toConfig will attempt to convert a given configuration cfg into the provided exemplar type.
  17. //
  18. // It will tolerate the cfg being passed as a []byte or string of a json representation of the
  19. // exemplar or the correct type of the exemplar itself
  20. func toConfig(exemplar, cfg interface{}) (interface{}, error) {
  21. // First of all check if we've got the same type as the exemplar - if so it's all fine.
  22. if reflect.TypeOf(cfg).AssignableTo(reflect.TypeOf(exemplar)) {
  23. return cfg, nil
  24. }
  25. // Now if not - does it provide a MapTo function we can try?
  26. if mappable, ok := cfg.(Mappable); ok {
  27. newVal := reflect.New(reflect.TypeOf(exemplar))
  28. if err := mappable.MapTo(newVal.Interface()); err == nil {
  29. return newVal.Elem().Interface(), nil
  30. }
  31. // MapTo has failed us ... let's try the json route ...
  32. }
  33. // OK we've been passed a byte array right?
  34. configBytes, ok := cfg.([]byte)
  35. if !ok {
  36. // oh ... it's a string then?
  37. var configStr string
  38. configStr, ok = cfg.(string)
  39. configBytes = []byte(configStr)
  40. }
  41. if !ok {
  42. // hmm ... can we marshal it to json?
  43. var err error
  44. configBytes, err = json.Marshal(cfg)
  45. ok = err == nil
  46. }
  47. if !ok {
  48. // no ... we've tried hard enough at this point - throw an error!
  49. return nil, ErrInvalidConfiguration{cfg: cfg}
  50. }
  51. // OK unmarshal the byte array into a new copy of the exemplar
  52. newVal := reflect.New(reflect.TypeOf(exemplar))
  53. if err := json.Unmarshal(configBytes, newVal.Interface()); err != nil {
  54. // If we can't unmarshal it then return an error!
  55. return nil, ErrInvalidConfiguration{cfg: cfg, err: err}
  56. }
  57. return newVal.Elem().Interface(), nil
  58. }
  59. var uninitializedStorage = discardStorage("uninitialized storage")
  60. type discardStorage string
  61. func (s discardStorage) Open(_ string) (Object, error) {
  62. return nil, fmt.Errorf("%s", s)
  63. }
  64. func (s discardStorage) Save(_ string, _ io.Reader, _ int64) (int64, error) {
  65. return 0, fmt.Errorf("%s", s)
  66. }
  67. func (s discardStorage) Stat(_ string) (os.FileInfo, error) {
  68. return nil, fmt.Errorf("%s", s)
  69. }
  70. func (s discardStorage) Delete(_ string) error {
  71. return fmt.Errorf("%s", s)
  72. }
  73. func (s discardStorage) URL(_, _ string) (*url.URL, error) {
  74. return nil, fmt.Errorf("%s", s)
  75. }
  76. func (s discardStorage) IterateObjects(_ func(string, Object) error) error {
  77. return fmt.Errorf("%s", s)
  78. }