本文整理汇总了Golang中github.com/GoogleCloudPlatform/kubernetes/pkg/watch/json.NewEncoder函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEncoder函数的具体用法?Golang NewEncoder怎么用?Golang NewEncoder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewEncoder函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: watchBody
func watchBody(codec runtime.Codec, events []watch.Event) io.ReadCloser {
buf := bytes.NewBuffer([]byte{})
enc := json.NewEncoder(buf, codec)
for i := range events {
enc.Encode(&events[i])
}
return ioutil.NopCloser(buf)
}
开发者ID:Ima8,项目名称:kubernetes,代码行数:8,代码来源:get_test.go
示例2: watchBody
func watchBody(events ...watch.Event) string {
buf := &bytes.Buffer{}
enc := watchjson.NewEncoder(buf, latest.Codec)
for _, e := range events {
enc.Encode(&e)
}
return buf.String()
}
开发者ID:hortonworks,项目名称:kubernetes-yarn,代码行数:8,代码来源:builder_test.go
示例3: ServeHTTP
// ServeHTTP serves a series of JSON encoded events via straight HTTP with
// Transfer-Encoding: chunked.
func (self *WatchServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
loggedW := httplog.LogOf(req, w)
w = httplog.Unlogged(w)
timeoutCh, cleanup := self.t.TimeoutCh()
defer cleanup()
defer self.watching.Stop()
cn, ok := w.(http.CloseNotifier)
if !ok {
loggedW.Addf("unable to get CloseNotifier")
http.NotFound(w, req)
return
}
flusher, ok := w.(http.Flusher)
if !ok {
loggedW.Addf("unable to get Flusher")
http.NotFound(w, req)
return
}
w.Header().Set("Transfer-Encoding", "chunked")
w.WriteHeader(http.StatusOK)
flusher.Flush()
encoder := watchjson.NewEncoder(w, self.codec)
for {
select {
case <-cn.CloseNotify():
return
case <-timeoutCh:
return
case event, ok := <-self.watching.ResultChan():
if !ok {
// End of results.
return
}
self.fixup(event.Object)
if err := encoder.Encode(&event); err != nil {
// Client disconnect.
return
}
flusher.Flush()
}
}
}
开发者ID:cjnygard,项目名称:origin,代码行数:45,代码来源:watch.go
示例4: TestWatch
func TestWatch(t *testing.T) {
var table = []struct {
t watch.EventType
obj runtime.Object
}{
{watch.Added, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "first"}}},
{watch.Modified, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "second"}}},
{watch.Deleted, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "last"}}},
}
auth := &Config{Username: "user", Password: "pass"}
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
checkAuth(t, auth, r)
flusher, ok := w.(http.Flusher)
if !ok {
panic("need flusher!")
}
w.Header().Set("Transfer-Encoding", "chunked")
w.WriteHeader(http.StatusOK)
flusher.Flush()
encoder := watchjson.NewEncoder(w, latest.Codec)
for _, item := range table {
if err := encoder.Encode(&watch.Event{item.t, item.obj}); err != nil {
panic(err)
}
flusher.Flush()
}
}))
s, err := New(&Config{
Host: testServer.URL,
Version: testapi.Version(),
Username: "user",
Password: "pass",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
watching, err := s.Get().Prefix("path/to/watch/thing").Watch()
if err != nil {
t.Fatalf("Unexpected error")
}
for _, item := range table {
got, ok := <-watching.ResultChan()
if !ok {
t.Fatalf("Unexpected early close")
}
if e, a := item.t, got.Type; e != a {
t.Errorf("Expected %v, got %v", e, a)
}
if e, a := item.obj, got.Object; !api.Semantic.DeepDerivative(e, a) {
t.Errorf("Expected %v, got %v", e, a)
}
}
_, ok := <-watching.ResultChan()
if ok {
t.Fatal("Unexpected non-close")
}
}
开发者ID:eghobo,项目名称:kubedash,代码行数:64,代码来源:request_test.go
注:本文中的github.com/GoogleCloudPlatform/kubernetes/pkg/watch/json.NewEncoder函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论