Skip to content

Commit

Permalink
fix: support "if" 1-arg shorthand (#131)
Browse files Browse the repository at this point in the history
Adds schema-support for 1-arg "if" shorthand.

Part of fixing: open-feature/flagd#1205

---------

Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
toddbaert authored Feb 14, 2024
1 parent b98a826 commit 463d33a
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 12 deletions.
24 changes: 19 additions & 5 deletions json/targeting.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@
}
}
},
"ifRule": {
"type": "object",
"additionalProperties": false,
"properties": {
"if": {
"title": "If Operator",
"description": "The if statement takes 1-3 arguments: a condition (\"if\"), what to do if its true (\"then\", optional, defaults to returning true), and what to do if its false (\"else\", optional, defaults to returning false). Note that the form accepting more than 3 arguments (else-if) is not supported in flagd; use nesting instead.",
"type": "array",
"minItems": 1,
"maxItems": 3,
"items": {
"$ref": "#/$defs/args"
}
}
}
},
"binaryOrTernaryOp": {
"type": "array",
"minItems": 2,
Expand All @@ -145,11 +161,6 @@
"type": "object",
"additionalProperties": false,
"properties": {
"if": {
"title": "If Operator",
"description": "The if statement takes 2-3 arguments: a condition (if), what to do if its true (then), and what to do if its false (else, optional). Note that the form accepting more than 3 arguments (if/else) is not supported in flagd; use nesting instead.",
"$ref": "#/$defs/binaryOrTernaryOp"
},
"substr": {
"title": "Substring Operation",
"description": "Get a portion of a string. Give a positive start position to return everything beginning at that index. Give a negative start position to work backwards from the end of the string, then return everything. Give a positive length to express how many characters to return.",
Expand Down Expand Up @@ -548,6 +559,9 @@
{
"$ref": "#/$defs/missingSomeRule"
},
{
"$ref": "#/$defs/ifRule"
},
{
"$ref": "#/$defs/binaryRule"
},
Expand Down
24 changes: 17 additions & 7 deletions json/targeting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ type: object
- type: array
items:
type: string
ifRule:
type: object
additionalProperties: false
properties:
if:
title: If Operator
description: 'The if statement takes 1-3 arguments: a condition ("if"), what to
do if its true ("then", optional, defaults to returning true),
and what to do if its false ("else", optional, defaults to returning false).
Note that the form accepting more than 3 arguments (else-if) is not supported in flagd;
use nesting instead.'
type: array
minItems: 1
maxItems: 3
items:
$ref: "#/$defs/args"
binaryOrTernaryOp:
type: array
minItems: 2
Expand All @@ -107,13 +123,6 @@ type: object
type: object
additionalProperties: false
properties:
if:
title: If Operator
description: 'The if statement takes 2-3 arguments: a condition (if), what to
do if its true (then), and what to do if its false (else, optional). Note that the
form accepting more than 3 arguments (if/else) is not supported in flagd;
use nesting instead.'
$ref: "#/$defs/binaryOrTernaryOp"
substr:
title: Substring Operation
description: Get a portion of a string. Give a positive start position to return everything beginning at that index.
Expand Down Expand Up @@ -418,6 +427,7 @@ type: object
- $ref: "#/$defs/varRule"
- $ref: "#/$defs/missingRule"
- $ref: "#/$defs/missingSomeRule"
- $ref: "#/$defs/ifRule"
- $ref: "#/$defs/binaryRule"
- $ref: "#/$defs/binaryOrTernaryRule"
- $ref: "#/$defs/associativeRule"
Expand Down
2 changes: 2 additions & 0 deletions json/test/positive/basic-json-ops.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"targeting": {
"*" : [
{"if" : [ true, "yes", "no" ]},
{"if" : [ true, "yes" ]},
{"if" : [ true ]},
{"==" : [1, 1]},
{"===" : [1, 1]},
{"!=" : [1, 2]},
Expand Down
71 changes: 71 additions & 0 deletions json/test/positive/if-shorthand.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"$schema": "../../flags.json",
"$comments": "tests that all the variants of 'if' work (bugfix)",
"flags": {
"if-shorthand-1": {
"state": "ENABLED",
"variants": {
"true": true,
"false": false
},
"defaultVariant": "true",
"targeting": {
"if": [
{
"===": [
{
"var": "env"
},
"production"
]
}
]
}
},
"if-shorthand-2": {
"state": "ENABLED",
"variants": {
"true": true,
"false": false
},
"defaultVariant": "true",
"targeting": {
"if": [
{
"===": [
{
"var": "env"
},
"production"
]
}, "true"
]
}
},
"if-shorthand-ref": {
"state": "ENABLED",
"variants": {
"true": true,
"false": false
},
"defaultVariant": "true",
"targeting": {
"if": [
{
"$ref": "env-production"
}
]
}
}
},
"$evaluators": {
"env-production": {
"===": [
{
"var": "env"
},
"production"
]
}
}
}

0 comments on commit 463d33a

Please sign in to comment.