From 463d33a3895f1cd1149c9d99cdc5fb7981abd296 Mon Sep 17 00:00:00 2001 From: Todd Baert Date: Wed, 14 Feb 2024 16:02:45 -0500 Subject: [PATCH] fix: support "if" 1-arg shorthand (#131) Adds schema-support for 1-arg "if" shorthand. Part of fixing: https://github.com/open-feature/flagd/issues/1205 --------- Signed-off-by: Todd Baert --- json/targeting.json | 24 +++++++-- json/targeting.yaml | 24 ++++++--- json/test/positive/basic-json-ops.json | 2 + json/test/positive/if-shorthand.json | 71 ++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 12 deletions(-) create mode 100644 json/test/positive/if-shorthand.json diff --git a/json/targeting.json b/json/targeting.json index 345e5e0..1b58ece 100644 --- a/json/targeting.json +++ b/json/targeting.json @@ -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, @@ -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.", @@ -548,6 +559,9 @@ { "$ref": "#/$defs/missingSomeRule" }, + { + "$ref": "#/$defs/ifRule" + }, { "$ref": "#/$defs/binaryRule" }, diff --git a/json/targeting.yaml b/json/targeting.yaml index 94ca767..f81f0dc 100644 --- a/json/targeting.yaml +++ b/json/targeting.yaml @@ -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 @@ -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. @@ -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" diff --git a/json/test/positive/basic-json-ops.json b/json/test/positive/basic-json-ops.json index 8faf2c6..c81465c 100644 --- a/json/test/positive/basic-json-ops.json +++ b/json/test/positive/basic-json-ops.json @@ -11,6 +11,8 @@ "targeting": { "*" : [ {"if" : [ true, "yes", "no" ]}, + {"if" : [ true, "yes" ]}, + {"if" : [ true ]}, {"==" : [1, 1]}, {"===" : [1, 1]}, {"!=" : [1, 2]}, diff --git a/json/test/positive/if-shorthand.json b/json/test/positive/if-shorthand.json new file mode 100644 index 0000000..88b1da8 --- /dev/null +++ b/json/test/positive/if-shorthand.json @@ -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" + ] + } + } +}