0

I am trying to apply the ddd concepts to my go project

and then I have an entity with a nested struct like this:

type Person struct {
    Id       string
    Name     valueObject.Name
    Email    valueObject.Email
    Password valueObject.Password
    Created  time.Time
    Updated  time.Time
}

    func NewPerson(name valueObject.Name, email valueObject.Email, password valueObject.Password) *Person {
        return &Person{
            Id:       "a",
            Name:     name,
            Email:    email,
            Password: password,
            Created:  time.Now(),
            Updated:  time.Now(),
        }
    }

all my object values has a structure similar to this:

type Email struct {
    address string
}

func NewEmail(address string) (Email, error) {
    isValid := valid(address)
    if !isValid {
        return Email{}, ErrEmail
    }
    return Email{address}, nil
}

func (e Email) Value() string {
    return e.address
}

func (e Email) Format() string {
    return strings.TrimSpace(strings.ToLower(e.Value()))
}

func (e Email) Equals(e2 Email) bool {
    return e.Value() == e2.Value()
}

and then as I have a struct that is nested I thought of creating a ToJSON function, to return my data, and I also need to exclude some of the return as a password:

type personJson struct {
    id        string
    name      string
    email     string
    createdAt time.Time
    updatedAt time.Time
}
func (p *Person) ToJSON() personJson {
    email := p.Email.Value()
    name := p.Name.FullName()
    person := personJson{id: "dasdas", email: email, name: name, createdAt: p.Created, updatedAt: p.Updated}
    return person
}

but then in my controller I try to access it, or when I return this ToJSON always comes as {}:

type createPersonDTO struct {
    FirstName string `json:"firstName"`
    LastName  string `json:"lastName"`
    Password  string `json:"password"`
    Email     string `json:"email"`
}

func (*PersonController) CreateNewPerson(ctx *fiber.Ctx) error {
    var dto createPersonDTO
    if err := ctx.BodyParser(&dto); err != nil {
        return utils.CtxError(ctx, http.StatusUnprocessableEntity, err)
    }
    email, err := valueObjects.NewEmail(dto.Email)
    if err != nil {
        return utils.CtxError(ctx, http.StatusUnprocessableEntity, err)
    }
    name, err := valueObjects.NewName(dto.FirstName, dto.LastName)
    if err != nil {
        return utils.CtxError(ctx, http.StatusUnprocessableEntity, err)
    }
    password, err := valueObjects.NewPassword(dto.Password)
    if err != nil {
        return utils.CtxError(ctx, http.StatusUnprocessableEntity, err)
    }
    person := entities.NewPerson(name, email, password)
    return ctx.Status(http.StatusOK).JSON(person.ToJSON())
}

my res.body is always an empty json

1
  • The json marshaller will only export public fields. Uppercase your struct fields' first letter to make them public. Commented Apr 26, 2021 at 0:13

1 Answer 1

2

Uppercase the first letter of each field of the struct you want to be exportable to json.Marshal:

type personJson struct {
    ID        string
    Name      string
    Email     string
    CreatedAt time.Time
    UpdatedAt time.Time
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.