Skip to content

Commit

Permalink
feat: standalone targeting, fractional shorthand
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
toddbaert committed Jul 4, 2024
1 parent 1ba4b03 commit 389625e
Show file tree
Hide file tree
Showing 30 changed files with 253 additions and 389 deletions.
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": {
"targeting": {
"$ref": "#/definitions/targeting"
}
},
"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,
"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"
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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

0 comments on commit 389625e

Please sign in to comment.