Skip to content

Commit

Permalink
use struct reference
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Mar 10, 2024
1 parent 61fceee commit e4f160d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/Generator/Go.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ protected function writeStruct(Code\Name $name, array $properties, ?string $exte

foreach ($properties as $property) {
/** @var Code\Property $property */
$code.= $this->indent . $property->getName()->getProperty() . ' ' . $property->getType() . ' `json:"' . $property->getName()->getRaw() . '"`' . "\n";
$ref = '';
if ($property->getOrigin() instanceof ReferenceType) {
$ref = '*';
}

$code.= $this->indent . $property->getName()->getProperty() . ' ' . $ref . $property->getType() . ' `json:"' . $property->getName()->getRaw() . '"`' . "\n";
}

$code.= '}' . "\n";
Expand Down
8 changes: 4 additions & 4 deletions tests/Generator/resource/go/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Author struct {
Email string `json:"email"`
Categories []string `json:"categories"`
Locations []Location `json:"locations"`
Origin Location `json:"origin"`
Origin *Location `json:"origin"`
}

type Meta = map[string]string
Expand All @@ -25,7 +25,7 @@ import "time"

// An general news entry
type News struct {
Config Meta `json:"config"`
Config *Meta `json:"config"`
InlineConfig map[string]string `json:"inlineConfig"`
MapTags map[string]string `json:"mapTags"`
MapReceiver map[string]Author `json:"mapReceiver"`
Expand All @@ -36,8 +36,8 @@ type News struct {
ProfileImage []byte `json:"profileImage"`
Read bool `json:"read"`
Source any `json:"source"`
Author Author `json:"author"`
Meta Meta `json:"meta"`
Author *Author `json:"author"`
Meta *Meta `json:"meta"`
SendDate time.Time `json:"sendDate"`
ReadDate time.Time `json:"readDate"`
Expires string `json:"expires"`
Expand Down
2 changes: 1 addition & 1 deletion tests/Generator/resource/go/go_complex.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,6 @@ type TypeSchema struct {
// Represents an union type. An union type can contain one of the provided types
type UnionType struct {
Description string `json:"description"`
Discriminator Discriminator `json:"discriminator"`
Discriminator *Discriminator `json:"discriminator"`
OneOf []any `json:"oneOf"`
}
4 changes: 2 additions & 2 deletions tests/Generator/resource/go/go_import.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type Import struct {
Students StudentMap `json:"students"`
Student Student `json:"student"`
Students *StudentMap `json:"students"`
Student *Student `json:"student"`
}

type MyMap struct {
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/go/go_import_ns.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package Foo.Bar
type Import struct {
Students My.Import.StudentMap `json:"students"`
Student My.Import.Student `json:"student"`
Students *My.Import.StudentMap `json:"students"`
Student *My.Import.Student `json:"student"`
}

package Foo.Bar
Expand Down
2 changes: 1 addition & 1 deletion tests/Generator/resource/go/go_oop.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ type Map[T any] struct {
}

type RootSchema struct {
Students StudentMap `json:"students"`
Students *StudentMap `json:"students"`
}

0 comments on commit e4f160d

Please sign in to comment.