updatemask

package module
v0.0.0-...-85e06d6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

README

GoDoc Go Report Card License codecov Build Status

update_mask

Parse a struct and field mask return map[string]interface{} or JSON

Field mask represents the path to a field inside a message, each '.' separates a sub-field.

paths: "a.b.d"

Would apply to:

{
    "a": {
        "b": {
            "d": "value"
        }
    }
}

Field masks are used for either get or update operations where you only want to return or update a subset of the fields.

If the field mask is empty all fields should be returned.

When you have multiple items with the same path inside a message a field mask will apply to all of them.

paths: "a.b"

Would apply to:

{
    "a": [
        {
            "b": "value"
        },
        {
            "b": "value"
        }
    ]
}

Example

package main
  
import (
        "fmt"
        "log"

        "github.com/mickep76/update_mask"
        "google.golang.org/genproto/protobuf/field_mask"
)

type Location struct {
        City    string `json:"city"`
        Country string `json:"country"`
}

type Contact struct {
        ID       int       `json:"id"`
        Name     string    `json:"name"`
        Age      int       `json:"age"`
        Mother   string    `json:"mother"`
        Father   string    `json:"father"`
        Location *Location `json:"location"`
}

type ContactList struct {
        Page          int        `json:"page"`
        NumberOfPages int        `json:"number_of_page"`
        Contacts      []*Contact `json:"contacts"`
}

var contacts = ContactList{
        Page:          1,
        NumberOfPages: 5,
        Contacts: []*Contact{
                &Contact{
                        ID:     "1",
                        Name:   "Carola",
                        Age:    55,
                        Mother: "Johanna",
                        Father: "John",
                        Location: &Location{
                                City:    "New York",
                                Country: "USA",
                        },
                },
                &Contact{
                        ID:     "2",
                        Name:   "Mark",
                        Age:    33,
                        Mother: "Marianne",
                        Father: "Karl",
                        Location: &Location{
                                City:    "Chicago",
                                Country: "USA",
                        },
                },
        },
}

func main() {
        b, err := updatemask.JSONIndent(contacts, field_mask.FieldMask{
                Paths: []string{
                        "page",
                        "contacts.name",
                        "contacts.id",
                        "contacts.location.city",
                },
        }, "  ")
        if err != nil {
                log.Fatalf("update mask: %v", err)
        }

        fmt.Println(string(b))
}

Output:

{
  "contacts": [
    {
      "id": 1,
      "location": {
        "city": "New York"
      },
      "name": "Carola"
    },
    {
      "id": 2,
      "location": {
        "city": "Chicago"
      },
      "name": "Mark"
    }
  ],
  "page": 1
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSON

func JSON(v interface{}, um field_mask.FieldMask) ([]byte, error)

JSON helper function to return JSON from a struct and field mask.

func JSONIndent

func JSONIndent(v interface{}, um field_mask.FieldMask, indent string) ([]byte, error)

JSONIndent helper function to return indented JSON from a struct and field mask.

func MapStringInterface

func MapStringInterface(v interface{}, um field_mask.FieldMask) (interface{}, error)

MapStringInterface return map[string]interface from a struct and field mask.

Field mask represents the path to a field inside a message, each '.' separates a sub-field.

paths: "a.b.d"

Would apply to:

{
    "a": {
        "b": {
            "d": "value"
        }
    }
}

Field masks are used for either get or update operations where you only want to return or update a subset of the fields.

If the field mask is empty all fields should be returned.

When you have multiple items with the same path inside a message a field mask will apply to all of them.

paths: "a.b"

Would apply to:

{
    "a": [
        {
            "b": "value"
        },
        {
            "b": "value"
        }
    ]
}

Specifying a slice or map will include all sub-fields.

paths: "a"

Would apply to all fields.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL