fieldmask.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package runtime
  2. import (
  3. "encoding/json"
  4. "io"
  5. "strings"
  6. "github.com/golang/protobuf/protoc-gen-go/generator"
  7. "google.golang.org/genproto/protobuf/field_mask"
  8. )
  9. // FieldMaskFromRequestBody creates a FieldMask printing all complete paths from the JSON body.
  10. func FieldMaskFromRequestBody(r io.Reader) (*field_mask.FieldMask, error) {
  11. fm := &field_mask.FieldMask{}
  12. var root interface{}
  13. if err := json.NewDecoder(r).Decode(&root); err != nil {
  14. if err == io.EOF {
  15. return fm, nil
  16. }
  17. return nil, err
  18. }
  19. queue := []fieldMaskPathItem{{node: root}}
  20. for len(queue) > 0 {
  21. // dequeue an item
  22. item := queue[0]
  23. queue = queue[1:]
  24. if m, ok := item.node.(map[string]interface{}); ok {
  25. // if the item is an object, then enqueue all of its children
  26. for k, v := range m {
  27. queue = append(queue, fieldMaskPathItem{path: append(item.path, generator.CamelCase(k)), node: v})
  28. }
  29. } else if len(item.path) > 0 {
  30. // otherwise, it's a leaf node so print its path
  31. fm.Paths = append(fm.Paths, strings.Join(item.path, "."))
  32. }
  33. }
  34. return fm, nil
  35. }
  36. // fieldMaskPathItem stores a in-progress deconstruction of a path for a fieldmask
  37. type fieldMaskPathItem struct {
  38. // the list of prior fields leading up to node
  39. path []string
  40. // a generic decoded json object the current item to inspect for further path extraction
  41. node interface{}
  42. }
  43. // CamelCaseFieldMask updates the given FieldMask by converting all of its paths to CamelCase, using the same heuristic
  44. // that's used for naming protobuf fields in Go.
  45. func CamelCaseFieldMask(mask *field_mask.FieldMask) {
  46. if mask == nil || mask.Paths == nil {
  47. return
  48. }
  49. var newPaths []string
  50. for _, path := range mask.Paths {
  51. lowerCasedParts := strings.Split(path, ".")
  52. var camelCasedParts []string
  53. for _, part := range lowerCasedParts {
  54. camelCasedParts = append(camelCasedParts, generator.CamelCase(part))
  55. }
  56. newPaths = append(newPaths, strings.Join(camelCasedParts, "."))
  57. }
  58. mask.Paths = newPaths
  59. }