Skip to content

Commit

Permalink
adding updated version of omit
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Mar 1, 2017
1 parent 720367f commit 50613a4
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions system/api/omit.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package api

import (
"fmt"
"log"

"github.com/ponzu-cms/ponzu/system/item"

"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

Expand All @@ -15,23 +17,25 @@ func omit(it interface{}, data []byte) ([]byte, error) {
return data, nil
}

return omitFields(om, data, "data.0.")
return omitFields(om, data, "data")
}

func omitFields(om item.Omittable, data []byte, pathPrefix string) ([]byte, error) {
// get fields to omit from json data
fields := om.Omit()

// remove each field from json, all responses contain json object(s) in top-level "data" array
var omitted = data
for i := range fields {
var err error
omitted, err = sjson.DeleteBytes(omitted, pathPrefix+fields[i])
if err != nil {
log.Println("Erorr omitting field:", fields[i], "from item.Omittable:", om)
return nil, err
n := int(gjson.GetBytes(data, pathPrefix+".#").Int())
for i := 0; i < n; i++ {
for k := range fields {
var err error
data, err = sjson.DeleteBytes(data, fmt.Sprintf("%s.%d.%s", pathPrefix, i, fields[k]))
if err != nil {
log.Println("Erorr omitting field:", fields[k], "from item.Omittable:", om)
return nil, err
}
}
}

return omitted, nil
return data, nil
}

0 comments on commit 50613a4

Please sign in to comment.