本文整理汇总了Golang中github.com/couchbaselabs/sync_gateway/channels.SetOf函数的典型用法代码示例。如果您正苦于以下问题:Golang SetOf函数的具体用法?Golang SetOf怎么用?Golang SetOf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SetOf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestAccessFunction
func TestAccessFunction(t *testing.T) {
db := setupTestDB(t)
defer tearDownTestDB(t, db)
authenticator := auth.NewAuthenticator(db.Bucket, db)
var err error
db.ChannelMapper, err = channels.NewChannelMapper(`function(doc){access(doc.users,doc.userChannels);}`)
assertNoError(t, err, "Couldn't create channel mapper")
user, _ := authenticator.NewUser("naomi", "letmein", channels.SetOf("Netflix"))
user.SetRoleNames([]string{"animefan", "tumblr"})
authenticator.Save(user)
body := Body{"users": []string{"naomi"}, "userChannels": []string{"Hulu"}}
_, err = db.Put("doc1", body)
assertNoError(t, err, "")
body = Body{"users": []string{"role:animefan"}, "userChannels": []string{"CrunchyRoll"}}
_, err = db.Put("doc2", body)
assertNoError(t, err, "")
// Create the role _after_ creating the documents, to make sure the previously-indexed access
// privileges are applied.
role, _ := authenticator.NewRole("animefan", nil)
authenticator.Save(role)
user, _ = authenticator.GetUser("naomi")
expected := channels.SetOf("Hulu", "Netflix").AtSequence(1)
assert.DeepEquals(t, user.Channels(), expected)
expected.AddChannel("CrunchyRoll", 2)
assert.DeepEquals(t, user.InheritedChannels(), expected)
}
示例2: TestRebuildRoleChannels
func TestRebuildRoleChannels(t *testing.T) {
computer := mockComputer{channels: ch.AtSequence(ch.SetOf("derived1", "derived2"), 1)}
auth := NewAuthenticator(gTestBucket, &computer)
role, _ := auth.NewRole("testRole", ch.SetOf("explicit1"))
err := auth.InvalidateChannels(role)
assert.Equals(t, err, nil)
role2, err := auth.GetRole("testRole")
assert.Equals(t, err, nil)
assert.DeepEquals(t, role2.Channels(), ch.AtSequence(ch.SetOf("explicit1", "derived1", "derived2"), 1))
}
示例3: TestRebuildUserChannels
func TestRebuildUserChannels(t *testing.T) {
computer := mockComputer{channels: ch.AtSequence(ch.SetOf("derived1", "derived2"), 1)}
auth := NewAuthenticator(gTestBucket, &computer)
user, _ := auth.NewUser("testUser", "password", ch.SetOf("explicit1"))
user.setChannels(nil)
err := auth.Save(user)
assert.Equals(t, err, nil)
//
user2, err := auth.GetUser("testUser")
assert.Equals(t, err, nil)
log.Printf("Channels = %s", user2.Channels())
assert.DeepEquals(t, user2.Channels(), ch.AtSequence(ch.SetOf("explicit1", "derived1", "derived2"), 1))
}
示例4: TestLogin
func TestLogin(t *testing.T) {
var rt restTester
a := auth.NewAuthenticator(rt.bucket(), nil)
user, err := a.GetUser("")
assert.Equals(t, err, nil)
user.SetDisabled(true)
err = a.Save(user)
assert.Equals(t, err, nil)
user, err = a.GetUser("")
assert.Equals(t, err, nil)
assert.True(t, user.Disabled())
response := rt.sendRequest("PUT", "/db/doc", `{"hi": "there"}`)
assertStatus(t, response, 401)
user, err = a.NewUser("pupshaw", "letmein", channels.SetOf("*"))
a.Save(user)
assertStatus(t, rt.sendRequest("GET", "/db/_session", ""), 200)
response = rt.sendRequest("POST", "/db/_session", `{"name":"pupshaw", "password":"letmein"}`)
assertStatus(t, response, 200)
log.Printf("Set-Cookie: %s", response.Header().Get("Set-Cookie"))
assert.True(t, response.Header().Get("Set-Cookie") != "")
}
示例5: updateChannels
// Updates the Channels property of a document object with current & past channels.
// Returns the set of channels that have changed (document joined or left in this revision)
func (doc *document) updateChannels(newChannels base.Set) (changedChannels base.Set) {
var changed []string
oldChannels := doc.Channels
if oldChannels == nil {
oldChannels = ChannelMap{}
doc.Channels = oldChannels
} else {
// Mark every no-longer-current channel as unsubscribed:
curSequence := doc.Sequence
for channel, removal := range oldChannels {
if removal == nil && !newChannels.Contains(channel) {
oldChannels[channel] = &ChannelRemoval{
Seq: curSequence,
RevID: doc.CurrentRev,
Deleted: doc.Deleted}
changed = append(changed, channel)
}
}
}
// Mark every current channel as subscribed:
for channel, _ := range newChannels {
if value, exists := oldChannels[channel]; value != nil || !exists {
oldChannels[channel] = nil
changed = append(changed, channel)
}
}
if changed != nil {
base.LogTo("CRUD", "\tDoc %q in channels %q", doc.ID, newChannels)
changedChannels = channels.SetOf(changed...)
}
return
}
示例6: defaultGuestUser
func (auth *Authenticator) defaultGuestUser() User {
user := &userImpl{
roleImpl: roleImpl{
ExplicitChannels_: ch.SetOf("*").AtSequence(1),
},
auth: auth,
}
user.Channels_ = user.ExplicitChannels_.Copy()
return user
}
示例7: TestSaveRoles
func TestSaveRoles(t *testing.T) {
auth := NewAuthenticator(gTestBucket, nil)
role, _ := auth.NewRole("testRole", ch.SetOf("test"))
err := auth.Save(role)
assert.Equals(t, err, nil)
role2, err := auth.GetRole("testRole")
assert.Equals(t, err, nil)
assert.DeepEquals(t, role2, role)
}
示例8: TestSaveUsers
func TestSaveUsers(t *testing.T) {
auth := NewAuthenticator(gTestBucket, nil)
user, _ := auth.NewUser("testUser", "password", ch.SetOf("test"))
err := auth.Save(user)
assert.Equals(t, err, nil)
user2, err := auth.GetUser("testUser")
assert.Equals(t, err, nil)
assert.DeepEquals(t, user2, user)
}
示例9: changesFeed
// Creates a Go-channel of all the changes made on a channel.
// Does NOT handle the Wait option. Does NOT check authorization.
func (db *Database) changesFeed(channel string, options ChangesOptions) (<-chan *ChangeEntry, error) {
dbExpvars.Add("channelChangesFeeds", 1)
log, err := db.changeCache.GetChangesInChannel(channel, options)
if err != nil {
return nil, err
}
if len(log) == 0 {
// There are no entries newer than 'since'. Return an empty feed:
feed := make(chan *ChangeEntry)
close(feed)
return feed, nil
}
feed := make(chan *ChangeEntry, 1)
go func() {
defer close(feed)
// Now write each log entry to the 'feed' channel in turn:
for _, logEntry := range log {
if !options.Conflicts && (logEntry.Flags&channels.Hidden) != 0 {
//continue // FIX: had to comment this out.
// This entry is shadowed by a conflicting one. We would like to skip it.
// The problem is that if this is the newest revision of this doc, then the
// doc will appear under this sequence # in the changes view, which means
// we won't emit the doc at all because we already stopped emitting entries
// from the view before this point.
}
if logEntry.Sequence >= options.Since.TriggeredBy {
options.Since.TriggeredBy = 0
}
seqID := SequenceID{
Seq: logEntry.Sequence,
TriggeredBy: options.Since.TriggeredBy,
}
change := ChangeEntry{
Seq: seqID,
ID: logEntry.DocID,
Deleted: (logEntry.Flags & channels.Deleted) != 0,
Changes: []ChangeRev{{"rev": logEntry.RevID}},
branched: (logEntry.Flags & channels.Branched) != 0,
}
if logEntry.Flags&channels.Removed != 0 {
change.Removed = channels.SetOf(channel)
}
select {
case <-options.Terminator:
base.LogTo("Changes+", "Aborting changesFeed")
return
case feed <- &change:
}
}
}()
return feed, nil
}
示例10: TestChangesAfterChannelAdded
// Unit test for bug #314
func TestChangesAfterChannelAdded(t *testing.T) {
base.LogKeys["Cache"] = true
base.LogKeys["Changes"] = true
base.LogKeys["Changes+"] = true
db := setupTestDB(t)
defer tearDownTestDB(t, db)
db.ChannelMapper = channels.NewDefaultChannelMapper()
// Create a user with access to channel ABC
authenticator := db.Authenticator()
user, _ := authenticator.NewUser("naomi", "letmein", channels.SetOf("ABC"))
authenticator.Save(user)
// Create a doc on two channels (sequence 1):
revid, _ := db.Put("doc1", Body{"channels": []string{"ABC", "PBS"}})
// Modify user to have access to both channels (sequence 2):
userInfo, err := db.GetPrincipal("naomi", true)
assert.True(t, userInfo != nil)
userInfo.ExplicitChannels = base.SetOf("ABC", "PBS")
_, err = db.UpdatePrincipal(*userInfo, true, true)
assertNoError(t, err, "UpdatePrincipal failed")
// Check the _changes feed:
db.changeCache.waitForSequence(1)
db.user, _ = authenticator.GetUser("naomi")
changes, err := db.GetChanges(base.SetOf("*"), ChangesOptions{Since: SequenceID{Seq: 1}})
assertNoError(t, err, "Couldn't GetChanges")
assert.Equals(t, len(changes), 2)
assert.DeepEquals(t, changes[0], &ChangeEntry{
Seq: SequenceID{Seq: 1, TriggeredBy: 2},
ID: "doc1",
Changes: []ChangeRev{{"rev": revid}}})
assert.DeepEquals(t, changes[1], &ChangeEntry{
Seq: SequenceID{Seq: 2},
ID: "_user/naomi",
Changes: []ChangeRev{}})
// Add a new doc (sequence 3):
revid, _ = db.Put("doc2", Body{"channels": []string{"PBS"}})
// Check the _changes feed -- this is to make sure the changeCache properly received
// sequence 2 (the user doc) and isn't stuck waiting for it.
db.changeCache.waitForSequence(3)
changes, err = db.GetChanges(base.SetOf("*"), ChangesOptions{Since: SequenceID{Seq: 2}})
assertNoError(t, err, "Couldn't GetChanges (2nd)")
assert.Equals(t, len(changes), 1)
assert.DeepEquals(t, changes[0], &ChangeEntry{
Seq: SequenceID{Seq: 3},
ID: "doc2",
Changes: []ChangeRev{{"rev": revid}}})
}
示例11: TestUpdateDesignDoc
func TestUpdateDesignDoc(t *testing.T) {
db := setupTestDB(t)
defer tearDownTestDB(t, db)
_, err := db.Put("_design/official", Body{})
assertNoError(t, err, "add design doc as admin")
authenticator := auth.NewAuthenticator(db.Bucket, db)
db.user, _ = authenticator.NewUser("naomi", "letmein", channels.SetOf("Netflix"))
_, err = db.Put("_design/pwn3d", Body{})
assertHTTPError(t, err, 403)
}
示例12: handleChanges
// Top-level handler for _changes feed requests.
func (h *handler) handleChanges() error {
// http://wiki.apache.org/couchdb/HTTP_database_API#Changes
// http://docs.couchdb.org/en/latest/api/database/changes.html
var options db.ChangesOptions
options.Since = channels.TimedSetFromString(h.getQuery("since"))
options.Limit = int(h.getIntQuery("limit", 0))
options.Conflicts = (h.getQuery("style") == "all_docs")
options.IncludeDocs = (h.getBoolQuery("include_docs"))
options.Terminator = make(chan bool)
defer close(options.Terminator)
// Get the channels as parameters to an imaginary "bychannel" filter.
// The default is all channels the user can access.
userChannels := channels.SetOf("*")
filter := h.getQuery("filter")
if filter != "" {
if filter != "sync_gateway/bychannel" {
return base.HTTPErrorf(http.StatusBadRequest, "Unknown filter; try sync_gateway/bychannel")
}
channelsParam := h.getQuery("channels")
if channelsParam == "" {
return base.HTTPErrorf(http.StatusBadRequest, "Missing 'channels' filter parameter")
}
var err error
userChannels, err = channels.SetFromArray(strings.Split(channelsParam, ","),
channels.ExpandStar)
if err != nil {
return err
}
if len(userChannels) == 0 {
return base.HTTPErrorf(http.StatusBadRequest, "Empty channel list")
}
}
h.db.ChangesClientStats.Increment()
defer h.db.ChangesClientStats.Decrement()
switch h.getQuery("feed") {
case "normal", "":
return h.sendSimpleChanges(userChannels, options)
case "longpoll":
options.Wait = true
return h.sendSimpleChanges(userChannels, options)
case "continuous":
return h.sendContinuousChangesByHTTP(userChannels, options)
case "websocket":
return h.sendContinuousChangesByWebSocket(userChannels, options)
default:
return base.HTTPErrorf(http.StatusBadRequest, "Unknown feed type")
}
}
示例13: TestRebuildChannelsError
func TestRebuildChannelsError(t *testing.T) {
computer := mockComputer{}
auth := NewAuthenticator(gTestBucket, &computer)
role, err := auth.NewRole("testRole2", ch.SetOf("explicit1"))
assert.Equals(t, err, nil)
assert.Equals(t, auth.InvalidateChannels(role), nil)
computer.err = fmt.Errorf("I'm sorry, Dave.")
role2, err := auth.GetRole("testRole2")
assert.Equals(t, role2, nil)
assert.DeepEquals(t, err, computer.err)
}
示例14: TestSerializeRole
func TestSerializeRole(t *testing.T) {
auth := NewAuthenticator(gTestBucket, nil)
role, _ := auth.NewRole("froods", ch.SetOf("hoopy", "public"))
encoded, _ := json.Marshal(role)
assert.True(t, encoded != nil)
log.Printf("Marshaled Role as: %s", encoded)
elor := &roleImpl{}
err := json.Unmarshal(encoded, elor)
assert.True(t, err == nil)
assert.DeepEquals(t, elor.Name(), role.Name())
assert.DeepEquals(t, elor.ExplicitChannels(), role.ExplicitChannels())
}
示例15: defaultGuestUser
func (auth *Authenticator) defaultGuestUser() User {
user := &userImpl{
roleImpl: roleImpl{
ExplicitChannels_: ch.AtSequence(ch.SetOf(), 1),
},
userImplBody: userImplBody{
Disabled_: true,
},
auth: auth,
}
user.Channels_ = user.ExplicitChannels_.Copy()
return user
}