Skip to content

Commit

Permalink
add getCompactClass and getTypeName methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kutluhanmetin committed Sep 26, 2023
1 parent 049a4a3 commit 6806991
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 4 deletions.
14 changes: 10 additions & 4 deletions base/commands/serializer/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"text/template"

"github.com/hazelcast/hazelcast-commandline-client/internal/check"
"github.com/hazelcast/hazelcast-go-client/types"
)

const (
Expand All @@ -18,6 +19,9 @@ const (
float64Type = "float64"
)

// UUIDGenFunc is a variable, because we are overriding its behaviour to return a constant UUID during the tests.
var UUIDGenFunc = types.NewUUID

var indent4 = strings.Repeat(" ", 4)
var indent8 = strings.Repeat(" ", 8)
var indent12 = strings.Repeat(" ", 12)
Expand Down Expand Up @@ -75,8 +79,9 @@ type codeTemplate struct {
}

type classSchema struct {
Class Class
Schema Schema
Class Class
Schema Schema
TypeName string
}

func GenerateClass(cls Class, sch Schema, w io.Writer) error {
Expand All @@ -103,8 +108,9 @@ func GenerateClass(cls Class, sch Schema, w io.Writer) error {
tmpl = template.Must(tmpl, err)
}
err = tmpl.Execute(w, classSchema{
Class: cls,
Schema: sch,
Class: cls,
Schema: sch,
TypeName: UUIDGenFunc().String(),
})
return err
}
Expand Down
10 changes: 10 additions & 0 deletions base/commands/serializer/java_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ const javaCompactSerializerTemplate = `public static final class Serializer impl
public void write(@Nonnull CompactWriter writer, @Nonnull {{ .Class.Name }} object) {
{{range $field := .Class.Fields}} writer.write{{methodName (toJavaType $field.Type) $field.Type }}("{{ $field.Name }}", object.{{ $field.Name }});
{{end}} }
@Override
public Class<{{ .Class.Name }}> getCompactClass() {
return {{ .Class.Name }}.class;
}
@Override
public String getTypeName() {
return "{{ .TypeName }}";
}
};
public static final CompactSerializer<{{ .Class.Name }}> HZ_COMPACT_SERIALIZER = new Serializer();`
Expand Down
7 changes: 7 additions & 0 deletions base/commands/serializer/serializer_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
package serializer

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/hazelcast/hazelcast-go-client/types"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -37,6 +39,11 @@ var (
)

func init() {
UUIDGenFunc = func() types.UUID {
return types.NewUUIDWith(10, 10)
}
s := types.NewUUIDWith(10, 10).String()
fmt.Println(s)
generationTestFilesDir := filepath.Join("testdata", "generationTestFiles")
generationTestFilesSchemaDir := filepath.Join("testdata", "generationTestFiles", "schema")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ public void write(@Nonnull CompactWriter writer, @Nonnull AllTypes object) {
writer.writeCompact("mcompact", object.mcompact);
writer.writeArrayOfCompact("mcompactArray", object.mcompactArray);
}

@Override
public Class<AllTypes> getCompactClass() {
return AllTypes.class;
}

@Override
public String getTypeName() {
return "00000000-0000-000a-0000-00000000000a";
}
};

public static final CompactSerializer<AllTypes> HZ_COMPACT_SERIALIZER = new Serializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public void write(@Nonnull CompactWriter writer, @Nonnull Classroom object) {
writer.writeInt32("id", object.id);
writer.writeArrayOfCompact("students", object.students);
}

@Override
public Class<Classroom> getCompactClass() {
return Classroom.class;
}

@Override
public String getTypeName() {
return "00000000-0000-000a-0000-00000000000a";
}
};

public static final CompactSerializer<Classroom> HZ_COMPACT_SERIALIZER = new Serializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public void write(@Nonnull CompactWriter writer, @Nonnull Example1 object) {
writer.writeCompact("example", object.example);
writer.writeArrayOfCompact("examples", object.examples);
}

@Override
public Class<Example1> getCompactClass() {
return Example1.class;
}

@Override
public String getTypeName() {
return "00000000-0000-000a-0000-00000000000a";
}
};

