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: remoteentities.go
    path of file: ./repos/juju/state
    
    the code of the file until where you have to start completion: // Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package state

import (
	"encoding/json"

	"github.com/juju/errors"
	"github.com/juju/mgo/v3"
	"github.com/juju/mgo/v3/bson"
	"github.com/juju/mgo/v3/txn"
	"github.com/juju/names/v5"
	jujutxn "github.com/juju/txn/v3"
	"github.com/juju/utils/v3"
	"gopkg.in/macaroon.v2"
)

// RemoteEntity defines a remote entity that has a unique opaque token that
// identifies the entity within the model.
type RemoteEntity struct {
	docID    string
	token    string
	macaroon string
}

// ID returns th
    
func (r *RemoteEntities) SaveMacaroon(entity names.Tag, mac *macaroon.Macaroon) error {
	var macJSON string
	if mac != nil {
		b, err := json.Marshal(mac)
		if err != nil {
			return errors.Trace(err)
		}
		macJSON = string(b)
	}
	buildTxn := func(attempt int) ([]txn.Op, error) {
		if attempt > 0 {
			// The entity may have been removed; if so,
			// return a not found error.
			_, err := r.GetToken(entity)
			if err == nil {
				return nil, errors.Errorf("unexpected entity %q", entity)
			}
			return nil, errors.Trace(err)
		}
		ops := []txn.Op{{
			C:      remoteEntitiesC,
			Id:     entity.String(),
			Assert: txn.DocExists,
			Update: bson.D{
				{"$set", bson.D{{"macaroon", macJSON}}},
			},
		}}
		return ops, nil
	}
	return r.st.db().Run(buildTxn)
}