fieldmapper.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package source
  2. import (
  3. "fmt"
  4. "maps"
  5. "github.com/opencost/opencost/core/pkg/util/sliceutil"
  6. )
  7. // A FieldMapper maps the source name for the field to a corresponding set of
  8. // potential labels representing that source field. It also maintains a reverse mapping
  9. // which allows finding a source field by a label.
  10. //
  11. // By design, there are NO overlapping labels permitted between source fields and labels. That is,
  12. // labels for a specific field cannot overlap with labels for another field.
  13. type FieldMapper interface {
  14. // LabelsFor returns all of the labels associated with that specific field.
  15. LabelsFor(field string) []string
  16. // FieldFor returns the field the provided label is associated with
  17. FieldFor(label string) string
  18. // Resolve attempts to find all labels associated with a given value. If the value
  19. // is a field, then all of the labels associated with that field are returned. If
  20. // the value is a label, then all the labels (including the provided value) are
  21. // returned. Lastly, if neither of the previous conditions are true, an error is
  22. // returned.
  23. Resolve(value string) ([]string, error)
  24. }
  25. // NoOpFieldMapper is a no-op implementation of the FieldMapper interface, which provides a direct
  26. // passthrough of lookups. Useful when all field names are deterministic!
  27. type NoOpFieldMapper struct{}
  28. // NewNoOpFieldMapper creates a new NoOpFieldMapper instance for use with query results where the
  29. // field names are deterministic.
  30. func NewNoOpFieldMapper() *NoOpFieldMapper {
  31. return new(NoOpFieldMapper)
  32. }
  33. // LabelsFor returns all of the labels associated with that specific field.
  34. func (nofm *NoOpFieldMapper) LabelsFor(field string) []string {
  35. return []string{field}
  36. }
  37. // FieldFor returns the field the provided label is associated with
  38. func (nofm *NoOpFieldMapper) FieldFor(label string) string {
  39. return label
  40. }
  41. // Resolve returns the value passed into the function
  42. func (nofm *NoOpFieldMapper) Resolve(value string) ([]string, error) {
  43. return []string{value}, nil
  44. }
  45. // ReverseFieldMapper maps the source name for the field to a corresponding set of
  46. // potential labels representing that source field. It also maintains a reverse mapping
  47. // which allows finding a source field by a label.
  48. //
  49. // By design, there are NO overlapping labels permitted between source fields and labels. That is,
  50. // labels for a specific field cannot overlap with labels for another field.
  51. type ReverseFieldMapper struct {
  52. fieldToLabel map[string][]string
  53. labelToField map[string]string
  54. }
  55. // NewReverseFieldMapper creates a new ReverseFieldMapper instance.
  56. func NewReverseFieldMapper() *ReverseFieldMapper {
  57. return &ReverseFieldMapper{
  58. fieldToLabel: make(map[string][]string),
  59. labelToField: make(map[string]string),
  60. }
  61. }
  62. // LabelsFor returns all of the labels associated with that specific field.
  63. func (rfm *ReverseFieldMapper) LabelsFor(field string) []string {
  64. return rfm.fieldToLabel[field]
  65. }
  66. // FieldFor returns the field the provided label is associated with
  67. func (rfm *ReverseFieldMapper) FieldFor(label string) string {
  68. return rfm.labelToField[label]
  69. }
  70. // Resolve attempts to find all labels associated with a given value. If the value
  71. // is a field, then all of the labels associated with that field are returned. If
  72. // the value is a label, then all the labels (including the provided value) are
  73. // returned. Lastly, if neither of the previous conditions are true, an error is
  74. // returned.
  75. func (rfm *ReverseFieldMapper) Resolve(value string) ([]string, error) {
  76. labels := rfm.LabelsFor(value)
  77. if len(labels) > 0 {
  78. return labels, nil
  79. }
  80. field := rfm.FieldFor(value)
  81. if field != "" {
  82. return rfm.LabelsFor(field), nil
  83. }
  84. return nil, fmt.Errorf("no labels found for value %s", value)
  85. }
  86. // Set appends a field -> labels mapping and returns an error if any keys or fields
  87. // overlap
  88. func (rfm *ReverseFieldMapper) Set(source string, labels ...string) error {
  89. if rfm.fieldToLabel == nil {
  90. rfm.fieldToLabel = make(map[string][]string)
  91. }
  92. if _, ok := rfm.fieldToLabel[source]; ok {
  93. return fmt.Errorf("source: %s is already mapped to a set of labels", source)
  94. }
  95. // ensure all labels are unique, and ensure non-zero length
  96. labels = toUnique(source, labels)
  97. rfm.fieldToLabel[source] = labels
  98. if rfm.labelToField == nil {
  99. rfm.labelToField = make(map[string]string)
  100. }
  101. for _, label := range labels {
  102. // overlap check -- clear out previously written mappings, return an error with conflict
  103. if l, ok := rfm.labelToField[label]; ok {
  104. for _, ll := range labels {
  105. delete(rfm.labelToField, ll)
  106. }
  107. return fmt.Errorf("label %s is already mapped to source %s", label, l)
  108. }
  109. rfm.labelToField[label] = source
  110. }
  111. return nil
  112. }
  113. // toUnique ensures that all labels are unique within the provided slices, and will add the source
  114. // string to the list if the provided labels are empty
  115. func toUnique(source string, labels []string) []string {
  116. set := make(map[string]struct{}, len(labels))
  117. for _, label := range labels {
  118. if label != "" {
  119. set[label] = struct{}{}
  120. }
  121. }
  122. result := sliceutil.SeqToSlice(maps.Keys(set))
  123. if len(result) == 0 {
  124. result = append(result, source)
  125. }
  126. return result
  127. }