public static final CompactSerializer<Example1> HZ_COMPACT_SERIALIZER = new Serializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public Example2 read(@Nonnull CompactReader reader) {
public void write(@Nonnull CompactWriter writer, @Nonnull Example2 object) {
writer.writeInt32("foo", object.foo);
}

@Override
public Class<Example2> getCompactClass() {
return Example2.class;
}

@Override
public String getTypeName() {
return "00000000-0000-000a-0000-00000000000a";
}
};

public static final CompactSerializer<Example2> HZ_COMPACT_SERIALIZER = new Serializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public Example3 read(@Nonnull CompactReader reader) {
public void write(@Nonnull CompactWriter writer, @Nonnull Example3 object) {
writer.writeInt64("bar", object.bar);
}

@Override
public Class<Example3> getCompactClass() {
return Example3.class;
}

@Override
public String getTypeName() {
return "00000000-0000-000a-0000-00000000000a";
}
};

public static final CompactSerializer<Example3> HZ_COMPACT_SERIALIZER = new Serializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public void write(@Nonnull CompactWriter writer, @Nonnull External object) {
writer.writeBoolean("foo", object.foo);
writer.writeCompact("bar", object.bar);
}

@Override
public Class<External> getCompactClass() {
return External.class;
}

@Override
public String getTypeName() {
return "00000000-0000-000a-0000-00000000000a";
}
};

public static final CompactSerializer<External> HZ_COMPACT_SERIALIZER = new Serializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public void write(@Nonnull CompactWriter writer, @Nonnull NestedCompact object)
writer.writeString("foo", object.foo);
writer.writeInt32("bar", object.bar);
}

@Override
public Class<NestedCompact> getCompactClass() {
return NestedCompact.class;
}

@Override
public String getTypeName() {
return "00000000-0000-000a-0000-00000000000a";
}
};

public static final CompactSerializer<NestedCompact> HZ_COMPACT_SERIALIZER = new Serializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public void write(@Nonnull CompactWriter writer, @Nonnull NoNamespace object) {
writer.writeBoolean("foo", object.foo);
writer.writeCompact("bar", object.bar);
}

@Override
public Class<NoNamespace> getCompactClass() {
return NoNamespace.class;
}

@Override
public String getTypeName() {
return "00000000-0000-000a-0000-00000000000a";
}
};

public static final CompactSerializer<NoNamespace> HZ_COMPACT_SERIALIZER = new Serializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public NoNamespaceNested read(@Nonnull CompactReader reader) {
public void write(@Nonnull CompactWriter writer, @Nonnull NoNamespaceNested object) {
writer.writeBoolean("baz", object.baz);
}

@Override
public Class<NoNamespaceNested> getCompactClass() {
return NoNamespaceNested.class;
}

@Override
public String getTypeName() {
return "00000000-0000-000a-0000-00000000000a";
}
};

public static final CompactSerializer<NoNamespaceNested> HZ_COMPACT_SERIALIZER = new Serializer();
Expand Down
10 changes: 10 additions & 0 deletions base/commands/serializer/testdata/generationTestFiles/School.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public void write(@Nonnull CompactWriter writer, @Nonnull School object) {
writer.writeInt32("id", object.id);
writer.writeArrayOfCompact("classrooms", object.classrooms);
}

@Override
public Class<School> getCompactClass() {
return School.class;
}

@Override
public String getTypeName() {
return "00000000-0000-000a-0000-00000000000a";
}
};

public static final CompactSerializer<School> HZ_COMPACT_SERIALIZER = new Serializer();
Expand Down
10 changes: 10 additions & 0 deletions base/commands/serializer/testdata/generationTestFiles/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public void write(@Nonnull CompactWriter writer, @Nonnull Student object) {
writer.writeString("name", object.name);
writer.writeInt16("number", object.number);
}

@Override
public Class<Student> getCompactClass() {
return Student.class;
}

@Override
public String getTypeName() {
return "00000000-0000-000a-0000-00000000000a";
}
};

public static final CompactSerializer<Student> HZ_COMPACT_SERIALIZER = new Serializer();
Expand Down

0 comments on commit 6806991

Please sign in to comment.