File size: 2,199 Bytes
287a0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package model

type CollectionMetadataValueType interface {
	IsCollectionMetadataValueType()
	Equals(other CollectionMetadataValueType) bool
}

type CollectionMetadataValueStringType struct {
	Value string
}

func (s *CollectionMetadataValueStringType) IsCollectionMetadataValueType() {}

func (s *CollectionMetadataValueStringType) Equals(other CollectionMetadataValueType) bool {
	if o, ok := other.(*CollectionMetadataValueStringType); ok {
		return s.Value == o.Value
	}
	return false
}

type CollectionMetadataValueInt64Type struct {
	Value int64
}

func (s *CollectionMetadataValueInt64Type) IsCollectionMetadataValueType() {}

func (s *CollectionMetadataValueInt64Type) Equals(other CollectionMetadataValueType) bool {
	if o, ok := other.(*CollectionMetadataValueInt64Type); ok {
		return s.Value == o.Value
	}
	return false
}

type CollectionMetadataValueFloat64Type struct {
	Value float64
}

func (s *CollectionMetadataValueFloat64Type) IsCollectionMetadataValueType() {}

func (s *CollectionMetadataValueFloat64Type) Equals(other CollectionMetadataValueType) bool {
	if o, ok := other.(*CollectionMetadataValueFloat64Type); ok {
		return s.Value == o.Value
	}
	return false
}

type CollectionMetadata[T CollectionMetadataValueType] struct {
	Metadata map[string]T
}

func NewCollectionMetadata[T CollectionMetadataValueType]() *CollectionMetadata[T] {
	return &CollectionMetadata[T]{
		Metadata: make(map[string]T),
	}
}

func (m *CollectionMetadata[T]) Add(key string, value T) {
	m.Metadata[key] = value
}

func (m *CollectionMetadata[T]) Get(key string) T {
	return m.Metadata[key]
}

func (m *CollectionMetadata[T]) Remove(key string) {
	delete(m.Metadata, key)
}

func (m *CollectionMetadata[T]) Empty() bool {
	return len(m.Metadata) == 0
}

func (m *CollectionMetadata[T]) Equals(other *CollectionMetadata[T]) bool {
	if m == nil && other == nil {
		return true
	}
	if m == nil && other != nil {
		return false
	}
	if m != nil && other == nil {
		return false
	}
	if len(m.Metadata) != len(other.Metadata) {
		return false
	}
	for key, value := range m.Metadata {
		if otherValue, ok := other.Metadata[key]; !ok || !value.Equals(otherValue) {
			return false
		}
	}
	return true
}