Skip to content

Commit

Permalink
add doc gen, move schema path, add tests, fix react gen
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Beemer <beeme1mr@users.noreply.github.com>
  • Loading branch information
beeme1mr committed Jan 6, 2025
1 parent 1530d38 commit ecf1bf2
Show file tree
Hide file tree
Showing 26 changed files with 378 additions and 87 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

.PHONY: test
test:
@echo "Running tests..."
go test -v ./...
@echo "Tests passed successfully!"

generate-docs:
@echo "Generating documentation..."
go run ./docs/generate-commands.go
@echo "Documentation generated successfully!"
23 changes: 23 additions & 0 deletions cmd/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra/doc"
)

// GenerateDoc generates cobra docs of the cmd
func GenerateDoc(path string) error {
linkHandler := func(name string) string {
return name
}

filePrepender := func(filename string) string {
return "<!-- markdownlint-disable-file -->\n<!-- WARNING: THIS DOC IS AUTO-GENERATED. DO NOT EDIT! -->\n"
}

if err := doc.GenMarkdownTreeCustom(rootCmd, path, filePrepender, linkHandler); err != nil {
return fmt.Errorf("error generating docs: %w", err)
}
return nil
}
10 changes: 5 additions & 5 deletions cmd/generate/testdata/success_manifest.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
"flags": {
"enableFeatureA": {
"flagType": "boolean",
"defaultValue": false,
"codeDefault": false,
"description": "Controls whether Feature A is enabled."
},
"usernameMaxLength": {
"flagType": "integer",
"defaultValue": 50,
"codeDefault": 50,
"description": "Maximum allowed length for usernames."
},
"greetingMessage": {
"flagType": "string",
"defaultValue": "Hello there!",
"codeDefault": "Hello there!",
"description": "The message to use for greeting users."
},
"discountPercentage": {
"flagType": "float",
"defaultValue": 0.15,
"codeDefault": 0.15,
"description": "Discount percentage applied to purchases."
},
"themeCustomization": {
"flagType": "object",
"defaultValue": {
"codeDefault": {
"primaryColor": "#007bff",
"secondaryColor": "#6c757d"
},
Expand Down
22 changes: 9 additions & 13 deletions cmd/generate/testdata/success_react.golden
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
'use client';

import {
useBooleanFlagDetails,
useNumberFlagDetails,
useStringFlagDetails,
} from "@openfeature/react-sdk";
import { type ReactFlagEvaluationOptions, useFlag } from "@openfeature/react-sdk";

/**
* Discount percentage applied to purchases.
Expand All @@ -14,8 +10,8 @@ import {
* - default value: `0.15`
* - type: `number`
*/
export const useDiscountPercentage = (options: Parameters<typeof useNumberFlagDetails>[2]) => {
return useNumberFlagDetails("discountPercentage", 0.15, options);
export const useDiscountPercentage = (options: ReactFlagEvaluationOptions) => {
return useFlag("discountPercentage", 0.15, options);
};

/**
Expand All @@ -26,8 +22,8 @@ export const useDiscountPercentage = (options: Parameters<typeof useNumberFlagDe
* - default value: `false`
* - type: `boolean`
*/
export const useEnableFeatureA = (options: Parameters<typeof useBooleanFlagDetails>[2]) => {
return useBooleanFlagDetails("enableFeatureA", false, options);
export const useEnableFeatureA = (options: ReactFlagEvaluationOptions) => {
return useFlag("enableFeatureA", false, options);
};

/**
Expand All @@ -38,8 +34,8 @@ export const useEnableFeatureA = (options: Parameters<typeof useBooleanFlagDetai
* - default value: `Hello there!`
* - type: `string`
*/
export const useGreetingMessage = (options: Parameters<typeof useStringFlagDetails>[2]) => {
return useStringFlagDetails("greetingMessage", "Hello there!", options);
export const useGreetingMessage = (options: ReactFlagEvaluationOptions) => {
return useFlag("greetingMessage", "Hello there!", options);
};

/**
Expand All @@ -50,6 +46,6 @@ export const useGreetingMessage = (options: Parameters<typeof useStringFlagDetai
* - default value: `50`
* - type: `number`
*/
export const useUsernameMaxLength = (options: Parameters<typeof useNumberFlagDetails>[2]) => {
return useNumberFlagDetails("usernameMaxLength", 50, options);
export const useUsernameMaxLength = (options: ReactFlagEvaluationOptions) => {
return useFlag("usernameMaxLength", 50, options);
};
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
Version string
Version = "dev"
Commit string
Date string
)
Expand All @@ -20,6 +20,7 @@ var rootCmd = &cobra.Command{
Use: "openfeature",
Short: "CLI for OpenFeature.",
Long: `CLI for OpenFeature related functionalities.`,
DisableAutoGenTag: true,
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
21 changes: 21 additions & 0 deletions docs/commands/openfeature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- markdownlint-disable-file -->
<!-- WARNING: THIS DOC IS AUTO-GENERATED. DO NOT EDIT! -->
## openfeature

CLI for OpenFeature.

### Synopsis

CLI for OpenFeature related functionalities.

### Options

```
-h, --help help for openfeature
```

### SEE ALSO

* [openfeature generate](openfeature_generate.md) - Code generation for flag accessors for OpenFeature.
* [openfeature version](openfeature_version.md) - Print the version number of the OpenFeature CLI

24 changes: 24 additions & 0 deletions docs/commands/openfeature_generate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- markdownlint-disable-file -->
<!-- WARNING: THIS DOC IS AUTO-GENERATED. DO NOT EDIT! -->
## openfeature generate

Code generation for flag accessors for OpenFeature.

### Synopsis

Code generation for flag accessors for OpenFeature.

### Options

```
--flag_manifest_path string Path to the flag manifest.
-h, --help help for generate
--output_path string Output path for the codegen
```

### SEE ALSO

* [openfeature](openfeature.md) - CLI for OpenFeature.
* [openfeature generate go](openfeature_generate_go.md) - Generate Golang flag accessors for OpenFeature.
* [openfeature generate react](openfeature_generate_react.md) - Generate typesafe React Hooks.

32 changes: 32 additions & 0 deletions docs/commands/openfeature_generate_go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!-- markdownlint-disable-file -->
<!-- WARNING: THIS DOC IS AUTO-GENERATED. DO NOT EDIT! -->
## openfeature generate go

Generate Golang flag accessors for OpenFeature.

### Synopsis

Generate Golang flag accessors for OpenFeature.

```
openfeature generate go [flags]
```

### Options

```
-h, --help help for go
--package_name string Name of the Go package to be generated.
```

### Options inherited from parent commands

```
--flag_manifest_path string Path to the flag manifest.
--output_path string Output path for the codegen
```

### SEE ALSO

* [openfeature generate](openfeature_generate.md) - Code generation for flag accessors for OpenFeature.

31 changes: 31 additions & 0 deletions docs/commands/openfeature_generate_react.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!-- markdownlint-disable-file -->
<!-- WARNING: THIS DOC IS AUTO-GENERATED. DO NOT EDIT! -->
## openfeature generate react

Generate typesafe React Hooks.

### Synopsis

Generate typesafe React Hooks compatible with the OpenFeature React SDK.

```
openfeature generate react [flags]
```

### Options

```
-h, --help help for react
```

### Options inherited from parent commands

```
--flag_manifest_path string Path to the flag manifest.
--output_path string Output path for the codegen
```

### SEE ALSO

* [openfeature generate](openfeature_generate.md) - Code generation for flag accessors for OpenFeature.

20 changes: 20 additions & 0 deletions docs/commands/openfeature_version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- markdownlint-disable-file -->
<!-- WARNING: THIS DOC IS AUTO-GENERATED. DO NOT EDIT! -->
## openfeature version

Print the version number of the OpenFeature CLI

```
openfeature version [flags]
```

### Options

```
-h, --help help for version
```

### SEE ALSO

* [openfeature](openfeature.md) - CLI for OpenFeature.

16 changes: 16 additions & 0 deletions docs/generate-commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"log"

"github.com/open-feature/cli/cmd"
)

const docPath = "./docs/commands"

// GenerateDoc generates cobra docs of the cmd
func main() {
if err := cmd.GenerateDoc(docPath); err != nil {
log.Fatal(err)
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ require (
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -30,6 +31,7 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
Expand Down
16 changes: 8 additions & 8 deletions internal/generate/manifestutils/manifestutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"sort"
"strconv"

flagmanifest "github.com/open-feature/cli/docs/schema/v0"
"github.com/open-feature/cli/internal/filesystem"
"github.com/open-feature/cli/internal/flagkeys"
"github.com/open-feature/cli/internal/generate/types"
flagmanifest "github.com/open-feature/cli/schema/v0"

"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/spf13/afero"
Expand Down Expand Up @@ -54,19 +54,19 @@ var stringToFlagType = map[string]types.FlagType{
"object": types.ObjectType,
}

func getDefaultValue(defaultValue interface{}, flagType types.FlagType) string {
func getCodeDefault(codeDefault interface{}, flagType types.FlagType) string {
switch flagType {
case types.BoolType:
return strconv.FormatBool(defaultValue.(bool))
return strconv.FormatBool(codeDefault.(bool))
case types.IntType:
//the conversion to float64 instead of integer typically occurs
//due to how JSON is parsed in Go. In Go's encoding/json package,
//all JSON numbers are unmarshaled into float64 by default when decoding into an interface{}.
return strconv.FormatFloat(defaultValue.(float64), 'g', -1, 64)
return strconv.FormatFloat(codeDefault.(float64), 'g', -1, 64)
case types.FloatType:
return strconv.FormatFloat(defaultValue.(float64), 'g', -1, 64)
return strconv.FormatFloat(codeDefault.(float64), 'g', -1, 64)
case types.StringType:
return defaultValue.(string)
return codeDefault.(string)
default:
return ""
}
Expand Down Expand Up @@ -97,11 +97,11 @@ func unmarshalFlagManifest(data []byte) (*types.BaseTmplData, error) {
flagTypeString := flagData["flagType"].(string)
flagType := stringToFlagType[flagTypeString]
docs := flagData["description"].(string)
defaultValue := getDefaultValue(flagData["defaultValue"], flagType)
codeDefault := getCodeDefault(flagData["codeDefault"], flagType)
btData.Flags = append(btData.Flags, &types.FlagTmplData{
Name: flagKey,
Type: flagType,
DefaultValue: defaultValue,
CodeDefault: codeDefault,
Docs: docs,
})
}
Expand Down
8 changes: 4 additions & 4 deletions internal/generate/plugins/golang/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ func supportImports(flags []*types.FlagTmplData) []string {
return res
}

func defaultValueLiteral(flag *types.FlagTmplData) string {
func codeDefaultValueLiteral(flag *types.FlagTmplData) string {
switch flag.Type {
case types.StringType:
return strconv.Quote(flag.DefaultValue)
return strconv.Quote(flag.CodeDefault)
default:
return flag.DefaultValue
return flag.CodeDefault
}
}

Expand Down Expand Up @@ -111,7 +111,7 @@ func (g *genImpl) Generate(input types.Input) error {
"FlagInitParam": flagInitParam,
"OpenFeatureType": openFeatureType,
"SupportImports": supportImports,
"DefaultValueLiteral": defaultValueLiteral,
"CodeDefaultValueLiteral": codeDefaultValueLiteral,
"TypeString": typeString,
}
td := TmplData{
Expand Down
Loading

0 comments on commit ecf1bf2

Please sign in to comment.