You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: cloudservice.go
    path of file: ./repos/juju/state
    
    the code of the file until where you have to start completion: // Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package state

import (
	"github.com/juju/errors"
	"github.com/juju/mgo/v3"
	"github.com/juju/mgo/v3/bson"
	"github.com/juju/mgo/v3/txn"

	"github.com/juju/juju/core/network"
)

// CloudServicer represents the state of a CAAS service.
type CloudServicer interface {
	// ProviderId returns the id assigned to the service
	// by the cloud.
	ProviderId() string

	// Addresses returns the service addresses.
	Addresses() network.SpaceAddresses

	// Generation returns the service config generation.
	Generation() int64

	// DesiredScaleProtected indicates if current desired scale in application has been applied to the cluster.
	DesiredScaleProtected() bool
}

// CloudService is an implementation of CloudService.
type CloudService struct {
	st  *State
	doc cloudServiceDoc
}

type cloudServiceDoc struct {
	// DocID holds cloud service document key.
	DocID string `bson:"_id"`

	ProviderId string    `bson:"provider-id"`
	Addresses  []address `bson:"addresses"`

	//
    
func buildCloudServiceOps(st *State, doc cloudServiceDoc) ([]txn.Op, error) {
	svc := newCloudService(st, &doc)
	existing, err := svc.cloudServiceDoc()
	if err != nil && !errors.IsNotFound(err) {
		return nil, errors.Trace(err)
	}
	if err != nil || existing == nil {
		return []txn.Op{{
			C:      cloudServicesC,
			Id:     doc.DocID,
			Assert: txn.DocMissing,
			Insert: doc,
		}}, nil
	}
	patchFields := bson.D{}
	addField := func(elm bson.DocElem) {
		patchFields = append(patchFields, elm)
	}
	if doc.ProviderId != "" {
		addField(bson.DocElem{"provider-id", doc.ProviderId})
	}
	if len(doc.Addresses) > 0 {
		addField(bson.DocElem{"addresses", doc.Addresses})
	}
	if doc.Generation > existing.Generation {
		addField(bson.DocElem{"generation", doc.Generation})
	}
	if doc.DesiredScaleProtected != existing.DesiredScaleProtected {
		addField(bson.DocElem{"desired-scale-protected", doc.DesiredScaleProtected})
	}
	return []txn.Op{{
		C:  cloudServicesC,
		Id: existing.DocID,
		Assert: bson.D{{"$or", []bson.D{
			{{"provider-id", existing.ProviderId}},
			{{"provider-id", bson.D{{"$exists", false}}}},
		}}},
		Update: bson.D{
			{"$set", patchFields},
		},
	}}, nil
}