Use a custom type on the client for Firestore documents

Use a custom type on the client for Firestore documents

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


// City represents a city.
type City struct {
	Name       string   `firestore:"name,omitempty"`
	State      string   `firestore:"state,omitempty"`
	Country    string   `firestore:"country,omitempty"`
	Capital    bool     `firestore:"capital,omitempty"`
	Population int64    `firestore:"population,omitempty"`
	Regions    []string `firestore:"regions,omitempty"`


public City() {
  // Must have a public no-argument constructor

// Initialize all fields of a city
public City(String name, String state, String country,
            Boolean capital, Long population, List<String> regions) { = name;
  this.state = state; = country; = capital;
  this.population = population;
  this.regions = regions;


class City(object):
    def __init__(self, name, state, country, capital=False, population=0,
                 regions=[]): = name
        self.state = state = country = capital
        self.population = population
        self.regions = regions

    def from_dict(source):
        # ...

    def to_dict(self):
        # ...

    def __repr__(self):
                name={}, \
                country={}, \
                population={self.population}, \
                capital={}, \

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser