ambiguous.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package reflectutils
  15. import (
  16. "fmt"
  17. )
  18. const (
  19. TAG_AMBIGUOUS_PREFIX = "yunion-ambiguous-prefix"
  20. TAG_DEPRECATED_BY = "yunion-deprecated-by"
  21. TAG_OLD_DEPRECATED_BY = "deprecated-by"
  22. )
  23. func expandAmbiguousPrefix(fields SStructFieldValueSet) SStructFieldValueSet {
  24. keyIndexMap := make(map[string][]int)
  25. for i := range fields {
  26. if fields[i].Info.Ignore {
  27. continue
  28. }
  29. key := fields[i].Info.MarshalName()
  30. values, ok := keyIndexMap[key]
  31. if !ok {
  32. values = make([]int, 0, 2)
  33. }
  34. keyIndexMap[key] = append(values, i)
  35. }
  36. for _, indexes := range keyIndexMap {
  37. if len(indexes) > 1 {
  38. // ambiguous found
  39. for _, idx := range indexes {
  40. if amPrefix, ok := fields[idx].Info.Tags[TAG_AMBIGUOUS_PREFIX]; ok {
  41. fields[idx].Info.Name = fmt.Sprintf("%s%s", amPrefix, fields[idx].Info.Name)
  42. if depBy, ok := fields[idx].Info.Tags[TAG_DEPRECATED_BY]; ok {
  43. fields[idx].Info.Tags[TAG_DEPRECATED_BY] = fmt.Sprintf("%s%s", amPrefix, depBy)
  44. }
  45. if depBy, ok := fields[idx].Info.Tags[TAG_OLD_DEPRECATED_BY]; ok {
  46. fields[idx].Info.Tags[TAG_OLD_DEPRECATED_BY] = fmt.Sprintf("%s%s", amPrefix, depBy)
  47. }
  48. }
  49. }
  50. }
  51. }
  52. return fields
  53. }