-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Support enums in generator #3602
Merged
Merged
Conversation
This file contains 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
Given the Golang model: Golang type `PeerAuthentication_MutualTLS` that uses a const: ```go type PeerAuthentication_MutualTLS_Mode int32 type PeerAuthentication_MutualTLS struct { // Defines the mTLS mode used for peer authentication. Mode PeerAuthentication_MutualTLS_Mode `protobuf:"varint,1,opt,name=mode,proto3,enum=istio.security.v1beta1.PeerAuthentication_MutualTLS_Mode" json:"mode,omitempty"` } ``` Where the const is: ```go type PeerAuthentication_MutualTLS_Mode int32 const ( // Inherit from parent, if has one. Otherwise treated as PERMISSIVE. PeerAuthentication_MutualTLS_UNSET PeerAuthentication_MutualTLS_Mode = 0 // Connection is not tunneled. PeerAuthentication_MutualTLS_DISABLE PeerAuthentication_MutualTLS_Mode = 1 // Connection can be either plaintext or mTLS tunnel. PeerAuthentication_MutualTLS_PERMISSIVE PeerAuthentication_MutualTLS_Mode = 2 // Connection is an mTLS tunnel (TLS with client cert must be presented). PeerAuthentication_MutualTLS_STRICT PeerAuthentication_MutualTLS_Mode = 3 ) ``` The resulting Java model now contains an auto generated enum: ``` // ... public class PeerAuthentication_MutualTLS implements KubernetesResource { @JsonProperty("mode") private PeerAuthentication_MutualTLS_Mode mode; // ... } ``` ```java public enum PeerAuthentication_MutualTLS_Mode { // Inherit from parent, if has one. Otherwise treated as PERMISSIVE. UNSET("UNSET"), // Connection is not tunneled. DISABLE("DISABLE"), // Connection can be either plaintext or mTLS tunnel. PERMISSIVE("PERMISSIVE"), // Connection is an mTLS tunnel (TLS with client cert must be presented). STRICT("STRICT"); } ``` Note that all the enumerators are auto discovered when the target type is a String, which means that if we want to achieve the following enumerator: public enum PeerAuthentication_MutualTLS_Mode { // Inherit from parent, if has one. Otherwise treated as PERMISSIVE. UNSET(1), // Connection is not tunneled. DISABLE(2), // Connection can be either plaintext or mTLS tunnel. PERMISSIVE(3), // Connection is an mTLS tunnel (TLS with client cert must be presented). STRICT(4); } ``` Then, the extension need to provide the enum values manually: ```go enumMapping := map[reflect.Type]schemagen.EnumDescriptor{ reflect.TypeOf(type.MyEnum{}):schemagen.EnumDescriptor{ Type: "integer" Values: []schemagen.EnumValueDescriptor{ schemagen.EnumValueDescriptor{ Name: "STRICT", Value: 1}, schemagen.EnumValueDescriptor{ Name: "PERMISSIVE", Value: 2}, .... } } } json := schemagen.GenerateSchemaWithAllOptions("http://fabric8.io/istio/IstioSchema#", crdLists, typesDescriptors, providedPackages, manualTypeMap, packageMapping, mappingSchema, providedTypes, constraints, interfacesMapping, javaNameStrategyMapping, enumMapping, "io.fabric8") ```
Can one of the admins verify this patch? |
Kudos, SonarCloud Quality Gate passed! |
manusa
approved these changes
Nov 23, 2021
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.
LGTM, thx!
oscerd
approved these changes
Nov 23, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Given the Golang model:
Golang type
PeerAuthentication_MutualTLS
that uses a const:Where the const is:
The resulting Java model now contains an auto generated enum:
Note that all the enumerators are auto discovered when the target type is a String, which means that if we want to achieve the following enumerator:
public enum PeerAuthentication_MutualTLS_Mode {
// Inherit from parent, if has one. Otherwise treated as PERMISSIVE.
UNSET(1),
// Connection is not tunneled.
DISABLE(2),
// Connection can be either plaintext or mTLS tunnel.
PERMISSIVE(3),
// Connection is an mTLS tunnel (TLS with client cert must be presented).
STRICT(4);
}
Fix #3592