mirror of
https://github.com/weaveworks/scope.git
synced 2026-05-19 15:46:41 +00:00
``` $ gvt delete github.com/weaveworks/common $ gvt fetch --revision 4d96fd8dcf2c7b417912c6219b310112cb4a4626 github.com/weaveworks/common 2018/07/23 15:31:11 Fetching: github.com/weaveworks/common 2018/07/23 15:31:14 · Skipping (existing): github.com/golang/protobuf/ptypes/any 2018/07/23 15:31:14 · Fetching recursive dependency: github.com/pkg/errors 2018/07/23 15:31:16 · Skipping (existing): github.com/aws/aws-sdk-go/aws 2018/07/23 15:31:16 · Fetching recursive dependency: github.com/sirupsen/logrus 2018/07/23 15:31:18 ·· Skipping (existing): golang.org/x/sys/unix 2018/07/23 15:31:18 ·· Skipping (existing): golang.org/x/crypto/ssh/terminal 2018/07/23 15:31:18 · Skipping (existing): google.golang.org/grpc/status 2018/07/23 15:31:18 · Skipping (existing): github.com/gorilla/mux 2018/07/23 15:31:18 · Fetching recursive dependency: github.com/opentracing-contrib/go-stdlib/nethttp 2018/07/23 15:31:20 ·· Skipping (existing): github.com/opentracing/opentracing-go/ext 2018/07/23 15:31:20 ·· Skipping (existing): github.com/opentracing/opentracing-go/log 2018/07/23 15:31:20 ·· Skipping (existing): github.com/opentracing/opentracing-go 2018/07/23 15:31:20 · Skipping (existing): github.com/prometheus/client_golang/prometheus 2018/07/23 15:31:20 · Skipping (existing): google.golang.org/grpc 2018/07/23 15:31:20 · Skipping (existing): github.com/pmezard/go-difflib/difflib 2018/07/23 15:31:20 · Fetching recursive dependency: github.com/go-kit/kit/log 2018/07/23 15:31:23 ·· Fetching recursive dependency: github.com/go-logfmt/logfmt 2018/07/23 15:31:25 ··· Fetching recursive dependency: github.com/kr/logfmt 2018/07/23 15:31:27 ·· Fetching recursive dependency: github.com/go-stack/stack 2018/07/23 15:31:29 · Fetching recursive dependency: google.golang.org/genproto/googleapis/rpc/status 2018/07/23 15:31:37 ·· Skipping (existing): github.com/golang/protobuf/proto 2018/07/23 15:31:37 ·· Skipping (existing): github.com/golang/protobuf/ptypes/any 2018/07/23 15:31:37 · Skipping (existing): github.com/opentracing/opentracing-go/log 2018/07/23 15:31:37 · Fetching recursive dependency: github.com/sercand/kuberesolver 2018/07/23 15:31:39 ·· Skipping (existing): google.golang.org/grpc/grpclog 2018/07/23 15:31:39 ·· Skipping (existing): google.golang.org/grpc/resolver 2018/07/23 15:31:39 ·· Skipping (existing): golang.org/x/net/context 2018/07/23 15:31:39 · Skipping (existing): google.golang.org/grpc/metadata 2018/07/23 15:31:39 · Skipping (existing): github.com/opentracing/opentracing-go/ext 2018/07/23 15:31:39 · Skipping (existing): github.com/armon/go-socks5 2018/07/23 15:31:39 · Skipping (existing): github.com/opentracing/opentracing-go 2018/07/23 15:31:39 · Skipping (existing): github.com/davecgh/go-spew/spew 2018/07/23 15:31:39 · Skipping (existing): github.com/golang/protobuf/ptypes 2018/07/23 15:31:39 · Skipping (existing): github.com/golang/protobuf/proto 2018/07/23 15:31:39 · Fetching recursive dependency: github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc 2018/07/23 15:31:41 ·· Skipping (existing): github.com/opentracing/opentracing-go/log 2018/07/23 15:31:41 ·· Skipping (existing): golang.org/x/net/context 2018/07/23 15:31:41 ·· Skipping (existing): google.golang.org/grpc/codes 2018/07/23 15:31:41 ·· Skipping (existing): github.com/golang/protobuf/proto 2018/07/23 15:31:41 ·· Skipping (existing): github.com/opentracing/opentracing-go 2018/07/23 15:31:41 ·· Skipping (existing): github.com/opentracing/opentracing-go/ext 2018/07/23 15:31:41 ·· Skipping (existing): google.golang.org/grpc 2018/07/23 15:31:41 ·· Skipping (existing): google.golang.org/grpc/metadata 2018/07/23 15:31:41 ·· Skipping (existing): google.golang.org/grpc/status 2018/07/23 15:31:41 · Fetching recursive dependency: github.com/uber/jaeger-client-go/config 2018/07/23 15:31:44 ·· Fetching recursive dependency: github.com/uber/jaeger-client-go/internal/throttler/remote 2018/07/23 15:31:44 ··· Fetching recursive dependency: github.com/uber/jaeger-client-go/utils 2018/07/23 15:31:44 ···· Fetching recursive dependency: github.com/uber/jaeger-client-go/thrift 2018/07/23 15:31:44 ···· Fetching recursive dependency: github.com/uber/jaeger-client-go/thrift-gen/agent 2018/07/23 15:31:44 ····· Fetching recursive dependency: github.com/uber/jaeger-client-go/thrift-gen/jaeger 2018/07/23 15:31:44 ····· Fetching recursive dependency: github.com/uber/jaeger-client-go/thrift-gen/zipkincore 2018/07/23 15:31:44 ··· Fetching recursive dependency: github.com/uber/jaeger-client-go 2018/07/23 15:31:44 ···· Fetching recursive dependency: github.com/crossdock/crossdock-go 2018/07/23 15:31:46 ····· Skipping (existing): github.com/davecgh/go-spew/spew 2018/07/23 15:31:46 ····· Skipping (existing): golang.org/x/net/context/ctxhttp 2018/07/23 15:31:46 ····· Skipping (existing): golang.org/x/net/context 2018/07/23 15:31:46 ····· Skipping (existing): github.com/pmezard/go-difflib/difflib 2018/07/23 15:31:46 ···· Skipping (existing): github.com/opentracing/opentracing-go/log 2018/07/23 15:31:46 ···· Fetching recursive dependency: go.uber.org/zap/zapcore 2018/07/23 15:31:49 ····· Fetching recursive dependency: go.uber.org/atomic 2018/07/23 15:31:51 ····· Fetching recursive dependency: go.uber.org/zap/internal/bufferpool 2018/07/23 15:31:51 ······ Fetching recursive dependency: go.uber.org/zap/buffer 2018/07/23 15:31:51 ····· Fetching recursive dependency: go.uber.org/multierr 2018/07/23 15:31:54 ····· Fetching recursive dependency: go.uber.org/zap/internal/exit 2018/07/23 15:31:54 ····· Fetching recursive dependency: go.uber.org/zap/internal/color 2018/07/23 15:31:54 ···· Fetching recursive dependency: go.uber.org/zap 2018/07/23 15:31:54 ···· Skipping (existing): github.com/opentracing/opentracing-go 2018/07/23 15:31:54 ···· Skipping (existing): github.com/opentracing/opentracing-go/ext 2018/07/23 15:31:54 ···· Fetching recursive dependency: github.com/uber/jaeger-lib/metrics 2018/07/23 15:31:56 ····· Fetching recursive dependency: github.com/uber-go/tally 2018/07/23 15:31:58 ······ Fetching recursive dependency: github.com/m3db/prometheus_client_golang/prometheus/promhttp 2018/07/23 15:32:00 ······· Skipping (existing): github.com/prometheus/client_golang/prometheus 2018/07/23 15:32:00 ······· Skipping (existing): github.com/prometheus/common/expfmt 2018/07/23 15:32:00 ······· Skipping (existing): github.com/prometheus/client_model/go 2018/07/23 15:32:00 ······ Fetching recursive dependency: gopkg.in/validator.v2 2018/07/23 15:32:06 ······ Fetching recursive dependency: github.com/cactus/go-statsd-client/statsd 2018/07/23 15:32:08 ······ Skipping (existing): gopkg.in/yaml.v2 2018/07/23 15:32:08 ······ Fetching recursive dependency: github.com/m3db/prometheus_client_golang/prometheus 2018/07/23 15:32:08 ······· Skipping (existing): github.com/prometheus/procfs 2018/07/23 15:32:08 ······· Skipping (existing): github.com/prometheus/client_model/go 2018/07/23 15:32:08 ······· Skipping (existing): github.com/prometheus/common/expfmt 2018/07/23 15:32:08 ······· Skipping (existing): golang.org/x/net/context 2018/07/23 15:32:08 ······· Skipping (existing): github.com/beorn7/perks/quantile 2018/07/23 15:32:08 ······· Skipping (existing): github.com/golang/protobuf/proto 2018/07/23 15:32:08 ······· Skipping (existing): github.com/prometheus/common/model 2018/07/23 15:32:08 ······· Skipping (existing): github.com/prometheus/client_golang/prometheus 2018/07/23 15:32:08 ······ Fetching recursive dependency: github.com/apache/thrift/lib/go/thrift 2018/07/23 15:32:13 ····· Skipping (existing): github.com/stretchr/testify/assert 2018/07/23 15:32:13 ····· Fetching recursive dependency: github.com/go-kit/kit/metrics/influx 2018/07/23 15:32:13 ······ Fetching recursive dependency: github.com/influxdata/influxdb/client/v2 2018/07/23 15:32:17 ······· Fetching recursive dependency: github.com/influxdata/influxdb/models 2018/07/23 15:32:17 ········ Fetching recursive dependency: github.com/influxdata/influxdb/pkg/escape 2018/07/23 15:32:17 ······ Fetching recursive dependency: github.com/go-kit/kit/metrics 2018/07/23 15:32:17 ······· Fetching recursive dependency: github.com/performancecopilot/speed 2018/07/23 15:32:19 ······· Fetching recursive dependency: github.com/aws/aws-sdk-go-v2/aws 2018/07/23 15:32:28 ········ Fetching recursive dependency: github.com/aws/aws-sdk-go-v2/internal/sdk 2018/07/23 15:32:28 ········ Skipping (existing): github.com/go-ini/ini 2018/07/23 15:32:28 ········ Fetching recursive dependency: github.com/aws/aws-sdk-go-v2/service/sts 2018/07/23 15:32:28 ········· Fetching recursive dependency: github.com/aws/aws-sdk-go-v2/private/protocol/query 2018/07/23 15:32:28 ·········· Fetching recursive dependency: github.com/aws/aws-sdk-go-v2/private/protocol 2018/07/23 15:32:28 ········· Fetching recursive dependency: github.com/aws/aws-sdk-go-v2/internal/awsutil 2018/07/23 15:32:28 ·········· Skipping (existing): github.com/jmespath/go-jmespath 2018/07/23 15:32:28 ······· Fetching recursive dependency: github.com/aws/aws-sdk-go-v2/service/cloudwatch 2018/07/23 15:32:29 ······· Skipping (existing): github.com/aws/aws-sdk-go/aws 2018/07/23 15:32:29 ······· Skipping (existing): github.com/prometheus/client_golang/prometheus 2018/07/23 15:32:29 ······· Skipping (existing): github.com/aws/aws-sdk-go/service/cloudwatch 2018/07/23 15:32:29 ······· Skipping (existing): github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface 2018/07/23 15:32:29 ······· Fetching recursive dependency: golang.org/x/sync/errgroup 2018/07/23 15:32:31 ········ Skipping (existing): golang.org/x/net/context 2018/07/23 15:32:31 ······· Fetching recursive dependency: github.com/go-kit/kit/util/conn 2018/07/23 15:32:31 ······· Fetching recursive dependency: github.com/VividCortex/gohistogram 2018/07/23 15:32:33 ····· Skipping (existing): github.com/prometheus/client_golang/prometheus 2018/07/23 15:32:33 ····· Fetching recursive dependency: github.com/codahale/hdrhistogram 2018/07/23 15:32:35 ·· Skipping (existing): github.com/opentracing/opentracing-go 2018/07/23 15:32:35 · Fetching recursive dependency: github.com/mwitkow/go-grpc-middleware 2018/07/23 15:32:37 ·· Fetching recursive dependency: github.com/grpc-ecosystem/go-grpc-middleware/logging 2018/07/23 15:32:39 ··· Fetching recursive dependency: github.com/grpc-ecosystem/go-grpc-middleware 2018/07/23 15:32:39 ···· Fetching recursive dependency: github.com/golang/protobuf/jsonpb 2018/07/23 15:32:42 ····· Skipping (existing): github.com/golang/protobuf/ptypes/timestamp 2018/07/23 15:32:42 ····· Skipping (existing): github.com/golang/protobuf/proto 2018/07/23 15:32:42 ····· Skipping (existing): github.com/golang/protobuf/ptypes/duration 2018/07/23 15:32:42 ····· Skipping (existing): github.com/golang/protobuf/ptypes/any 2018/07/23 15:32:42 ····· Skipping (existing): github.com/golang/protobuf/ptypes/struct 2018/07/23 15:32:42 ····· Skipping (existing): github.com/golang/protobuf/ptypes/wrappers 2018/07/23 15:32:42 ···· Skipping (existing): google.golang.org/grpc/metadata 2018/07/23 15:32:42 ···· Fetching recursive dependency: github.com/stretchr/testify/suite 2018/07/23 15:32:45 ····· Skipping (existing): github.com/stretchr/testify/assert 2018/07/23 15:32:45 ····· Fetching recursive dependency: github.com/stretchr/testify/require 2018/07/23 15:32:45 ······ Skipping (existing): github.com/stretchr/testify/assert 2018/07/23 15:32:45 ···· Skipping (existing): google.golang.org/grpc/peer 2018/07/23 15:32:45 ···· Skipping (existing): golang.org/x/net/context 2018/07/23 15:32:45 ···· Skipping (existing): golang.org/x/net/trace 2018/07/23 15:32:45 ···· Fetching recursive dependency: github.com/gogo/protobuf/gogoproto 2018/07/23 15:32:48 ····· Fetching recursive dependency: github.com/gogo/protobuf/protoc-gen-gogo/descriptor 2018/07/23 15:32:48 ······ Skipping (existing): github.com/gogo/protobuf/proto 2018/07/23 15:32:48 ····· Skipping (existing): github.com/gogo/protobuf/proto 2018/07/23 15:32:48 ···· Skipping (existing): google.golang.org/grpc/credentials 2018/07/23 15:32:48 ···· Skipping (existing): google.golang.org/grpc 2018/07/23 15:32:48 ···· Skipping (existing): github.com/opentracing/opentracing-go 2018/07/23 15:32:48 ···· Skipping (existing): google.golang.org/grpc/codes 2018/07/23 15:32:48 ···· Skipping (existing): github.com/golang/protobuf/proto 2018/07/23 15:32:48 ···· Skipping (existing): google.golang.org/grpc/grpclog 2018/07/23 15:32:48 ···· Skipping (existing): github.com/opentracing/opentracing-go/ext 2018/07/23 15:32:48 ···· Skipping (existing): github.com/opentracing/opentracing-go/log 2018/07/23 15:32:48 ··· Skipping (existing): golang.org/x/net/context 2018/07/23 15:32:48 ··· Skipping (existing): google.golang.org/grpc 2018/07/23 15:32:48 ··· Skipping (existing): google.golang.org/grpc/grpclog 2018/07/23 15:32:48 ··· Skipping (existing): google.golang.org/grpc/codes 2018/07/23 15:32:48 ··· Skipping (existing): github.com/golang/protobuf/proto 2018/07/23 15:32:48 ·· Skipping (existing): github.com/opentracing/opentracing-go 2018/07/23 15:32:48 ·· Skipping (existing): google.golang.org/grpc 2018/07/23 15:32:48 ·· Skipping (existing): golang.org/x/net/context 2018/07/23 15:32:48 ·· Skipping (existing): google.golang.org/grpc/codes 2018/07/23 15:32:48 ·· Skipping (existing): google.golang.org/grpc/grpclog 2018/07/23 15:32:48 ·· Skipping (existing): github.com/opentracing/opentracing-go/log 2018/07/23 15:32:48 ·· Skipping (existing): google.golang.org/grpc/metadata 2018/07/23 15:32:48 ·· Skipping (existing): google.golang.org/grpc/peer 2018/07/23 15:32:48 ·· Skipping (existing): google.golang.org/grpc/credentials 2018/07/23 15:32:48 ·· Skipping (existing): github.com/golang/protobuf/proto 2018/07/23 15:32:48 ·· Skipping (existing): golang.org/x/net/trace 2018/07/23 15:32:48 ·· Skipping (existing): github.com/opentracing/opentracing-go/ext 2018/07/23 15:32:48 · Fetching recursive dependency: github.com/weaveworks/promrus 2018/07/23 15:32:53 ·· Skipping (existing): gopkg.in/yaml.v2 2018/07/23 15:32:53 ·· Skipping (existing): golang.org/x/net/context/ctxhttp 2018/07/23 15:32:53 ·· Fetching recursive dependency: github.com/stretchr/objx 2018/07/23 15:32:55 ·· Fetching recursive dependency: gopkg.in/alecthomas/kingpin.v2 2018/07/23 15:32:58 ··· Fetching recursive dependency: github.com/alecthomas/units 2018/07/23 15:33:00 ··· Fetching recursive dependency: github.com/alecthomas/template 2018/07/23 15:33:02 ·· Fetching recursive dependency: github.com/julienschmidt/httprouter 2018/07/23 15:33:05 ·· Skipping (existing): golang.org/x/net/context 2018/07/23 15:33:05 · Skipping (existing): github.com/aws/aws-sdk-go/aws/credentials 2018/07/23 15:33:05 · Skipping (existing): github.com/golang/protobuf/ptypes/empty 2018/07/23 15:33:05 · Skipping (existing): golang.org/x/net/context 2018/07/23 15:33:05 · Skipping (existing): golang.org/x/tools/cover 2018/07/23 15:33:05 · Skipping (existing): github.com/mgutz/ansi ```
291 lines
6.6 KiB
Go
291 lines
6.6 KiB
Go
package json
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/private/protocol"
|
|
)
|
|
|
|
// An Encoder provides encoding of the AWS JSON protocol. This encoder will will
|
|
// write all content to JSON. Only supports body and payload targets.
|
|
type Encoder struct {
|
|
encoder
|
|
root bool
|
|
}
|
|
|
|
// NewEncoder creates a new encoder for encoding AWS JSON protocol. Only encodes
|
|
// fields into the JSON body, and error is returned if target is anything other
|
|
// than Body or Payload.
|
|
func NewEncoder() *Encoder {
|
|
buf := bytes.NewBuffer([]byte{'{'})
|
|
e := &Encoder{
|
|
encoder: encoder{
|
|
buf: buf,
|
|
parent: nil,
|
|
fieldBuf: &protocol.FieldBuffer{},
|
|
},
|
|
root: true,
|
|
}
|
|
|
|
return e
|
|
}
|
|
|
|
// Encode returns the encoded XMl reader. An error will be returned if one was
|
|
// encountered while building the JSON body.
|
|
func (e *Encoder) Encode() (io.ReadSeeker, error) {
|
|
b, err := e.encode()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(b) == 2 {
|
|
// Account for first starting object in buffer
|
|
return nil, nil
|
|
}
|
|
|
|
return bytes.NewReader(b), nil
|
|
}
|
|
|
|
// SetValue sets an individual value to the JSON body.
|
|
func (e *Encoder) SetValue(t protocol.Target, k string, v protocol.ValueMarshaler, meta protocol.Metadata) {
|
|
e.encoder.writeSep()
|
|
e.writeKey(k)
|
|
e.writeValue(v)
|
|
}
|
|
|
|
// SetStream is not supported for JSON protocol marshaling.
|
|
func (e *Encoder) SetStream(t protocol.Target, k string, v protocol.StreamMarshaler, meta protocol.Metadata) {
|
|
if e.err != nil {
|
|
return
|
|
}
|
|
e.err = fmt.Errorf("json encoder SetStream not supported, %s, %s", t, k)
|
|
}
|
|
|
|
// Map will return a new mapEncoder and create a new scope for the map encoding.
|
|
func (e *Encoder) Map(t protocol.Target, k string, meta protocol.Metadata) protocol.MapEncoder {
|
|
temp := newScope(e.encoder, &e.encoder)
|
|
return &mapEncoder{temp, k}
|
|
}
|
|
|
|
// List will return a new listEncoder and create a new scope for the list encoding.
|
|
func (e *Encoder) List(t protocol.Target, k string, meta protocol.Metadata) protocol.ListEncoder {
|
|
temp := newScope(e.encoder, &e.encoder)
|
|
return &listEncoder{temp, k}
|
|
}
|
|
|
|
// SetFields sets the nested fields to the JSON body.
|
|
func (e *Encoder) SetFields(t protocol.Target, k string, m protocol.FieldMarshaler, meta protocol.Metadata) {
|
|
if t == protocol.PayloadTarget {
|
|
// Ignore payload key and only marshal body without wrapping in object first.
|
|
nested := Encoder{
|
|
encoder: encoder{
|
|
buf: e.encoder.buf,
|
|
fieldBuf: e.encoder.fieldBuf,
|
|
},
|
|
}
|
|
m.MarshalFields(&nested)
|
|
e.err = nested.err
|
|
return
|
|
}
|
|
|
|
e.writeSep()
|
|
e.writeKey(k)
|
|
e.writeObject(func(enc encoder) error {
|
|
temp := newScope(enc, &e.encoder)
|
|
nested := Encoder{encoder: temp}
|
|
m.MarshalFields(&nested)
|
|
return nested.err
|
|
})
|
|
}
|
|
|
|
// A listEncoder encodes elements within a list for the JSON encoder.
|
|
type listEncoder struct {
|
|
encoder
|
|
k string
|
|
}
|
|
|
|
// Map return a new mapEncoder while creating a new scope for the encoder.
|
|
func (e *listEncoder) Map() protocol.MapEncoder {
|
|
temp := newScope(e.encoder, &e.encoder)
|
|
return &mapEncoder{temp, ""}
|
|
}
|
|
|
|
// List return a new listEncoder while creating a new scope for the encoder.
|
|
func (e *listEncoder) List() protocol.ListEncoder {
|
|
temp := newScope(e.encoder, &e.encoder)
|
|
return &listEncoder{temp, ""}
|
|
}
|
|
|
|
// Start will open a new scope for a list and write the given key.
|
|
func (e *listEncoder) Start() {
|
|
e.encoder.parent.writeSep()
|
|
e.writeKey(e.k)
|
|
e.WriteListStart()
|
|
}
|
|
|
|
// End will close the list.
|
|
func (e *listEncoder) End() {
|
|
e.WriteListEnd()
|
|
}
|
|
|
|
// ListAddValue will add the value to the list.
|
|
func (e *listEncoder) ListAddValue(v protocol.ValueMarshaler) {
|
|
e.encoder.writeSep()
|
|
e.writeValue(v)
|
|
}
|
|
|
|
// ListAddFields will set the nested type's fields to the list.
|
|
func (e *listEncoder) ListAddFields(m protocol.FieldMarshaler) {
|
|
e.encoder.writeSep()
|
|
e.writeObject(func(enc encoder) error {
|
|
temp := newScope(enc, &e.encoder)
|
|
nested := Encoder{encoder: temp}
|
|
m.MarshalFields(&nested)
|
|
return nested.err
|
|
})
|
|
}
|
|
|
|
// A mapEncoder encodes key values pair map values for the JSON encoder.
|
|
type mapEncoder struct {
|
|
encoder encoder
|
|
k string
|
|
}
|
|
|
|
// Start will open a new scope for a list and write the given key.
|
|
func (e *mapEncoder) Start() {
|
|
e.encoder.parent.writeSep()
|
|
e.encoder.writeKey(e.k)
|
|
e.encoder.WriteMapStart()
|
|
}
|
|
|
|
// End will close the list.
|
|
func (e *mapEncoder) End() {
|
|
e.encoder.WriteMapEnd()
|
|
}
|
|
|
|
// Map will create a new scope and return a mapEncoder.
|
|
func (e *mapEncoder) Map(k string) protocol.MapEncoder {
|
|
temp := newScope(e.encoder, &e.encoder)
|
|
return &mapEncoder{temp, k}
|
|
}
|
|
|
|
// List will create a new scope and return a listEncoder
|
|
func (e *mapEncoder) List(k string) protocol.ListEncoder {
|
|
temp := newScope(e.encoder, &e.encoder)
|
|
return &listEncoder{temp, k}
|
|
}
|
|
|
|
// MapSetValue sets a map value.
|
|
func (e *mapEncoder) MapSetValue(k string, v protocol.ValueMarshaler) {
|
|
e.encoder.writeSep()
|
|
e.encoder.writeKey(k)
|
|
e.encoder.writeValue(v)
|
|
}
|
|
|
|
// MapSetFields will set the nested type's fields under the map.
|
|
func (e *mapEncoder) MapSetFields(k string, m protocol.FieldMarshaler) {
|
|
e.encoder.writeSep()
|
|
e.encoder.writeKey(k)
|
|
e.encoder.writeObject(func(enc encoder) error {
|
|
temp := newScope(enc, &e.encoder)
|
|
nested := Encoder{encoder: temp}
|
|
m.MarshalFields(&nested)
|
|
return nested.err
|
|
})
|
|
}
|
|
|
|
type encoder struct {
|
|
buf *bytes.Buffer
|
|
fieldBuf *protocol.FieldBuffer
|
|
started bool
|
|
parent *encoder
|
|
err error
|
|
}
|
|
|
|
func (e encoder) encode() ([]byte, error) {
|
|
if e.err != nil {
|
|
return nil, e.err
|
|
}
|
|
|
|
// Close the root object
|
|
e.buf.WriteByte('}')
|
|
|
|
return e.buf.Bytes(), nil
|
|
}
|
|
|
|
func (e *encoder) writeKey(k string) {
|
|
e.buf.WriteByte('"')
|
|
e.buf.WriteString(k)
|
|
e.buf.WriteByte('"')
|
|
e.buf.WriteByte(':')
|
|
}
|
|
|
|
func (e *encoder) writeValue(v protocol.ValueMarshaler) {
|
|
if e.err != nil {
|
|
return
|
|
}
|
|
|
|
b, err := e.fieldBuf.GetValue(v)
|
|
if err != nil {
|
|
e.err = err
|
|
return
|
|
}
|
|
|
|
var asStr bool
|
|
switch v.(type) {
|
|
case protocol.QuotedValue:
|
|
asStr = true
|
|
}
|
|
|
|
if asStr {
|
|
escapeStringBytes(e.buf, b)
|
|
} else {
|
|
e.buf.Write(b)
|
|
}
|
|
}
|
|
|
|
func (e *encoder) writeObject(fn func(encoder) error) {
|
|
if e.err != nil {
|
|
return
|
|
}
|
|
|
|
e.buf.WriteByte('{')
|
|
e.err = fn(*e)
|
|
e.buf.WriteByte('}')
|
|
}
|
|
|
|
func (e *encoder) WriteListStart() {
|
|
e.buf.WriteByte('[')
|
|
}
|
|
|
|
func (e *encoder) WriteListEnd() {
|
|
e.buf.WriteByte(']')
|
|
}
|
|
|
|
func (e *encoder) WriteMapStart() {
|
|
e.buf.WriteByte('{')
|
|
}
|
|
|
|
func (e *encoder) WriteMapEnd() {
|
|
e.buf.WriteByte('}')
|
|
}
|
|
|
|
func (e *encoder) writeSep() {
|
|
if e.started {
|
|
e.buf.WriteByte(',')
|
|
} else {
|
|
e.started = true
|
|
}
|
|
|
|
}
|
|
|
|
// newScope will return a new encoder with the correct parent and started to false.
|
|
func newScope(e encoder, parent *encoder) encoder {
|
|
temp := e
|
|
temp.started = false
|
|
temp.parent = parent
|
|
return temp
|
|
}
|