Skip to content

Commit 80676d9

Browse files
committed
fix(openfeature): align canonical fixture reasons
1 parent fe68a62 commit 80676d9

2 files changed

Lines changed: 22 additions & 15 deletions

File tree

packages/dd-trace/src/openfeature/flagging_provider.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class FlaggingProvider extends DatadogNodeServerProvider {
7373
*/
7474
resolveBooleanEvaluation (flagKey, defaultValue, context, logger) {
7575
return super.resolveBooleanEvaluation(flagKey, defaultValue, context, logger)
76-
.then(result => this.#normalizeResolution(flagKey, defaultValue, result))
76+
.then(result => this.#normalizeResolution(flagKey, result))
7777
}
7878

7979
/**
@@ -87,7 +87,7 @@ class FlaggingProvider extends DatadogNodeServerProvider {
8787
*/
8888
resolveStringEvaluation (flagKey, defaultValue, context, logger) {
8989
return super.resolveStringEvaluation(flagKey, defaultValue, context, logger)
90-
.then(result => this.#normalizeResolution(flagKey, defaultValue, result))
90+
.then(result => this.#normalizeResolution(flagKey, result))
9191
}
9292

9393
/**
@@ -101,7 +101,7 @@ class FlaggingProvider extends DatadogNodeServerProvider {
101101
*/
102102
resolveNumberEvaluation (flagKey, defaultValue, context, logger) {
103103
return super.resolveNumberEvaluation(flagKey, defaultValue, context, logger)
104-
.then(result => this.#normalizeResolution(flagKey, defaultValue, result))
104+
.then(result => this.#normalizeResolution(flagKey, result))
105105
}
106106

107107
/**
@@ -116,39 +116,46 @@ class FlaggingProvider extends DatadogNodeServerProvider {
116116
*/
117117
resolveObjectEvaluation (flagKey, defaultValue, context, logger) {
118118
return super.resolveObjectEvaluation(flagKey, defaultValue, context, logger)
119-
.then(result => this.#normalizeResolution(flagKey, defaultValue, result))
119+
.then(result => this.#normalizeResolution(flagKey, result))
120120
}
121121

122122
/**
123123
* Converts provider results to the canonical FFE reason contract.
124124
*
125125
* @template {import('@openfeature/server-sdk').FlagValue} T
126126
* @param {string} flagKey
127-
* @param {T} defaultValue
128127
* @param {import('@openfeature/server-sdk').ResolutionDetails<T>} result
129128
* @returns {import('@openfeature/server-sdk').ResolutionDetails<T>}
130129
*/
131-
#normalizeResolution (flagKey, defaultValue, result) {
132-
if (result?.errorCode === 'FLAG_NOT_FOUND') {
133-
const { errorCode, ...withoutError } = result
134-
return { ...withoutError, value: defaultValue, reason: 'DEFAULT' }
130+
#normalizeResolution (flagKey, result) {
131+
if (result?.reason !== 'TARGETING_MATCH' && result?.reason !== 'DEFAULT') {
132+
return result
135133
}
136134

137-
if (result?.reason !== 'TARGETING_MATCH' && result?.reason !== 'DEFAULT') {
135+
const allocations = this.#ffeConfig?.flags?.[flagKey]?.allocations
136+
if (!Array.isArray(allocations)) {
138137
return result
139138
}
140139

141-
const allocationKey = result.flagMetadata?.allocationKey
142-
const allocation = this.#ffeConfig?.flags?.[flagKey]?.allocations?.find(item => item.key === allocationKey)
143-
if (!allocation || allocation.rules?.length) {
140+
const allocation = allocations.find(item => item.key === result.flagMetadata?.allocationKey)
141+
if (!allocation || allocation.rules?.length || !Array.isArray(allocation.splits)) {
144142
return result
145143
}
146144

147145
const flag = this.#ffeConfig.flags[flagKey]
148-
const selectedSplit = allocation.splits?.find(split => {
146+
const selectedSplit = allocation.splits.find(split => {
149147
const variant = flag.variations?.[split.variationKey]
150148
return variant?.key === result.variant || split.variationKey === result.variant
151149
})
150+
if (!selectedSplit) {
151+
return result
152+
}
153+
154+
const hasTimeBounds = allocation.startAt !== undefined || allocation.endAt !== undefined
155+
if (hasTimeBounds && allocation.splits.length === 1 && !selectedSplit.shards?.length) {
156+
return { ...result, reason: 'DEFAULT' }
157+
}
158+
152159
const reason = selectedSplit?.shards?.length ? 'SPLIT' : 'STATIC'
153160
return { ...result, reason }
154161
}

0 commit comments

Comments
 (0)