Skip to content

Commit

Permalink
api: use pointers in models to get proper omitempty semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Feb 24, 2016
1 parent 1a5aa88 commit 04c7351
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions api/v1/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type Error struct {
}

type LayerEnvelope struct {
Layer Layer `json:"Layer,omitempty"`
Error Error `json:"Error,omitempty"`
Layer *Layer `json:"Layer,omitempty"`
Error *Error `json:"Error,omitempty"`
}

type Layer struct {
Expand Down Expand Up @@ -63,6 +63,6 @@ type Notification struct {
}

type VulnerabilityWithLayers struct {
Vulnerability Vulnerability `json:"Vulnerability,omitempty"`
LayersIntroducingVulnerability []string `json:"LayersIntroducingVulnerability,omitempty"`
Vulnerability *Vulnerability `json:"Vulnerability,omitempty"`
LayersIntroducingVulnerability []string `json:"LayersIntroducingVulnerability,omitempty"`
}
12 changes: 6 additions & 6 deletions api/v1/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx
request := LayerEnvelope{}
err := decodeJSON(r, &request)
if err != nil {
writeResponse(w, LayerEnvelope{Error: Error{err.Error()}})
writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest)
}

err = worker.Process(ctx.Store, request.Layer.Name, request.Layer.ParentName, request.Layer.Path, request.Layer.Format)
if err != nil {
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)
}
writeResponse(w, LayerEnvelope{Error: Error{err.Error()}})
writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError)
}

Expand All @@ -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)
if err == cerrors.ErrNotFound {
writeResponse(w, LayerEnvelope{Error: Error{err.Error()}})
writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusNotFound)
} else if err != nil {
writeResponse(w, LayerEnvelope{Error: Error{err.Error()}})
writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError)
}

Expand Down Expand Up @@ -121,7 +121,7 @@ func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *
}

// add envelope
writeResponse(w, LayerEnvelope{Layer: layer})
writeResponse(w, LayerEnvelope{Layer: &layer})
return writeHeader(w, http.StatusOK)
}

Expand Down

0 comments on commit 04c7351

Please sign in to comment.