Skip to content

Commit

Permalink
Java: Make Panel extensible (#562)
Browse files Browse the repository at this point in the history
* Make Panel extensible

* Update tests

* Skip test for other languages
  • Loading branch information
spinillos authored Sep 30, 2024
1 parent cd35dd6 commit 8597003
Show file tree
Hide file tree
Showing 33 changed files with 313 additions and 28 deletions.
1 change: 1 addition & 0 deletions internal/jennies/golang/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestBuilder_Generate(t *testing.T) {
Name: "GoBuilder",
Skip: map[string]string{
"builder_delegation_in_disjunction": "disjunctions are eliminated with compiler passes",
"dashboard_panel": "this test if for Java generics for dashboard.Panel",
},
}

Expand Down
1 change: 1 addition & 0 deletions internal/jennies/java/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (b Builders) genBuilders(pkg string, name string) ([]Builder, bool) {
Properties: builder.Properties,
Defaults: b.genDefaults(builder.Options),
ImportAlias: b.config.PackagePath,
IsGeneric: builder.For.SelfRef.ReferredPkg == "dashboard" && object.Name == "Panel",
}
}), true
}
Expand Down
8 changes: 4 additions & 4 deletions internal/jennies/java/templates/types/builder.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{- define "builder" }}
public static class {{ .BuilderName }}Builder implements {{ if not (eq .Builder.ImportAlias "") }}{{ .Builder.ImportAlias }}.{{ end }}cog.Builder<{{ .Builder.BuilderSignatureType }}> {
private final {{ .Builder.ObjectName }} internal;
public static class {{ .BuilderName }}Builder{{ if .Builder.IsGeneric }}<T extends {{ .BuilderName }}Builder<T>>{{ end }} implements {{ if not (eq .Builder.ImportAlias "") }}{{ .Builder.ImportAlias }}.{{ end }}cog.Builder<{{ .Builder.BuilderSignatureType }}> {
protected final {{ .Builder.ObjectName }} internal;

{{- range .Builder.Properties }}
private {{ .Type | formatBuilderFieldType }} {{ .Name | escapeVar }};
Expand All @@ -25,11 +25,11 @@
}

{{- range $opt := .Builder.Options }}
public {{ $.BuilderName }}Builder {{ .Name | lowerCamelCase | escapeVar }}({{- template "args" .Args }}) {
public {{ if $.Builder.IsGeneric }}T {{ else }}{{ $.BuilderName }}Builder {{ end }}{{ .Name | lowerCamelCase | escapeVar }}({{- template "args" .Args }}) {
{{- range .Assignments }}
{{- template "assignment" (dict "Assignment" . "BuilderName" $.Builder.BuilderName "OptionName" $opt.Name) }}
{{- end }}
return this;
return {{ if $.Builder.IsGeneric }}(T) {{ end }}this;
}
{{ end -}}

Expand Down
1 change: 1 addition & 0 deletions internal/jennies/java/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ type Builder struct {
Properties []ast.StructField
Options []ast.Option
Defaults []OptionCall
IsGeneric bool
}

type OptionCall struct {
Expand Down
3 changes: 3 additions & 0 deletions internal/jennies/php/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ func TestBuilder_Generate(t *testing.T) {
test := testutils.GoldenFilesTestSuite[languages.Context]{
TestDataRoot: "../../../testdata/jennies/builders",
Name: "PHPBuilder",
Skip: map[string]string{
"dashboard_panel": "this test if for Java generics for dashboard.Panel",
},
}

config := Config{
Expand Down
1 change: 1 addition & 0 deletions internal/jennies/python/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestBuilder_Generate(t *testing.T) {
Name: "PythonBuilder",
Skip: map[string]string{
"anonymous_struct": "Anonymous structs are not supported in Python",
"dashboard_panel": "this test if for Java generics for dashboard.Panel",
},
}

Expand Down
3 changes: 3 additions & 0 deletions internal/jennies/typescript/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ func TestBuilder_Generate(t *testing.T) {
test := testutils.GoldenFilesTestSuite[languages.Context]{
TestDataRoot: "../../../testdata/jennies/builders",
Name: "TypescriptBuilder",
Skip: map[string]string{
"dashboard_panel": "this test if for Java generics for dashboard.Panel",
},
}

language := New(Config{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<SomeStruct> {
private final SomeStruct internal;
protected final SomeStruct internal;

public Builder() {
this.internal = new SomeStruct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<SomeStruct> {
private final SomeStruct internal;
protected final SomeStruct internal;

public Builder() {
this.internal = new SomeStruct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<SomeStruct> {
private final SomeStruct internal;
protected final SomeStruct internal;

public Builder() {
this.internal = new SomeStruct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<SomeStruct> {
private final SomeStruct internal;
protected final SomeStruct internal;

public Builder() {
this.internal = new SomeStruct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<Dashboard> {
private final Dashboard internal;
protected final Dashboard internal;

public Builder() {
this.internal = new Dashboard();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<DashboardLink> {
private final DashboardLink internal;
protected final DashboardLink internal;

public Builder() {
this.internal = new DashboardLink();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<Dashboard> {
private final Dashboard internal;
protected final Dashboard internal;

public Builder() {
this.internal = new Dashboard();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<DashboardLink> {
private final DashboardLink internal;
protected final DashboardLink internal;

public Builder() {
this.internal = new DashboardLink();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<ExternalLink> {
private final ExternalLink internal;
protected final ExternalLink internal;

public Builder() {
this.internal = new ExternalLink();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<Dashboard> {
private final Dashboard internal;
protected final Dashboard internal;

public Builder() {
this.internal = new Dashboard();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<SomeStruct> {
private final SomeStruct internal;
protected final SomeStruct internal;

public Builder() {
this.internal = new SomeStruct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<SomeStruct> {
private final SomeStruct internal;
protected final SomeStruct internal;

public Builder() {
this.internal = new SomeStruct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<SomeStruct> {
private final SomeStruct internal;
protected final SomeStruct internal;

public Builder(String title) {
this.internal = new SomeStruct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public String toJSON() throws JsonProcessingException {


public static class Builder implements cog.Builder<SomePanel> {
private final SomePanel internal;
protected final SomePanel internal;

public Builder() {
this.internal = new SomePanel();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dashboard;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

public class Panel {
@JsonProperty("onlyFromThisDashboard")
public Boolean onlyFromThisDashboard;

public String toJSON() throws JsonProcessingException {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
return ow.writeValueAsString(this);
}


public static class Builder<T extends Builder<T>> implements cog.Builder<Panel> {
protected final Panel internal;

public Builder() {
this.internal = new Panel();
this.onlyFromThisDashboard(false);
}
public T onlyFromThisDashboard(Boolean onlyFromThisDashboard) {
this.internal.onlyFromThisDashboard = onlyFromThisDashboard;
return (T) this;
}
public Panel build() {
return this.internal;
}
}
}
Loading

0 comments on commit 8597003

Please sign in to comment.