|  | 
|  | 1 | +package dtos | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"encoding/json" | 
|  | 5 | +) | 
|  | 6 | + | 
|  | 7 | +// FFResponse is an interface that abstracts the differences between different versions of feature flag responses. | 
|  | 8 | +type FFResponse interface { | 
|  | 9 | +	NeedsAnotherFetch() bool | 
|  | 10 | +	RuleBasedSegments() []RuleBasedSegmentDTO | 
|  | 11 | +	FeatureFlags() []SplitDTO | 
|  | 12 | +	FFTill() int64 | 
|  | 13 | +	RBTill() int64 | 
|  | 14 | +	FFSince() int64 | 
|  | 15 | +	RBSince() int64 | 
|  | 16 | +} | 
|  | 17 | + | 
|  | 18 | +// FFResponseLegacy handles the legacy format of feature flag responses. | 
|  | 19 | +type FFResponseLegacy struct { | 
|  | 20 | +	SplitChanges SplitsDTO | 
|  | 21 | +} | 
|  | 22 | + | 
|  | 23 | +// NewFFResponseLegacy creates a new FFResponseLegacy instance from the provided JSON data. | 
|  | 24 | +func NewFFResponseLegacy(data []byte) (FFResponse, error) { | 
|  | 25 | +	var splitChangesDto SplitsDTO | 
|  | 26 | +	err := json.Unmarshal(data, &splitChangesDto) | 
|  | 27 | +	if err == nil { | 
|  | 28 | +		return &FFResponseLegacy{ | 
|  | 29 | +			SplitChanges: splitChangesDto, | 
|  | 30 | +		}, nil | 
|  | 31 | +	} | 
|  | 32 | +	return nil, err | 
|  | 33 | +} | 
|  | 34 | + | 
|  | 35 | +// NeedsAnotherFetch checks if another fetch is needed based on the since and till values. | 
|  | 36 | +func (ffLegacy *FFResponseLegacy) NeedsAnotherFetch() bool { | 
|  | 37 | +	return ffLegacy.SplitChanges.Since == ffLegacy.SplitChanges.Till | 
|  | 38 | +} | 
|  | 39 | + | 
|  | 40 | +// FeatureFlags returns the list of feature flags (splits) from the response. | 
|  | 41 | +func (ffLegacy *FFResponseLegacy) FeatureFlags() []SplitDTO { | 
|  | 42 | +	return ffLegacy.SplitChanges.Splits | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +// RuleBasedSegments returns an empty list as legacy responses do not contain rule-based segments. | 
|  | 46 | +func (ffLegacy *FFResponseLegacy) RuleBasedSegments() []RuleBasedSegmentDTO { | 
|  | 47 | +	return []RuleBasedSegmentDTO{} | 
|  | 48 | +} | 
|  | 49 | + | 
|  | 50 | +// FFTill returns the till value for feature flags. | 
|  | 51 | +func (ffLegacy *FFResponseLegacy) FFTill() int64 { | 
|  | 52 | +	return ffLegacy.SplitChanges.Till | 
|  | 53 | +} | 
|  | 54 | + | 
|  | 55 | +// RBTill returns 0 as legacy responses do not contain rule-based segments. | 
|  | 56 | +func (ffLegacy *FFResponseLegacy) RBTill() int64 { | 
|  | 57 | +	return 0 | 
|  | 58 | +} | 
|  | 59 | + | 
|  | 60 | +// FFSince returns the since value for feature flags. | 
|  | 61 | +func (ffLegacy *FFResponseLegacy) FFSince() int64 { | 
|  | 62 | +	return ffLegacy.SplitChanges.Since | 
|  | 63 | +} | 
|  | 64 | + | 
|  | 65 | +// RBSince returns 0 as legacy responses do not contain rule-based segments. | 
|  | 66 | +func (ffLegacy *FFResponseLegacy) RBSince() int64 { | 
|  | 67 | +	return 0 | 
|  | 68 | +} | 
|  | 69 | + | 
|  | 70 | +// FFResponseV13 handles the version 1.3 format of feature flag responses. | 
|  | 71 | +type FFResponseV13 struct { | 
|  | 72 | +	SplitChanges SplitChangesDTO | 
|  | 73 | +} | 
|  | 74 | + | 
|  | 75 | +// NewFFResponseV13 creates a new FFResponseV13 instance from the provided JSON data. | 
|  | 76 | +func NewFFResponseV13(data []byte) (FFResponse, error) { | 
|  | 77 | +	var splitChangesDto SplitChangesDTO | 
|  | 78 | +	err := json.Unmarshal(data, &splitChangesDto) | 
|  | 79 | +	if err == nil { | 
|  | 80 | +		return &FFResponseV13{ | 
|  | 81 | +			SplitChanges: splitChangesDto, | 
|  | 82 | +		}, nil | 
|  | 83 | +	} | 
|  | 84 | +	return nil, err | 
|  | 85 | +} | 
|  | 86 | + | 
|  | 87 | +// FeatureFlags returns the list of feature flags (splits) from the response. | 
|  | 88 | +func (f13 *FFResponseV13) FeatureFlags() []SplitDTO { | 
|  | 89 | +	return f13.SplitChanges.FeatureFlags.Splits | 
|  | 90 | +} | 
|  | 91 | + | 
|  | 92 | +// RuleBasedSegments returns the list of rule-based segments from the response. | 
|  | 93 | +func (f13 *FFResponseV13) RuleBasedSegments() []RuleBasedSegmentDTO { | 
|  | 94 | +	return f13.SplitChanges.RuleBasedSegments.RuleBasedSegments | 
|  | 95 | +} | 
|  | 96 | + | 
|  | 97 | +// NeedsAnotherFetch checks if another fetch is needed based on the since and till values for both feature flags and rule-based segments. | 
|  | 98 | +func (f13 FFResponseV13) NeedsAnotherFetch() bool { | 
|  | 99 | +	return f13.SplitChanges.FeatureFlags.Since == f13.SplitChanges.FeatureFlags.Till && f13.SplitChanges.RuleBasedSegments.Since == f13.SplitChanges.RuleBasedSegments.Till | 
|  | 100 | +} | 
|  | 101 | + | 
|  | 102 | +// FFTill returns the till value for feature flags. | 
|  | 103 | +func (f13 *FFResponseV13) FFTill() int64 { | 
|  | 104 | +	return f13.SplitChanges.FeatureFlags.Till | 
|  | 105 | +} | 
|  | 106 | + | 
|  | 107 | +// RBTill returns the till value for rule-based segments. | 
|  | 108 | +func (f13 *FFResponseV13) RBTill() int64 { | 
|  | 109 | +	return f13.SplitChanges.RuleBasedSegments.Till | 
|  | 110 | +} | 
|  | 111 | + | 
|  | 112 | +// RBSince returns the since value for rule-based segments. | 
|  | 113 | +func (f13 *FFResponseV13) RBSince() int64 { | 
|  | 114 | +	return f13.SplitChanges.RuleBasedSegments.Since | 
|  | 115 | +} | 
|  | 116 | + | 
|  | 117 | +// FFSince returns the since value for feature flags. | 
|  | 118 | +func (f13 *FFResponseV13) FFSince() int64 { | 
|  | 119 | +	return f13.SplitChanges.FeatureFlags.Since | 
|  | 120 | +} | 
|  | 121 | + | 
|  | 122 | +// FFResponseLocalV13 is a local version of FFResponseV13 for internal use. | 
|  | 123 | +type FFResponseLocalV13 struct { | 
|  | 124 | +	SplitChanges SplitChangesDTO | 
|  | 125 | +} | 
|  | 126 | + | 
|  | 127 | +// NewFFResponseLocalV13 creates a new FFResponseLocalV13 instance from the provided JSON data. | 
|  | 128 | +func NewFFResponseLocalV13(data []byte) (*FFResponseLocalV13, error) { | 
|  | 129 | +	var splitChangesDto SplitChangesDTO | 
|  | 130 | +	err := json.Unmarshal(data, &splitChangesDto) | 
|  | 131 | +	if err == nil { | 
|  | 132 | +		return &FFResponseLocalV13{ | 
|  | 133 | +			SplitChanges: splitChangesDto, | 
|  | 134 | +		}, nil | 
|  | 135 | +	} | 
|  | 136 | +	return nil, err | 
|  | 137 | +} | 
|  | 138 | + | 
|  | 139 | +// FeatureFlags returns the list of feature flags (splits) from the response. | 
|  | 140 | +func (f *FFResponseLocalV13) FeatureFlags() []SplitDTO { | 
|  | 141 | +	return f.SplitChanges.FeatureFlags.Splits | 
|  | 142 | +} | 
|  | 143 | + | 
|  | 144 | +// RuleBasedSegments returns the list of rule-based segments from the response. | 
|  | 145 | +func (f *FFResponseLocalV13) RuleBasedSegments() []RuleBasedSegmentDTO { | 
|  | 146 | +	return f.SplitChanges.RuleBasedSegments.RuleBasedSegments | 
|  | 147 | +} | 
|  | 148 | + | 
|  | 149 | +// NeedsAnotherFetch checks if another fetch is needed based on the since and till values for both feature flags and rule-based segments. | 
|  | 150 | +func (f FFResponseLocalV13) NeedsAnotherFetch() bool { | 
|  | 151 | +	return f.SplitChanges.FeatureFlags.Since == f.SplitChanges.FeatureFlags.Till && f.SplitChanges.RuleBasedSegments.Since == f.SplitChanges.RuleBasedSegments.Till | 
|  | 152 | +} | 
|  | 153 | + | 
|  | 154 | +// FFTill returns the till value for feature flags. | 
|  | 155 | +func (f *FFResponseLocalV13) FFTill() int64 { | 
|  | 156 | +	return f.SplitChanges.FeatureFlags.Till | 
|  | 157 | +} | 
|  | 158 | + | 
|  | 159 | +// RBTill returns the till value for rule-based segments. | 
|  | 160 | +func (f *FFResponseLocalV13) RBTill() int64 { | 
|  | 161 | +	return f.SplitChanges.RuleBasedSegments.Till | 
|  | 162 | +} | 
|  | 163 | + | 
|  | 164 | +// RBSince returns the since value for rule-based segments. | 
|  | 165 | +func (f *FFResponseLocalV13) RBSince() int64 { | 
|  | 166 | +	return f.SplitChanges.RuleBasedSegments.Since | 
|  | 167 | +} | 
|  | 168 | + | 
|  | 169 | +// FFSince returns the since value for feature flags. | 
|  | 170 | +func (f *FFResponseLocalV13) SetFFTill(till int64) { | 
|  | 171 | +	f.SplitChanges.FeatureFlags.Till = till | 
|  | 172 | +} | 
|  | 173 | + | 
|  | 174 | +// SetFFSince sets the since value for feature flags. | 
|  | 175 | +func (f *FFResponseLocalV13) SetFFSince(since int64) { | 
|  | 176 | +	f.SplitChanges.FeatureFlags.Since = since | 
|  | 177 | +} | 
|  | 178 | + | 
|  | 179 | +// SetRBTill sets the till value for rule-based segments. | 
|  | 180 | +func (f *FFResponseLocalV13) SetRBTill(till int64) { | 
|  | 181 | +	f.SplitChanges.RuleBasedSegments.Till = till | 
|  | 182 | +} | 
|  | 183 | + | 
|  | 184 | +// FFSince returns the since value for feature flags. | 
|  | 185 | +func (f *FFResponseLocalV13) FFSince() int64 { | 
|  | 186 | +	return f.SplitChanges.FeatureFlags.Since | 
|  | 187 | +} | 
|  | 188 | + | 
|  | 189 | +// SetRBSince sets the since value for rule-based segments. | 
|  | 190 | +func (f *FFResponseLocalV13) SetRBSince(since int64) { | 
|  | 191 | +	f.SplitChanges.RuleBasedSegments.Since = since | 
|  | 192 | +} | 
|  | 193 | + | 
|  | 194 | +// ReplaceFF replaces the feature flags (splits) in the response with the provided list. | 
|  | 195 | +func (f *FFResponseLocalV13) ReplaceFF(featureFlags []SplitDTO) { | 
|  | 196 | +	f.SplitChanges.FeatureFlags.Splits = featureFlags | 
|  | 197 | +} | 
|  | 198 | + | 
|  | 199 | +// ReplaceRB replaces the rule-based segments in the response with the provided list. | 
|  | 200 | +func (f *FFResponseLocalV13) ReplaceRB(ruleBasedSegments []RuleBasedSegmentDTO) { | 
|  | 201 | +	f.SplitChanges.RuleBasedSegments.RuleBasedSegments = ruleBasedSegments | 
|  | 202 | +} | 
|  | 203 | + | 
|  | 204 | +var _ FFResponse = (*FFResponseLegacy)(nil) | 
|  | 205 | +var _ FFResponse = (*FFResponseV13)(nil) | 
|  | 206 | +var _ FFResponse = (*FFResponseLocalV13)(nil) | 
0 commit comments