api: use pointers in models to get proper omitempty semantics

This commit is contained in:
Jimmy Zelinskie 2016-01-29 15:32:54 -05:00
parent 1a5aa88b18
commit 04c7351911
2 changed files with 10 additions and 10 deletions

View File

@ -19,8 +19,8 @@ type Error struct {
} }
type LayerEnvelope struct { type LayerEnvelope struct {
Layer Layer `json:"Layer,omitempty"` Layer *Layer `json:"Layer,omitempty"`
Error Error `json:"Error,omitempty"` Error *Error `json:"Error,omitempty"`
} }
type Layer struct { type Layer struct {
@ -63,6 +63,6 @@ type Notification struct {
} }
type VulnerabilityWithLayers struct { type VulnerabilityWithLayers struct {
Vulnerability Vulnerability `json:"Vulnerability,omitempty"` Vulnerability *Vulnerability `json:"Vulnerability,omitempty"`
LayersIntroducingVulnerability []string `json:"LayersIntroducingVulnerability,omitempty"` LayersIntroducingVulnerability []string `json:"LayersIntroducingVulnerability,omitempty"`
} }

View File

@ -52,17 +52,17 @@ func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx
request := LayerEnvelope{} request := LayerEnvelope{}
err := decodeJSON(r, &request) err := decodeJSON(r, &request)
if err != nil { if err != nil {
writeResponse(w, LayerEnvelope{Error: Error{err.Error()}}) writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest) return writeHeader(w, http.StatusBadRequest)
} }
err = worker.Process(ctx.Store, request.Layer.Name, request.Layer.ParentName, request.Layer.Path, request.Layer.Format) err = worker.Process(ctx.Store, request.Layer.Name, request.Layer.ParentName, request.Layer.Path, request.Layer.Format)
if err != nil { if err != nil {
if _, ok := err.(*cerrors.ErrBadRequest); ok { if _, ok := err.(*cerrors.ErrBadRequest); ok {
writeResponse(w, LayerEnvelope{Error: Error{err.Error()}}) writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest) return writeHeader(w, http.StatusBadRequest)
} }
writeResponse(w, LayerEnvelope{Error: Error{err.Error()}}) writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return writeHeader(w, http.StatusInternalServerError)
} }
@ -75,10 +75,10 @@ func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *
dbLayer, err := ctx.Store.FindLayer(p.ByName("layerName"), withFeatures, withVulnerabilities) dbLayer, err := ctx.Store.FindLayer(p.ByName("layerName"), withFeatures, withVulnerabilities)
if err == cerrors.ErrNotFound { if err == cerrors.ErrNotFound {
writeResponse(w, LayerEnvelope{Error: Error{err.Error()}}) writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusNotFound) return writeHeader(w, http.StatusNotFound)
} else if err != nil { } else if err != nil {
writeResponse(w, LayerEnvelope{Error: Error{err.Error()}}) writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return writeHeader(w, http.StatusInternalServerError)
} }
@ -121,7 +121,7 @@ func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *
} }
// add envelope // add envelope
writeResponse(w, LayerEnvelope{Layer: layer}) writeResponse(w, LayerEnvelope{Layer: &layer})
return writeHeader(w, http.StatusOK) return writeHeader(w, http.StatusOK)
} }