1

I made a MySQL query using GORM inside a little go app.

I have declared my domain struct

type Domain struct {
    gorm.Model
    Name string
    ...
}

Then when I send a query to MySQL with GORM with this method.

func DomainInfos(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    w.WriteHeader(http.StatusOK)

    var d Domain

    config.DbConnection.Where("name = ?", vars["domain"]).Find(&d)

    json.NewEncoder(w).Encode(d)
}

When the domain is not found it return an default object from my struct definition

{
    {0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} 
    0
    0
    0
}

So I wrote a little condition to manually return an empty object

if d.ID == 0 {
    json.NewEncoder(w).Encode(make(map[string]string))
    return
}

Is it possible GORM return an empty object directly when the query return nothing, to avoid this manual checking ?

2 Answers 2

5

GORM is returning an empty object; when it comes to Go values, "empty" and "default" are the same, and are actually called the zero value. In your situation, you're needing to control the JSON output, not the GORM return value.

You can add the omitempty tag to your fields to have them exluded from JSON output if they contain the zero value for their type:

type Domain struct {
    gorm.Model
    Name string `json:",omitempty"`
    ...
}

For each field with this tag, when you call Encode or Marshal, if the field contains its zero value (e.g. for Name, which is a string, if it is equal to ""), the field will not be included in the output. If you tag all the exported fields this way, and they all contain their zero values, the output will be an empty JSON object {}.

Also note that this:

json.NewEncoder(w).Encode(make(map[string]string))

Is equivalent to, but significantly less efficient than:

w.Write([]byte("{}"))

Your other option would be a custom marshal func, something like this:

func (d Domain) MarshalJSON() ([]byte, error) {
    if t.ID == 0 {
        return []byte("{}"), nil
    }

    // Wrap the type to avoid infinite recursion on MarshalJSON
    type dd Domain
    return json.Marshal(dd(d))
}

You can see a working sample here: https://play.golang.org/p/mIRfRKXeyyW

Sign up to request clarification or add additional context in comments.

4 Comments

This is working for Name, ... but it still return the gorm.Model value. I also tried to add json:",omitempty" to gorm.Model without success
If it's not omitting the field, then the field does not contain its type's zero value.
I think it's because gorm.Model extend my struct and add non omitted field.
I've added another option in case the first does not work for your case. It's less intuitive but may help.
1

omitempty does not work on the struct field of type time.Time

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.