Skip to content

Commit

Permalink
Finish Instance+Feature.kt and Instance+Segments.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
uniumuniu committed Oct 18, 2023
1 parent 5b78435 commit ea136a6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ We are breaking down the various parts that we need to migrate to Swift in the s
| | SDK's `conditions.ts` ➡️ `Conditions.kt` ||
| | SDK's `datafileReader.ts` ➡️ `DatafileReader.kt` ||
| | SDK's `emitter.ts` ➡️ `Emitter.kt` ||
| | SDK's `feature.ts` ➡️ `Feature.kt` | |
| | SDK's `feature.ts` ➡️ `Instance+Feature.kt` | |
| | SDK's `instance.ts` ➡️ `Instance.kt` | 🟠 |
| | SDK's `logger.ts` ➡️ `Logger.kt` | |
| | SDK's `segments.ts` ➡️ `Segments.kt` | |
| | SDK's `logger.ts` ➡️ `Logger.kt` | |
| | SDK's `segments.ts` ➡️ `Instance+Segments.kt` | |
| | | |
| Constructor options | `bucketKeySeparator` | |
| | `configureBucketKey` | |
Expand Down
37 changes: 16 additions & 21 deletions src/main/kotlin/com/featurevisor/sdk/Instance+Feature.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.featurevisor.sdk

import com.featurevisor.sdk.Conditions.allConditionsAreMatched
import com.featurevisor.types.Allocation
import com.featurevisor.types.Context
import com.featurevisor.types.Feature
import com.featurevisor.types.Force
import com.featurevisor.types.Traffic

fun FeaturevisorInstance.getFeatureByKey(featureKey: String): Feature? {
return datafileReader?.getFeature(featureKey)
Expand All @@ -12,7 +14,7 @@ fun FeaturevisorInstance.getFeatureByKey(featureKey: String): Feature? {
fun FeaturevisorInstance.findForceFromFeature(
feature: Feature,
context: Context,
datafileReader: DatafileReader
datafileReader: DatafileReader,
): Force? {

return feature.force?.firstOrNull { force ->
Expand All @@ -27,54 +29,47 @@ fun FeaturevisorInstance.findForceFromFeature(
fun FeaturevisorInstance.getMatchedTraffic(
traffic: List<Traffic>,
context: Context,
datafileReader: DatafileReader
datafileReader: DatafileReader,
): Traffic? {

return traffic.firstOrNull { trafficItem ->
if (!allGroupSegmentsAreMatched(trafficItem.segments, context, datafileReader)) {
return false
}

true
allGroupSegmentsAreMatched(trafficItem.segments, context, datafileReader)
}
}

fun FeaturevisorInstance.getMatchedAllocation(
traffic: Traffic,
bucketValue: Int
bucketValue: Int,
): Allocation? {

return traffic.allocation.firstOrNull { allocation ->
val start = allocation.range.start
val end = allocation.range.end

start <= bucketValue && end >= bucketValue
with(allocation.range) {
bucketValue in start..end
}
}
}

data class MatchedTrafficAndAllocation(
val matchedTraffic: Traffic?,
val matchedAllocation: Allocation?
val matchedAllocation: Allocation?,
)

fun FeaturevisorInstance.getMatchedTrafficAndAllocation(
traffic: List<Traffic>,
context: Context,
bucketValue: Int,
datafileReader: DatafileReader,
logger: Logger
logger: Logger,
): MatchedTrafficAndAllocation {

var matchedAllocation: Allocation? = null

val matchedTraffic = traffic.firstOrNull { trafficItem ->
if (!allGroupSegmentsAreMatched(trafficItem.segments, context, datafileReader)) {
return false
if (allGroupSegmentsAreMatched(trafficItem.segments, context, datafileReader).not()) {
false
} else {
matchedAllocation = getMatchedAllocation(trafficItem, bucketValue)
matchedAllocation != null
}

matchedAllocation = getMatchedAllocation(trafficItem, bucketValue)

matchedAllocation != null
}

return MatchedTrafficAndAllocation(matchedTraffic, matchedAllocation)
Expand Down
39 changes: 23 additions & 16 deletions src/main/kotlin/com/featurevisor/sdk/Instance+Segments.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.featurevisor.sdk

import com.featurevisor.sdk.Conditions.allConditionsAreMatched
import com.featurevisor.types.Context
import com.featurevisor.types.FeatureKey
import com.featurevisor.types.GroupSegment
import com.featurevisor.types.PlainGroupSegment
import com.featurevisor.types.GroupSegment.*
import com.featurevisor.types.Segment
import com.featurevisor.types.VariationValue

fun FeaturevisorInstance.segmentIsMatched(featureKey: FeatureKey, context: Context): VariationValue? {
Expand All @@ -20,39 +22,44 @@ fun FeaturevisorInstance.segmentIsMatched(featureKey: FeatureKey, context: Conte
return null
}

fun FeaturevisorInstance.segmentIsMatched(segment: Segment, context: Context): Boolean {
return allConditionsAreMatched(segment.conditions, context)
}

fun FeaturevisorInstance.allGroupSegmentsAreMatched(
groupSegments: GroupSegment,
context: Context,
datafileReader: DatafileReader
): Boolean {
when (groupSegments) {
is GroupSegment.Plain -> {
return when (groupSegments) {
is Plain -> {
val segmentKey = groupSegments.segment
if (segmentKey == "*") {
return true
true
} else {
datafileReader.getSegment(segmentKey)?.let {
segmentIsMatched(it, context)
} ?: false
}

val segment = datafileReader.getSegment(segmentKey)
return segmentIsMatched(segment, context)
}
is GroupSegment.Multiple -> {
return groupSegments.segments.all {
is Multiple -> {
groupSegments.segments.all {
allGroupSegmentsAreMatched(it, context, datafileReader)
}
}
is GroupSegment.And -> {
return groupSegments.segment.and.all {
is And -> {
groupSegments.segment.and.all {
allGroupSegmentsAreMatched(it, context, datafileReader)
}
}
is GroupSegment.Or -> {
return groupSegments.segment.or.any {
is Or -> {
groupSegments.segment.or.any {
allGroupSegmentsAreMatched(it, context, datafileReader)
}
}
is GroupSegment.Not -> {
return groupSegments.segment.not.all {
!allGroupSegmentsAreMatched(it, context, datafileReader)
is Not -> {
groupSegments.segment.not.all {
allGroupSegmentsAreMatched(it, context, datafileReader).not()
}
}
}
Expand Down

0 comments on commit ea136a6

Please sign in to comment.