bxcodec / go-clean-arch

Go (Golang) Clean Architecture based on Reading Uncle Bob's Clean Architecture
MIT License
9.22k stars 1.22k forks source link

How do you reference ID's #62

Closed MutazAshhab closed 2 years ago

MutazAshhab commented 2 years ago

In domain/article.go we have

type Article struct {
    ID        int64     `json:"id"`
    Title     string    `json:"title" validate:"required"`
    Content   string    `json:"content" validate:"required"`
    Author    Author    `json:"author"`
    UpdatedAt time.Time `json:"updated_at"`
    CreatedAt time.Time `json:"created_at"`
}

Here an Author attribute exists because an Article belongs to an Author

Going from that logic I attempted to add ArticleStats so I made the following changes:

in domain/article.go

type Article struct {
    .
    .
    .
    ArticleStatistics ArticleStats `json:"article_statistics"`
}

and added

in domain/articlestats.go

type ArticleStats struct {
    ID      int64   `json:"id"`
    Article Article `json:"article"`
    .
    .
    .
}

And I get illegal cycle in declaration of Article + illegal cycle in declaration of ArticleStats

How is this solved?

frederikhors commented 2 years ago

@Taz17 I suggest you to read https://threedots.tech/post/ddd-lite-in-go-introduction/.

MutazAshhab commented 2 years ago

Thank you, for everyone who is wondering what the answer would be its to reference a pointer to Article in ArticleStats

type ArticleStats struct {
    ID       int64   `json:"id"`
    Article *Article `json:"article"`
    .
}