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

Support enums in generator #3602

Merged
merged 1 commit into from
Nov 23, 2021
Merged

Support enums in generator #3602

merged 1 commit into from
Nov 23, 2021

Conversation

Sgitario
Copy link
Contributor

Given the Golang model:

Golang type PeerAuthentication_MutualTLS that uses a const:

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:

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;
    // ...
}
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")

Fix #3592

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")
```
@centos-ci
Copy link

Can one of the admins verify this patch?

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Copy link
Member

@manusa manusa left a comment

Choose a reason for hiding this comment

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

LGTM, thx!

@manusa manusa added this to the 5.11.0 milestone Nov 23, 2021
@manusa manusa requested a review from oscerd November 23, 2021 11:51
@manusa manusa merged commit de44680 into fabric8io:master Nov 23, 2021
@Sgitario Sgitario deleted the autogen_enum branch November 23, 2021 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Golang generator does not support Enumerators
4 participants