|
| 1 | +# Task |
| 2 | + |
| 3 | +Convert the selected Java source into idiomatic Go **with minimal semantic drift**. |
| 4 | + |
| 5 | +- Map packages to directories and package names. |
| 6 | +- Map classes (including abstract) and their fields/methods to Go **interface** and **unexported implementation structs**. |
| 7 | +- Preserve logic while adapting to Go naming, encapsulation and error handling. |
| 8 | +- Keep diffs focused: one Java file -> one Go file (small support code if strictly necessary). |
| 9 | + |
| 10 | +## Inputs |
| 11 | + |
| 12 | +- Java file(s) to convert. |
| 13 | +- Any related types the file depends on. |
| 14 | +- Package path target within the Go module. |
| 15 | + |
| 16 | +## Outputs |
| 17 | + |
| 18 | +- A new Go file with idiomatic code. |
| 19 | + |
| 20 | +## Package & File Mapping |
| 21 | + |
| 22 | +1. **One-to-one element mapping** |
| 23 | + - **Files**: Map each Java source file `<Name>.java` to to one go file, named `lowercase-with-dashes.go` (e.g., `FooBar.java` -> `foo-bar.go`). Be consistent within the package. |
| 24 | + - **Classes/Interfaces**: For each Java class or interface define a **Go interface** named after the Java type (exported) and an **unexported implementation struct**. |
| 25 | + - **Methods**: Map each java method to a Go method or function. For overloads, pick distinct names (e.g., `Advance`, `AdvanceN`). |
| 26 | + |
| 27 | +2. **Package structure** |
| 28 | + - Java: `package com.example.foo.something;` |
| 29 | + - Go: directory `foo/something` with `package something` in the file. |
| 30 | + |
| 31 | +## Types & Encapsulation |
| 32 | + |
| 33 | +### Classes -> Interface + Impl Struct |
| 34 | + |
| 35 | +- Define an **interface** named exactly after the Java class, e.g. `Foo`. |
| 36 | +- Define a constructor `NewFoo(...) Foo` returning the interface. |
| 37 | +- Define an **unexported** struct `fooImpl` implements the interface. Prefer composition/embedding for reuse. |
| 38 | + |
| 39 | +### Fields -> Struct Fields |
| 40 | + |
| 41 | +- Private/encapsulated state lives in unexported struct fields (e.g., `bar int`). |
| 42 | +- Provide getters/setters as interface methods only if the Java API requires them to be public. Avoid exporting fields directly unless they are immutable configuration. |
| 43 | + |
| 44 | +### Constructors |
| 45 | + |
| 46 | +For each Java public constructor: |
| 47 | + |
| 48 | +```go |
| 49 | +func NewFoo(params) Foo { |
| 50 | + return &fooImpl{/* initialize fields */} |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +- If multiple Java constructors exist, use either distinct names (`NewFooWithParams`) **or** the functional options pattern for optional params (avoid overloading). |
| 55 | + |
| 56 | +### Methods & Receivers |
| 57 | + |
| 58 | +- **Non-mutating** -> value receiver if the struct is small and the method is read-only. |
| 59 | +- **Mutating** -> pointer receiver. |
| 60 | +- Prefer returning `(T, error)` over panicking; translate Java exceptions to `error` values when they cross API boundaries. |
| 61 | + |
| 62 | +### Abstract classes |
| 63 | + |
| 64 | +```go |
| 65 | +type Foo interface { |
| 66 | + // abstract methods + requires accessors |
| 67 | +} |
| 68 | + |
| 69 | +type fooBase struct {/* shared fields */} |
| 70 | + |
| 71 | +func (b fooBase) GetX() T { return b.x } |
| 72 | + |
| 73 | +type fooImpl struct { |
| 74 | + fooBase |
| 75 | +} |
| 76 | +func NewFoo(params) Foo { |
| 77 | + return &fooImpl{ |
| 78 | + fooBase: fooBase{/* initialize shared fields */}, |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### Method overloading |
| 84 | + |
| 85 | +Java: |
| 86 | + |
| 87 | +```java |
| 88 | +void advance(); |
| 89 | +void advance(int n); |
| 90 | +``` |
| 91 | + |
| 92 | +Go: |
| 93 | + |
| 94 | +```go |
| 95 | +func (r *readerImpl) Advance() {} |
| 96 | +func (r *readerImpl) AdvanceN(n int) {} |
| 97 | +``` |
| 98 | + |
| 99 | +### Equality / Hashing |
| 100 | + |
| 101 | +- If Java only overrides `equals()`/`hashCode()`, in Go prefer: |
| 102 | + - Use direct `==` for comparable structs; or |
| 103 | + - Provide an explicit **lookup key**: |
| 104 | + |
| 105 | + ```go |
| 106 | + type FooLookupKey struct { Bar int; Baz string } |
| 107 | + func (f *fooImpl) FooLookupKey() FooLookupKey { return FooLookupKey{f.bar, f.baz} } |
| 108 | + ``` |
| 109 | + |
| 110 | +## Naming & Comments |
| 111 | + |
| 112 | +- **Packages:** short, lowercase, single word (`something`). |
| 113 | +- **Exports:** capitalize to export. Keep names concise and avoid stutter (prefer `something.Reader` with type name `Reader`). |
| 114 | + |
| 115 | +## Error Handling |
| 116 | + |
| 117 | +- Always return `(T, error)` for fallible operations. Don't use `panic` for normal control flow. |
| 118 | +- Wrap lower-level errors with context using `fmt.Errorf("op: %w", err)` so callers can use `errors.Is/As`. |
| 119 | +- Match errors with `errors.Is` (sentinels) or `errors.As` (typed errors with additional context). |
| 120 | +
|
| 121 | +### Java exceptions -> Go typed errors |
| 122 | +
|
| 123 | +- Define Java-specific exceptions as typed errros in `common/errors/errors.go` (package `errors`). |
| 124 | +
|
| 125 | +```go |
| 126 | +package errors |
| 127 | +
|
| 128 | +import "fmt" |
| 129 | +
|
| 130 | +type IndexOutOfBoundsError struct { |
| 131 | + index int |
| 132 | + length int |
| 133 | +} |
| 134 | +
|
| 135 | +func (e IndexOutOfBoundsError) Error() string { |
| 136 | + return fmt.Sprintf("Index %d out of bounds for length %d", e.index, e.length) |
| 137 | +} |
| 138 | +
|
| 139 | +func (e IndexOutOfBoundsError) GetIndex() int { return e.index } |
| 140 | +func (e IndexOutOfBoundsError) GetLength() int { return e.length } |
| 141 | +``` |
| 142 | +
|
| 143 | +## Generics |
| 144 | +
|
| 145 | +- Map Java generics to Go generics when needed. |
| 146 | +
|
| 147 | +## Guardrails & Non goals |
| 148 | +
|
| 149 | +- **Do no** add file headers or license comments. |
| 150 | +- **Do not** introduce new public APIs unless requires by the Java surface. |
| 151 | +- **Do not** add comments unless the Java source has them. |
0 commit comments