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

feat: standalone targeting, fractional shorthand #168

Merged
merged 3 commits into from
Jul 8, 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
33 changes: 25 additions & 8 deletions json/flagd_definitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,51 @@ import (
"github.com/xeipuuv/gojsonschema"
)

var compiledSchema *gojsonschema.Schema
var compiledFlagDefinitionSchema *gojsonschema.Schema
var compiledTargetingSchema *gojsonschema.Schema

func init() {
schemaLoader := gojsonschema.NewSchemaLoader()
schemaLoader.AddSchemas(gojsonschema.NewStringLoader(flagd_definitions.TargetingSchema))
flagDefinitionSchemaLoader := gojsonschema.NewSchemaLoader()
flagDefinitionSchemaLoader.AddSchemas(gojsonschema.NewStringLoader(flagd_definitions.TargetingSchema))
targetingSchemaLoader := gojsonschema.NewSchemaLoader()
var err error
compiledSchema, err = schemaLoader.Compile(gojsonschema.NewStringLoader(flagd_definitions.FlagSchema))
compiledFlagDefinitionSchema, err = flagDefinitionSchemaLoader.Compile(gojsonschema.NewStringLoader(flagd_definitions.FlagSchema))
compiledTargetingSchema, err = targetingSchemaLoader.Compile(gojsonschema.NewStringLoader(flagd_definitions.TargetingSchema))
if err != nil {
message := fmt.Errorf("err: %v", err)
log.Fatal(message)
}
}

func TestPositiveParsing(t *testing.T) {
if err := walkPath(true, "./test/positive"); err != nil {
if err := walkPath(true, "./test/flags/positive", compiledFlagDefinitionSchema); err != nil {
t.Error(err)
t.FailNow()
}
}

func TestNegativeParsing(t *testing.T) {
if err := walkPath(false, "./test/negative"); err != nil {
if err := walkPath(false, "./test/flags/negative", compiledFlagDefinitionSchema); err != nil {
t.Error(err)
t.FailNow()
}
}

func walkPath(shouldPass bool, root string) error {
func TestPositiveTargetingParsing(t *testing.T) {
if err := walkPath(true, "./test/targeting/positive", compiledTargetingSchema); err != nil {
t.Error(err)
t.FailNow()
}
}

func TestNegativeTargetingParsing(t *testing.T) {
if err := walkPath(false, "./test/targeting/negative", compiledTargetingSchema); err != nil {
t.Error(err)
t.FailNow()
}
}

func walkPath(shouldPass bool, root string, schema *gojsonschema.Schema) error {
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -56,7 +73,7 @@ func walkPath(shouldPass bool, root string) error {

flagStringLoader := gojsonschema.NewStringLoader(string(file))

p, err := compiledSchema.Validate(flagStringLoader)
p, err := schema.Validate(flagStringLoader)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion json/targeting.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"title": "flagd Targeting",
"description": "Defines targeting logic for flagd; a extension of JSONLogic, including purpose-built feature-flagging operations.",
"type": "object",
"properties": {
Copy link
Member

Choose a reason for hiding this comment

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

i do not think this is actually what we want. this validates an object with a property targeting like

{
  "targeting": {
    "ifddd": [
      {
        "starts_with": [
          {
            "var": "id"
          },
          "abc"
        ]
      },
      "prefix",
      "else"
    ]
  }
}

see https://www.jsonschemavalidator.net/s/yRS6TJ9g -

but what we actually want is a validation without the targeting property like in proposed in #165 (comment) something like

{
  "ifddd": [
    {
      "starts_with": [
        {
          "var": "id"
        },
        "abc"
      ]
    },
    "prefix",
    "else"
  ]
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Absolutely correct, thanks a lot.

I've made this change.

"targeting": {
"$ref": "#/definitions/targeting"
}
},
Copy link
Member

@aepfli aepfli Jul 5, 2024

Choose a reason for hiding this comment

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

Suggested change
"properties": {
"targeting": {
"$ref": "#/definitions/targeting"
}
},
"anyOf": [
{
"$ref": "#/definitions/targeting"
}
],

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh you're totally right. I've corrected this and updated all the tests!

"definitions": {
"targeting": {
"title": "Targeting",
Expand Down Expand Up @@ -462,7 +467,7 @@
"$comment": "if we remove the \"sum to 100\" restriction, update the descriptions below!",
"description": "Distribution for all possible variants, with their associated weighting out of 100.",
"type": "array",
"minItems": 2,
"minItems": 1,
Copy link
Member Author

Choose a reason for hiding this comment

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

This allows for the fractional shorthand support (no wieght == weight 1)

"maxItems": 2,
"items": [
{
Expand Down
5 changes: 4 additions & 1 deletion json/targeting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ title: flagd Targeting
description: Defines targeting logic for flagd; a extension of JSONLogic, including
purpose-built feature-flagging operations.
type: object
properties:
targeting:
$ref: "#/definitions/targeting"
Copy link
Member Author

Choose a reason for hiding this comment

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

This allows for the use of targeting by itself.

definitions:
targeting:
title: Targeting
Expand Down Expand Up @@ -363,7 +366,7 @@ definitions:
description: Distribution for all possible variants, with their associated weighting
out of 100.
type: array
minItems: 2
minItems: 1
maxItems: 2
items:
- description: If this bucket is randomly selected, this string is used to as
Expand Down
26 changes: 0 additions & 26 deletions json/test/negative/fractional-invalid-bucketing.json

This file was deleted.

25 changes: 0 additions & 25 deletions json/test/negative/fractional-invalid-weighting.json

This file was deleted.

23 changes: 0 additions & 23 deletions json/test/negative/invalid-ends-with-param.json

This file was deleted.

23 changes: 0 additions & 23 deletions json/test/negative/invalid-flagd-props.json

This file was deleted.

23 changes: 0 additions & 23 deletions json/test/negative/invalid-starts-with-param.json

This file was deleted.

23 changes: 0 additions & 23 deletions json/test/negative/sem-ver-invalid-range-specifier.json

This file was deleted.

23 changes: 0 additions & 23 deletions json/test/negative/sem-ver-invalid-ver-expression.json

This file was deleted.

74 changes: 0 additions & 74 deletions json/test/positive/basic-json-ops.json

This file was deleted.

Loading
Loading