Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support "if" 1-arg shorthand #131

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @austindrenski , the JSON is generated from the YAML, and the CI makes sure they are in sync.

"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:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, "if" can no longer share the its anatomy with other ops (binaryOrTernaryOp), since it's the only one that takes 1-3 args, so it's implemented as it's own rule now.

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
70 changes: 70 additions & 0 deletions json/test/positive/if-shorthand.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these didn't validate before this change, and now they do.

"$schema": "../../flags.json",
"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"
]
}
}
}
Loading