-
Notifications
You must be signed in to change notification settings - Fork 22
feat: add java output for codegen initial version #62
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
Closed
Closed
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
c67f767
feat!: initial commit for codegen CLI with golang support
anghelflorinm ec78985
docs!: add comments to few missing exported functions
anghelflorinm 32f8069
refactor!: add better error messages for the CLI
anghelflorinm e4b02d0
refactor: move package to the top of the repository
anghelflorinm 47889fc
refactor: move main file to top directory
anghelflorinm e5620ed
refactor: embed flag manifest schema into the script
anghelflorinm faf47d4
Merge branch 'main' into 2-cli-prototype
anghelflorinm 9b8ddd8
fix: remove files after being moved
anghelflorinm 5bdfc10
refactor: move main.go back into SRC and attach module to flagmanifes…
anghelflorinm 896ac8d
Merge remote-tracking branch 'origin' into 2-cli-prototype
anghelflorinm a5059d0
refactor: change property names in manifest to camel case format
anghelflorinm 01240cc
refactor: change property names to camel case
anghelflorinm 301a51b
Merge remote-tracking branch 'origin' into 2-cli-prototype
anghelflorinm 9c37b8f
Merge remote-tracking branch 'origin' into 2-cli-prototype
anghelflorinm 96b2139
Merge remote-tracking branch 'origin' into 2-cli-prototype
anghelflorinm c766e83
refactor: change folder, package structure; integrate with cobra
anghelflorinm 087b76b
chore: delete comment; this has been replaced with Git project issue
anghelflorinm 36124c8
chore: update internal/generate/plugins/golang/golang.tmpl
anghelflorinm d858654
Merge remote-tracking branch 'origin' into 2-cli-prototype
anghelflorinm 97cdaff
test: add small tests for generate command for go and react with in m…
anghelflorinm 4682490
Merge branch '2-cli-prototype' of https://github.com/open-feature/cod…
anghelflorinm 09f0ac5
chore: update cmd/generate/generate_test.go
anghelflorinm 6509bda
chore: update cmd/generate/generate_test.go
anghelflorinm efdcc4b
chore: update the permissions when making directory
anghelflorinm a883f7e
Merge remote-tracking branch 'origin' into 2-cli-prototype
anghelflorinm fc94016
chore: remove empty testutils package
anghelflorinm d96c3a0
/bin/bash: qm: command not found
anghelflorinm 3cd02c6
Merge branch main into 2-cli-prototype
anghelflorinm 1076fb8
Merge branch '2-cli-prototype' of https://github.com/open-feature/cod…
anghelflorinm 356780b
Merge branch '2-cli-prototype' of https://github.com/open-feature/cod…
anghelflorinm 9604583
chore: update back to previous mkdir permissions
anghelflorinm dd4fc72
Merge branch '2-cli-prototype' of https://github.com/open-feature/cod…
anghelflorinm 2753953
feat: update golang output with new version
anghelflorinm 929bfbe
Merge remote-tracking branch 'origin' into java-output
anghelflorinm 50fb220
feat: add initial version for java flag accessors
anghelflorinm d23d93a
Merge remote-tracking branch 'origin' into java-output
anghelflorinm ebbc3e3
feat: add initial version for java flag accessors
anghelflorinm 3fbf5b8
chore(main): release 0.1.9 (#58)
openfeaturebot 6a49c87
Merge branch 'java-output' of https://github.com/open-feature/codegen…
anghelflorinm cdebd5d
feat: add initial version for java flag accessors
anghelflorinm 89b6c71
chore(main): release 0.1.9 (#58)
openfeaturebot a2e1105
Merge branch 'java-output' of https://github.com/open-feature/codegen…
anghelflorinm 7095703
Revert "feat: update golang output with new version" for the java branch
anghelflorinm 3327dc2
Merge branch 'main' into java-output
beeme1mr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package java | ||
|
|
||
| import ( | ||
| "github.com/open-feature/cli/internal/flagkeys" | ||
| "github.com/open-feature/cli/internal/generate" | ||
| "github.com/open-feature/cli/internal/generate/plugins/java" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| // Cmd for `generate java“ command, handling code generation for flag accessors | ||
| // for Java. | ||
| var Cmd = &cobra.Command{ | ||
| Use: "java", | ||
| Short: "Generate Java flag accessors for OpenFeature.", | ||
| Long: `Generate Java flag accessors for OpenFeature.`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| params := java.Params{ | ||
| JavaPackage: viper.GetString(flagkeys.JavaPackageName), | ||
| } | ||
| gen := java.NewGenerator(params) | ||
| err := generate.CreateFlagAccessors(gen) | ||
| return err | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| Cmd.Flags().String(flagkeys.JavaPackageName, "", "Name of the Java package to be generated.") | ||
| Cmd.MarkFlagRequired(flagkeys.JavaPackageName) | ||
| viper.BindPFlag(flagkeys.JavaPackageName, Cmd.Flags().Lookup(flagkeys.JavaPackageName)) | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // AUTOMATICALLY GENERATED BY OPENFEATURE CODEGEN, DO NOT EDIT. | ||
| package example_java; | ||
| import dev.openfeature.sdk.*; | ||
|
|
||
|
|
||
| public class ExperimentFlags { | ||
| private static Client client = OpenFeatureAPI.getInstance().getClient(); | ||
| /** | ||
| * Discount percentage applied to purchases. | ||
| */ | ||
| public static class DiscountPercentage { | ||
| public static Double getValue(EvaluationContext evaluationContext) { | ||
| return client.getDoubleValue("discountPercentage", 0.15, evaluationContext); | ||
| } | ||
|
|
||
| public static FlagEvaluationDetails<Double> getValueDetails(EvaluationContext evaluationContext) { | ||
| return client.getDoubleDetails("discountPercentage", 0.15, evaluationContext); | ||
| } | ||
| } | ||
| /** | ||
| * Controls whether Feature A is enabled. | ||
| */ | ||
| public static class EnableFeatureA { | ||
| public static Boolean getValue(EvaluationContext evaluationContext) { | ||
| return client.getBooleanValue("enableFeatureA", false, evaluationContext); | ||
| } | ||
|
|
||
| public static FlagEvaluationDetails<Boolean> getValueDetails(EvaluationContext evaluationContext) { | ||
| return client.getBooleanDetails("enableFeatureA", false, evaluationContext); | ||
| } | ||
| } | ||
| /** | ||
| * The message to use for greeting users. | ||
| */ | ||
| public static class GreetingMessage { | ||
| public static String getValue(EvaluationContext evaluationContext) { | ||
| return client.getStringValue("greetingMessage", "Hello there!", evaluationContext); | ||
| } | ||
|
|
||
| public static FlagEvaluationDetails<String> getValueDetails(EvaluationContext evaluationContext) { | ||
| return client.getStringDetails("greetingMessage", "Hello there!", evaluationContext); | ||
| } | ||
| } | ||
| /** | ||
| * Maximum allowed length for usernames. | ||
| */ | ||
| public static class UsernameMaxLength { | ||
| public static Integer getValue(EvaluationContext evaluationContext) { | ||
| return client.getIntegerValue("usernameMaxLength", 50, evaluationContext); | ||
| } | ||
|
|
||
| public static FlagEvaluationDetails<Integer> getValueDetails(EvaluationContext evaluationContext) { | ||
| return client.getIntegerDetails("usernameMaxLength", 50, evaluationContext); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package java | ||
|
|
||
| import ( | ||
| _ "embed" | ||
| "html/template" | ||
| "path/filepath" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/iancoleman/strcase" | ||
| "github.com/open-feature/cli/internal/generate" | ||
| "github.com/open-feature/cli/internal/generate/types" | ||
| ) | ||
|
|
||
| // TmplData contains the Java-specific data and the base data for the codegen. | ||
| type TmplData struct { | ||
| *types.BaseTmplData | ||
| JavaPackage string | ||
| } | ||
|
|
||
| type genImpl struct { | ||
| javaPackage string | ||
| } | ||
|
|
||
| // BaseTmplDataInfo provides the base template data for the codegen. | ||
| func (td *TmplData) BaseTmplDataInfo() *types.BaseTmplData { | ||
| return td.BaseTmplData | ||
| } | ||
|
|
||
| // supportedFlagTypes is the flag types supported by the Java template. | ||
| var supportedFlagTypes = map[types.FlagType]bool{ | ||
| types.FloatType: true, | ||
| types.StringType: true, | ||
| types.IntType: true, | ||
| types.BoolType: true, | ||
| types.ObjectType: false, | ||
| } | ||
|
|
||
| func (*genImpl) SupportedFlagTypes() map[types.FlagType]bool { | ||
| return supportedFlagTypes | ||
| } | ||
|
|
||
| //go:embed java.tmpl | ||
| var javaTmpl string | ||
|
|
||
| // Java Funcs BEGIN | ||
|
|
||
| func flagClass(flagName string) string { | ||
| return strcase.ToCamel(flagName) | ||
| } | ||
|
|
||
| func topLevelClass(outputPath string) string { | ||
| return strings.TrimSuffix(filepath.Base(outputPath), filepath.Ext(outputPath)) | ||
| } | ||
|
|
||
| func flagInitParam(flagName string) string { | ||
| return strconv.Quote(flagName) | ||
| } | ||
|
|
||
| func javaType(t types.FlagType) string { | ||
| switch t { | ||
| case types.IntType: | ||
| return "Integer" | ||
| case types.FloatType: | ||
| return "Double" | ||
| case types.BoolType: | ||
| return "Boolean" | ||
| case types.StringType: | ||
| return "String" | ||
| default: | ||
| return "" | ||
| } | ||
| } | ||
|
|
||
| func supportImports(flags []*types.FlagTmplData) []string { | ||
| var res []string | ||
| if len(flags) > 0 { | ||
| res = append(res, "dev.openfeature.sdk.*") | ||
| } | ||
| sort.Strings(res) | ||
| return res | ||
| } | ||
|
|
||
| func defaultValueLiteral(flag *types.FlagTmplData) string { | ||
| switch flag.Type { | ||
| case types.StringType: | ||
| return strconv.Quote(flag.DefaultValue) | ||
| default: | ||
| return flag.DefaultValue | ||
| } | ||
| } | ||
|
|
||
| // Java Funcs END | ||
|
|
||
| // Generate generates the Java flag accessors for OpenFeature. | ||
| func (g *genImpl) Generate(input types.Input) error { | ||
| funcs := template.FuncMap{ | ||
| "FlagClass": flagClass, | ||
| "TopLevelClass": topLevelClass, | ||
| "FlagInitParam": flagInitParam, | ||
| "JavaType": javaType, | ||
| "SupportImports": supportImports, | ||
| "DefaultValueLiteral": defaultValueLiteral, | ||
| } | ||
| td := TmplData{ | ||
| BaseTmplData: input.BaseData, | ||
| JavaPackage: g.javaPackage, | ||
| } | ||
| return generate.GenerateFile(funcs, javaTmpl, &td) | ||
| } | ||
|
|
||
| // Params are parameters for creating a Generator | ||
| type Params struct { | ||
| JavaPackage string | ||
| } | ||
|
|
||
| // NewGenerator creates a generator for Java. | ||
| func NewGenerator(params Params) types.Generator { | ||
| return &genImpl{ | ||
| javaPackage: params.JavaPackage, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // AUTOMATICALLY GENERATED BY OPENFEATURE CODEGEN, DO NOT EDIT. | ||
| package {{.JavaPackage}}; | ||
|
|
||
|
|
||
| {{- range $_, $p := SupportImports .Flags}} | ||
| import {{$p}}; | ||
| {{- end}} | ||
|
|
||
|
|
||
| public class {{TopLevelClass .OutputPath}} { | ||
| private static Client client = OpenFeatureAPI.getInstance().getClient(); | ||
|
|
||
| {{- range .Flags}} | ||
| /** | ||
| * {{.Docs}} | ||
| */ | ||
| public static class {{FlagClass .Name}} { | ||
| public static {{JavaType .Type}} getValue(EvaluationContext evaluationContext) { | ||
| return client.get{{JavaType .Type}}Value({{FlagInitParam .Name}}, {{DefaultValueLiteral .}}, evaluationContext); | ||
| } | ||
|
|
||
| public static FlagEvaluationDetails<{{JavaType .Type}}> getValueDetails(EvaluationContext evaluationContext) { | ||
| return client.get{{JavaType .Type}}Details({{FlagInitParam .Name}}, {{DefaultValueLiteral .}}, evaluationContext); | ||
| } | ||
| } | ||
| {{- end}} | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does
types.ObjectTypecase belong here too?EDIT: Michael let me know that this is OOS for now, so disregard 😄