Search code examples
gogo-gorm

Gorm return empty object instead default object when value is not found


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 ?


Solution

  • 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