Compare commits

..

13 Commits

Author SHA1 Message Date
cthuang 588f1a03c4 Release 2021.10.4 2021-10-25 09:29:45 +01:00
cthuang f8fbbcd806 TUN-5184: Make sure outstanding websocket write is finished, and no more writes after shutdown 2021-10-25 08:27:40 +00:00
cthuang 2ca4633f89 TUN-5286: Upgrade crypto/ssh package to fix CVE-2020-29652 2021-10-22 16:50:35 +00:00
cthuang 2ce11a20c4 TUN-5287: Fix misuse of wait group in TestQUICServer that caused the test to exit immediately 2021-10-22 13:40:51 +00:00
cthuang ff7c48568c TUN-5261: Collect QUIC metrics about RTT, packets and bytes transfered and log events at tracing level 2021-10-21 15:26:57 +01:00
Nuno Diegues 958650be1f TUN-5262: Improvements to max-fetch-size that allow to deal with large number of tunnels in account
* `max-fetch-size` can now be set up in the config YAML
 * we no longer pass that to filter commands that filter by name
 * flag changed to signed int since altsrc does not support UInt flags
 * we now look up each non UUID (to convert it to a UUID) when needed, separately
2021-10-19 18:28:29 +01:00
Nuno Diegues eb51ff0a6d TUN-5262: Allow to configure max fetch size for listing queries
This can be useful/important for accounts with many tunnels that exceed
the 1000 default page size.

There are various tunnel subcommands that use listing underneath, so we make
that flag a tunnel one, rather than adding it to each subcommand.
2021-10-18 11:07:02 +01:00
Nuno Diegues 3f4407ce27 Release 2021.10.3 2021-10-14 17:52:50 +01:00
Nuno Diegues d9636c73b4 TUN-5257: Fix more cfsetup targets that were broken by recent package changes 2021-10-14 16:47:34 +01:00
Nuno Diegues 6cbf90883d TUN-5255: Fix potential panic if Cloudflare API fails to respond to GetTunnel(id) during delete command 2021-10-14 15:18:34 +00:00
Nuno Diegues 76ade67f95 Release 2021.10.2 2021-10-14 14:42:56 +01:00
Nuno Diegues 997f2cf612 TUN-5250: Add missing packages for cfsetup to succeed in github release pkgs target 2021-10-14 08:56:21 +00:00
Sudarsan Reddy ceb509ee98 TUN-5138: Switch to QUIC on auto protocol based on threshold 2021-10-14 09:18:20 +01:00
214 changed files with 15481 additions and 4999 deletions
+16
View File
@@ -1,3 +1,19 @@
2021.10.4
- 2021-10-21 TUN-5287: Fix misuse of wait group in TestQUICServer that caused the test to exit immediately
- 2021-10-21 TUN-5286: Upgrade crypto/ssh package to fix CVE-2020-29652
- 2021-10-18 TUN-5262: Allow to configure max fetch size for listing queries
- 2021-10-19 TUN-5262: Improvements to `max-fetch-size` that allow to deal with large number of tunnels in account
- 2021-10-15 TUN-5261: Collect QUIC metrics about RTT, packets and bytes transfered and log events at tracing level
- 2021-10-19 TUN-5184: Make sure outstanding websocket write is finished, and no more writes after shutdown
2021.10.3
- 2021-10-14 TUN-5255: Fix potential panic if Cloudflare API fails to respond to GetTunnel(id) during delete command
- 2021-10-14 TUN-5257: Fix more cfsetup targets that were broken by recent package changes
2021.10.2
- 2021-10-11 TUN-5138: Switch to QUIC on auto protocol based on threshold
- 2021-10-14 TUN-5250: Add missing packages for cfsetup to succeed in github release pkgs target
2021.10.1
- 2021-10-12 TUN-5246: Use protocol: quic for Quick tunnels if one is not already set
- 2021-10-13 TUN-5249: Revert "TUN-5138: Switch to QUIC on auto protocol based on threshold"
+8
View File
@@ -55,6 +55,8 @@ stretch: &stretch
# libmsi and libgcab are libraries the wixl binary depends on.
- libmsi-dev
- libgcab-dev
- python3-dev
- libffi-dev
- python3-setuptools
- python3-pip
pre-cache:
@@ -111,6 +113,9 @@ stretch: &stretch
build_dir: *build_dir
builddeps:
- *pinned_go
- build-essential
- python3-dev
- libffi-dev
- python3-setuptools
- python3-pip
pre-cache: &install_pygithub
@@ -163,6 +168,9 @@ stretch: &stretch
build_dir: *build_dir
builddeps:
- *pinned_go
- build-essential
- python3-dev
- libffi-dev
- python3-setuptools
- python3-pip
pre-cache: *install_pygithub
+6
View File
@@ -646,6 +646,12 @@ func tunnelFlags(shouldHide bool) []cli.Flag {
Value: "https://api.trycloudflare.com",
Hidden: true,
}),
altsrc.NewIntFlag(&cli.IntFlag{
Name: "max-fetch-size",
Usage: `The maximum number of results that cloudflared can fetch from Cloudflare API for any listing operations needed`,
EnvVars: []string{"TUNNEL_MAX_FETCH_SIZE"},
Hidden: true,
}),
selectProtocolFlag,
overwriteDNSFlag,
}...)
+1 -1
View File
@@ -238,7 +238,7 @@ func prepareTunnelConfig(
log.Info().Msgf("Warp-routing is enabled")
}
protocolSelector, err := connection.NewProtocolSelector(c.String("protocol"), warpRoutingEnabled, namedTunnel, edgediscovery.HTTP2Percentage, origin.ResolveTTL, log)
protocolSelector, err := connection.NewProtocolSelector(c.String("protocol"), warpRoutingEnabled, namedTunnel, edgediscovery.ProtocolPercentage, origin.ResolveTTL, log)
if err != nil {
return nil, ingress.Ingress{}, err
}
+29 -39
View File
@@ -230,7 +230,7 @@ func (sc *subcommandContext) delete(tunnelIDs []uuid.UUID) error {
for _, id := range tunnelIDs {
tunnel, err := client.GetTunnel(id)
if err != nil {
return errors.Wrapf(err, "Can't get tunnel information. Please check tunnel id: %s", tunnel.ID)
return errors.Wrapf(err, "Can't get tunnel information. Please check tunnel id: %s", id)
}
// Check if tunnel DeletedAt field has already been set
@@ -373,52 +373,42 @@ func (sc *subcommandContext) findID(input string) (uuid.UUID, error) {
}
// findIDs is just like mapping `findID` over a slice, but it only uses
// one Tunnelstore API call.
// one Tunnelstore API call per non-UUID input provided.
func (sc *subcommandContext) findIDs(inputs []string) ([]uuid.UUID, error) {
uuids, names := splitUuids(inputs)
// Shortcut without Tunnelstore call if we find that all inputs are already UUIDs.
uuids, err := convertNamesToUuids(inputs, make(map[string]uuid.UUID))
if err == nil {
return uuids, nil
for _, name := range names {
filter := tunnelstore.NewFilter()
filter.NoDeleted()
filter.ByName(name)
tunnels, err := sc.list(filter)
if err != nil {
return nil, err
}
if len(tunnels) != 1 {
return nil, fmt.Errorf("there should only be 1 non-deleted Tunnel named %s", name)
}
uuids = append(uuids, tunnels[0].ID)
}
// First, look up all tunnels the user has
filter := tunnelstore.NewFilter()
filter.NoDeleted()
tunnels, err := sc.list(filter)
if err != nil {
return nil, err
}
// Do the pure list-processing in its own function, so that it can be
// unit tested easily.
return findIDs(tunnels, inputs)
return uuids, nil
}
func findIDs(tunnels []*tunnelstore.Tunnel, inputs []string) ([]uuid.UUID, error) {
// Put them into a dictionary for faster lookups
nameToID := make(map[string]uuid.UUID, len(tunnels))
for _, tunnel := range tunnels {
nameToID[tunnel.Name] = tunnel.ID
}
func splitUuids(inputs []string) ([]uuid.UUID, []string) {
uuids := make([]uuid.UUID, 0)
names := make([]string, 0)
return convertNamesToUuids(inputs, nameToID)
}
func convertNamesToUuids(inputs []string, nameToID map[string]uuid.UUID) ([]uuid.UUID, error) {
tunnelIDs := make([]uuid.UUID, len(inputs))
var badInputs []string
for i, input := range inputs {
if id, err := uuid.Parse(input); err == nil {
tunnelIDs[i] = id
} else if id, ok := nameToID[input]; ok {
tunnelIDs[i] = id
for _, input := range inputs {
id, err := uuid.Parse(input)
if err != nil {
names = append(names, input)
} else {
badInputs = append(badInputs, input)
uuids = append(uuids, id)
}
}
if len(badInputs) > 0 {
msg := "Please specify either the ID or name of a tunnel. The following inputs were neither: %s"
return nil, fmt.Errorf(msg, strings.Join(badInputs, ", "))
}
return tunnelIDs, nil
return uuids, names
}
@@ -17,79 +17,6 @@ import (
"github.com/cloudflare/cloudflared/tunnelstore"
)
func Test_findIDs(t *testing.T) {
type args struct {
tunnels []*tunnelstore.Tunnel
inputs []string
}
tests := []struct {
name string
args args
want []uuid.UUID
wantErr bool
}{
{
name: "input not found",
args: args{
inputs: []string{"asdf"},
},
wantErr: true,
},
{
name: "only UUID",
args: args{
inputs: []string{"a8398a0b-876d-48ed-b609-3fcfd67a4950"},
},
want: []uuid.UUID{uuid.MustParse("a8398a0b-876d-48ed-b609-3fcfd67a4950")},
},
{
name: "only name",
args: args{
tunnels: []*tunnelstore.Tunnel{
{
ID: uuid.MustParse("a8398a0b-876d-48ed-b609-3fcfd67a4950"),
Name: "tunnel1",
},
},
inputs: []string{"tunnel1"},
},
want: []uuid.UUID{uuid.MustParse("a8398a0b-876d-48ed-b609-3fcfd67a4950")},
},
{
name: "both UUID and name",
args: args{
tunnels: []*tunnelstore.Tunnel{
{
ID: uuid.MustParse("a8398a0b-876d-48ed-b609-3fcfd67a4950"),
Name: "tunnel1",
},
{
ID: uuid.MustParse("bf028b68-744f-466e-97f8-c46161d80aa5"),
Name: "tunnel2",
},
},
inputs: []string{"tunnel1", "bf028b68-744f-466e-97f8-c46161d80aa5"},
},
want: []uuid.UUID{
uuid.MustParse("a8398a0b-876d-48ed-b609-3fcfd67a4950"),
uuid.MustParse("bf028b68-744f-466e-97f8-c46161d80aa5"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := findIDs(tt.args.tunnels, tt.args.inputs)
if (err != nil) != tt.wantErr {
t.Errorf("findIDs() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("findIDs() = %v, want %v", got, tt.want)
}
})
}
}
type mockFileSystem struct {
rf func(string) ([]byte, error)
vfp func(string) bool
+3
View File
@@ -277,6 +277,9 @@ func listCommand(c *cli.Context) error {
}
filter.ByTunnelID(tunnelID)
}
if maxFetch := c.Int("max-fetch-size"); maxFetch > 0 {
filter.MaxFetchSize(uint(maxFetch))
}
tunnels, err := sc.list(filter)
if err != nil {
+77 -18
View File
@@ -4,16 +4,17 @@ import (
"context"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"testing"
"time"
"github.com/gobwas/ws/wsutil"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/cloudflare/cloudflared/ingress"
"github.com/cloudflare/cloudflared/websocket"
)
const (
@@ -50,7 +51,15 @@ func (moc *mockOriginProxy) ProxyHTTP(
isWebsocket bool,
) error {
if isWebsocket {
return wsEndpoint(w, req)
switch req.URL.Path {
case "/ws/echo":
return wsEchoEndpoint(w, req)
case "/ws/flaky":
return wsFlakyEndpoint(w, req)
default:
originRespEndpoint(w, http.StatusNotFound, []byte("ws endpoint not found"))
return fmt.Errorf("Unknwon websocket endpoint %s", req.URL.Path)
}
}
switch req.URL.Path {
case "/ok":
@@ -78,32 +87,82 @@ func (moc *mockOriginProxy) ProxyTCP(
return nil
}
type nowriter struct {
io.Reader
type echoPipe struct {
reader *io.PipeReader
writer *io.PipeWriter
}
func (nowriter) Write(p []byte) (int, error) {
return 0, fmt.Errorf("Writer not implemented")
func (ep *echoPipe) Read(p []byte) (int, error) {
return ep.reader.Read(p)
}
func wsEndpoint(w ResponseWriter, r *http.Request) error {
func (ep *echoPipe) Write(p []byte) (int, error) {
return ep.writer.Write(p)
}
// A mock origin that echos data by streaming like a tcpOverWSConnection
// https://github.com/cloudflare/cloudflared/blob/master/ingress/origin_connection.go
func wsEchoEndpoint(w ResponseWriter, r *http.Request) error {
resp := &http.Response{
StatusCode: http.StatusSwitchingProtocols,
}
_ = w.WriteRespHeaders(resp.StatusCode, resp.Header)
clientReader := nowriter{r.Body}
if err := w.WriteRespHeaders(resp.StatusCode, resp.Header); err != nil {
return err
}
wsCtx, cancel := context.WithCancel(r.Context())
readPipe, writePipe := io.Pipe()
wsConn := websocket.NewConn(wsCtx, NewHTTPResponseReadWriterAcker(w, r), &log)
go func() {
for {
data, err := wsutil.ReadClientText(clientReader)
if err != nil {
return
}
if err := wsutil.WriteServerText(w, data); err != nil {
return
}
select {
case <-wsCtx.Done():
case <-r.Context().Done():
}
readPipe.Close()
writePipe.Close()
}()
<-r.Context().Done()
originConn := &echoPipe{reader: readPipe, writer: writePipe}
websocket.Stream(wsConn, originConn, &log)
cancel()
wsConn.Close()
return nil
}
type flakyConn struct {
closeAt time.Time
}
func (fc *flakyConn) Read(p []byte) (int, error) {
if time.Now().After(fc.closeAt) {
return 0, io.EOF
}
n := copy(p, []byte("Read from flaky connection"))
return n, nil
}
func (fc *flakyConn) Write(p []byte) (int, error) {
if time.Now().After(fc.closeAt) {
return 0, fmt.Errorf("Flaky connection closed")
}
return len(p), nil
}
func wsFlakyEndpoint(w ResponseWriter, r *http.Request) error {
resp := &http.Response{
StatusCode: http.StatusSwitchingProtocols,
}
if err := w.WriteRespHeaders(resp.StatusCode, resp.Header); err != nil {
return err
}
wsCtx, cancel := context.WithCancel(r.Context())
wsConn := websocket.NewConn(wsCtx, NewHTTPResponseReadWriterAcker(w, r), &log)
closedAfter := time.Millisecond * time.Duration(rand.Intn(50))
originConn := &flakyConn{closeAt: time.Now().Add(closedAfter)}
websocket.Stream(wsConn, originConn, &log)
cancel()
wsConn.Close()
return nil
}
+3 -3
View File
@@ -147,7 +147,7 @@ func TestServeStreamWS(t *testing.T) {
headers := []h2mux.Header{
{
Name: ":path",
Value: "/ws",
Value: "/ws/echo",
},
{
Name: "connection",
@@ -167,10 +167,10 @@ func TestServeStreamWS(t *testing.T) {
assert.True(t, hasHeader(stream, ResponseMetaHeader, responseMetaHeaderOrigin))
data := []byte("test websocket")
err = wsutil.WriteClientText(writePipe, data)
err = wsutil.WriteClientBinary(writePipe, data)
require.NoError(t, err)
respBody, err := wsutil.ReadServerText(stream)
respBody, err := wsutil.ReadServerBinary(stream)
require.NoError(t, err)
require.Equal(t, data, respBody, fmt.Sprintf("Expect %s, got %s", string(data), string(respBody)))
+84 -13
View File
@@ -27,7 +27,7 @@ var (
)
func newTestHTTP2Connection() (*HTTP2Connection, net.Conn) {
edgeConn, originConn := net.Pipe()
edgeConn, cfdConn := net.Pipe()
var connIndex = uint8(0)
log := zerolog.Nop()
obs := NewObserver(&log, &log, false)
@@ -41,7 +41,8 @@ func newTestHTTP2Connection() (*HTTP2Connection, net.Conn) {
1*time.Second,
)
return NewHTTP2Connection(
originConn,
cfdConn,
// OriginProxy is set in testConfig
testConfig,
&pogs.ConnectionOptions{},
obs,
@@ -166,6 +167,7 @@ type wsRespWriter struct {
*httptest.ResponseRecorder
readPipe *io.PipeReader
writePipe *io.PipeWriter
closed bool
}
func newWSRespWriter() *wsRespWriter {
@@ -174,46 +176,58 @@ func newWSRespWriter() *wsRespWriter {
httptest.NewRecorder(),
readPipe,
writePipe,
false,
}
}
type nowriter struct {
io.Reader
}
func (nowriter) Write(p []byte) (int, error) {
return 0, fmt.Errorf("Writer not implemented")
}
func (w *wsRespWriter) RespBody() io.ReadWriter {
return nowriter{w.readPipe}
}
func (w *wsRespWriter) Write(data []byte) (n int, err error) {
if w.closed {
// Simulate writing to http2 ResponseWriter after ServeHTTP has returned
panic("Write to closed ResponseWriter")
}
return w.writePipe.Write(data)
}
func (w *wsRespWriter) close() {
w.closed = true
}
func TestServeWS(t *testing.T) {
http2Conn, _ := newTestHTTP2Connection()
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
http2Conn.Serve(ctx)
}()
respWriter := newWSRespWriter()
readPipe, writePipe := io.Pipe()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/ws", readPipe)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/ws/echo", readPipe)
require.NoError(t, err)
req.Header.Set(InternalUpgradeHeader, WebsocketUpgrade)
wg.Add(1)
serveDone := make(chan struct{})
go func() {
defer wg.Done()
defer close(serveDone)
http2Conn.ServeHTTP(respWriter, req)
respWriter.close()
}()
data := []byte("test websocket")
err = wsutil.WriteClientText(writePipe, data)
err = wsutil.WriteClientBinary(writePipe, data)
require.NoError(t, err)
respBody, err := wsutil.ReadServerText(respWriter.RespBody())
respBody, err := wsutil.ReadServerBinary(respWriter.RespBody())
require.NoError(t, err)
require.Equal(t, data, respBody, fmt.Sprintf("Expect %s, got %s", string(data), string(respBody)))
@@ -223,7 +237,64 @@ func TestServeWS(t *testing.T) {
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, responseMetaHeaderOrigin, resp.Header.Get(ResponseMetaHeader))
<-serveDone
}
// TestNoWriteAfterServeHTTPReturns is a regression test of https://jira.cfops.it/browse/TUN-5184
// to make sure we don't write to the ResponseWriter after the ServeHTTP method returns
func TestNoWriteAfterServeHTTPReturns(t *testing.T) {
cfdHTTP2Conn, edgeTCPConn := newTestHTTP2Connection()
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
serverDone := make(chan struct{})
go func() {
defer close(serverDone)
cfdHTTP2Conn.Serve(ctx)
}()
edgeTransport := http2.Transport{}
edgeHTTP2Conn, err := edgeTransport.NewClientConn(edgeTCPConn)
require.NoError(t, err)
message := []byte(t.Name())
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
readPipe, writePipe := io.Pipe()
reqCtx, reqCancel := context.WithCancel(ctx)
req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, "http://localhost:8080/ws/flaky", readPipe)
require.NoError(t, err)
req.Header.Set(InternalUpgradeHeader, WebsocketUpgrade)
resp, err := edgeHTTP2Conn.RoundTrip(req)
require.NoError(t, err)
// http2RespWriter should rewrite status 101 to 200
require.Equal(t, http.StatusOK, resp.StatusCode)
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-reqCtx.Done():
return
default:
}
_ = wsutil.WriteClientBinary(writePipe, message)
}
}()
time.Sleep(time.Millisecond * 100)
reqCancel()
}()
}
wg.Wait()
cancel()
<-serverDone
}
func TestServeControlStream(t *testing.T) {
+105 -53
View File
@@ -7,6 +7,8 @@ import (
"time"
"github.com/rs/zerolog"
"github.com/cloudflare/cloudflared/edgediscovery"
)
const (
@@ -24,7 +26,7 @@ const (
var (
// ProtocolList represents a list of supported protocols for communication with the edge.
ProtocolList = []Protocol{H2mux, HTTP2, QUIC}
ProtocolList = []Protocol{H2mux, HTTP2, HTTP2Warp, QUIC, QUICWarp}
)
type Protocol int64
@@ -36,6 +38,12 @@ const (
HTTP2
// QUIC is used only with named tunnels.
QUIC
// HTTP2Warp is used only with named tunnels. It's useful for warp-routing where we don't want to fallback to
// H2mux on HTTP2 failure to connect.
HTTP2Warp
//QUICWarp is used only with named tunnels. It's useful for warp-routing where we want to fallback to HTTP2 but
// dont' want HTTP2 to fallback to H2mux
QUICWarp
)
// Fallback returns the fallback protocol and whether the protocol has a fallback
@@ -45,8 +53,12 @@ func (p Protocol) fallback() (Protocol, bool) {
return 0, false
case HTTP2:
return H2mux, true
case HTTP2Warp:
return 0, false
case QUIC:
return HTTP2, true
case QUICWarp:
return HTTP2Warp, true
default:
return 0, false
}
@@ -56,9 +68,9 @@ func (p Protocol) String() string {
switch p {
case H2mux:
return "h2mux"
case HTTP2:
case HTTP2, HTTP2Warp:
return "http2"
case QUIC:
case QUIC, QUICWarp:
return "quic"
default:
return fmt.Sprintf("unknown protocol")
@@ -71,11 +83,11 @@ func (p Protocol) TLSSettings() *TLSSettings {
return &TLSSettings{
ServerName: edgeH2muxTLSServerName,
}
case HTTP2:
case HTTP2, HTTP2Warp:
return &TLSSettings{
ServerName: edgeH2TLSServerName,
}
case QUIC:
case QUIC, QUICWarp:
return &TLSSettings{
ServerName: edgeQUICServerName,
NextProtos: []string{"argotunnel"},
@@ -108,29 +120,36 @@ func (s *staticProtocolSelector) Fallback() (Protocol, bool) {
}
type autoProtocolSelector struct {
lock sync.RWMutex
current Protocol
switchThrehold int32
fetchFunc PercentageFetcher
refreshAfter time.Time
ttl time.Duration
log *zerolog.Logger
lock sync.RWMutex
current Protocol
// protocolPool is desired protocols in the order of priority they should be picked in.
protocolPool []Protocol
switchThreshold int32
fetchFunc PercentageFetcher
refreshAfter time.Time
ttl time.Duration
log *zerolog.Logger
}
func newAutoProtocolSelector(
current Protocol,
switchThrehold int32,
protocolPool []Protocol,
switchThreshold int32,
fetchFunc PercentageFetcher,
ttl time.Duration,
log *zerolog.Logger,
) *autoProtocolSelector {
return &autoProtocolSelector{
current: current,
switchThrehold: switchThrehold,
fetchFunc: fetchFunc,
refreshAfter: time.Now().Add(ttl),
ttl: ttl,
log: log,
current: current,
protocolPool: protocolPool,
switchThreshold: switchThreshold,
fetchFunc: fetchFunc,
refreshAfter: time.Now().Add(ttl),
ttl: ttl,
log: log,
}
}
@@ -141,28 +160,39 @@ func (s *autoProtocolSelector) Current() Protocol {
return s.current
}
percentage, err := s.fetchFunc()
protocol, err := getProtocol(s.protocolPool, s.fetchFunc, s.switchThreshold)
if err != nil {
s.log.Err(err).Msg("Failed to refresh protocol")
return s.current
}
s.current = protocol
if s.switchThrehold < percentage {
s.current = HTTP2
} else {
s.current = H2mux
}
s.refreshAfter = time.Now().Add(s.ttl)
return s.current
}
func getProtocol(protocolPool []Protocol, fetchFunc PercentageFetcher, switchThreshold int32) (Protocol, error) {
protocolPercentages, err := fetchFunc()
if err != nil {
return 0, err
}
for _, protocol := range protocolPool {
protocolPercentage := protocolPercentages.GetPercentage(protocol.String())
if protocolPercentage > switchThreshold {
return protocol, nil
}
}
return protocolPool[len(protocolPool)-1], nil
}
func (s *autoProtocolSelector) Fallback() (Protocol, bool) {
s.lock.RLock()
defer s.lock.RUnlock()
return s.current.fallback()
}
type PercentageFetcher func() (int32, error)
type PercentageFetcher func() (edgediscovery.ProtocolPercents, error)
func NewProtocolSelector(
protocolFlag string,
@@ -179,22 +209,34 @@ func NewProtocolSelector(
}, nil
}
// warp routing cannot be served over h2mux connections
if warpRoutingEnabled {
if protocolFlag == H2mux.String() {
log.Warn().Msg("Warp routing is not supported in h2mux protocol. Upgrading to http2 to allow it.")
}
if protocolFlag == QUIC.String() {
return &staticProtocolSelector{
current: QUIC,
}, nil
}
threshold := switchThreshold(namedTunnel.Credentials.AccountTag)
fetchedProtocol, err := getProtocol([]Protocol{QUIC, HTTP2}, fetchFunc, threshold)
if err != nil {
log.Err(err).Msg("Unable to lookup protocol. Defaulting to `http2`. If this fails, you can set `--protocol h2mux` in your cloudflared command.")
return &staticProtocolSelector{
current: HTTP2,
}, nil
}
if warpRoutingEnabled {
if protocolFlag == H2mux.String() || fetchedProtocol == H2mux {
log.Warn().Msg("Warp routing is not supported in h2mux protocol. Upgrading to http2 to allow it.")
protocolFlag = HTTP2.String()
fetchedProtocol = HTTP2Warp
}
return selectWarpRoutingProtocols(protocolFlag, fetchFunc, ttl, log, threshold, fetchedProtocol)
}
return selectNamedTunnelProtocols(protocolFlag, fetchFunc, ttl, log, threshold, fetchedProtocol)
}
func selectNamedTunnelProtocols(
protocolFlag string,
fetchFunc PercentageFetcher,
ttl time.Duration,
log *zerolog.Logger,
threshold int32,
protocol Protocol,
) (ProtocolSelector, error) {
if protocolFlag == H2mux.String() {
return &staticProtocolSelector{
current: H2mux,
@@ -202,31 +244,41 @@ func NewProtocolSelector(
}
if protocolFlag == QUIC.String() {
return newAutoProtocolSelector(QUIC, explicitHTTP2FallbackThreshold, fetchFunc, ttl, log), nil
return newAutoProtocolSelector(QUIC, []Protocol{QUIC, HTTP2, H2mux}, explicitHTTP2FallbackThreshold, fetchFunc, ttl, log), nil
}
http2Percentage, err := fetchFunc()
if err != nil {
log.Err(err).Msg("Unable to lookup protocol. Defaulting to `http2`. If this fails, you can set `--protocol h2mux` in your cloudflared command.")
return &staticProtocolSelector{
current: HTTP2,
}, nil
}
if protocolFlag == HTTP2.String() {
if http2Percentage < 0 {
return newAutoProtocolSelector(H2mux, explicitHTTP2FallbackThreshold, fetchFunc, ttl, log), nil
}
return newAutoProtocolSelector(HTTP2, explicitHTTP2FallbackThreshold, fetchFunc, ttl, log), nil
return newAutoProtocolSelector(HTTP2, []Protocol{HTTP2, H2mux}, explicitHTTP2FallbackThreshold, fetchFunc, ttl, log), nil
}
if protocolFlag != autoSelectFlag {
return nil, fmt.Errorf("Unknown protocol %s, %s", protocolFlag, AvailableProtocolFlagMessage)
}
threshold := switchThreshold(namedTunnel.Credentials.AccountTag)
if threshold < http2Percentage {
return newAutoProtocolSelector(HTTP2, threshold, fetchFunc, ttl, log), nil
return newAutoProtocolSelector(protocol, []Protocol{QUIC, HTTP2, H2mux}, threshold, fetchFunc, ttl, log), nil
}
func selectWarpRoutingProtocols(
protocolFlag string,
fetchFunc PercentageFetcher,
ttl time.Duration,
log *zerolog.Logger,
threshold int32,
protocol Protocol,
) (ProtocolSelector, error) {
if protocolFlag == QUIC.String() {
return newAutoProtocolSelector(QUICWarp, []Protocol{QUICWarp, HTTP2Warp}, explicitHTTP2FallbackThreshold, fetchFunc, ttl, log), nil
}
return newAutoProtocolSelector(H2mux, threshold, fetchFunc, ttl, log), nil
if protocolFlag == HTTP2.String() {
return newAutoProtocolSelector(HTTP2Warp, []Protocol{HTTP2Warp}, explicitHTTP2FallbackThreshold, fetchFunc, ttl, log), nil
}
if protocolFlag != autoSelectFlag {
return nil, fmt.Errorf("Unknown protocol %s, %s", protocolFlag, AvailableProtocolFlagMessage)
}
return newAutoProtocolSelector(protocol, []Protocol{QUICWarp, HTTP2Warp}, threshold, fetchFunc, ttl, log), nil
}
func switchThreshold(accountTag string) int32 {
+106 -61
View File
@@ -6,6 +6,8 @@ import (
"time"
"github.com/stretchr/testify/assert"
"github.com/cloudflare/cloudflared/edgediscovery"
)
const (
@@ -21,29 +23,23 @@ var (
}
)
func mockFetcher(percentage int32) PercentageFetcher {
return func() (int32, error) {
return percentage, nil
}
}
func mockFetcherWithError() PercentageFetcher {
return func() (int32, error) {
return 0, fmt.Errorf("failed to fetch precentage")
func mockFetcher(getError bool, protocolPercent ...edgediscovery.ProtocolPercent) PercentageFetcher {
return func() (edgediscovery.ProtocolPercents, error) {
if getError {
return nil, fmt.Errorf("failed to fetch precentage")
}
return protocolPercent, nil
}
}
type dynamicMockFetcher struct {
percentage int32
err error
protocolPercents edgediscovery.ProtocolPercents
err error
}
func (dmf *dynamicMockFetcher) fetch() PercentageFetcher {
return func() (int32, error) {
if dmf.err != nil {
return 0, dmf.err
}
return dmf.percentage, nil
return func() (edgediscovery.ProtocolPercents, error) {
return dmf.protocolPercents, dmf.err
}
}
@@ -69,6 +65,7 @@ func TestNewProtocolSelector(t *testing.T) {
name: "named tunnel over h2mux",
protocol: "h2mux",
expectedProtocol: H2mux,
fetchFunc: func() (edgediscovery.ProtocolPercents, error) { return nil, nil },
namedTunnelConfig: testNamedTunnelConfig,
},
{
@@ -77,28 +74,38 @@ func TestNewProtocolSelector(t *testing.T) {
expectedProtocol: HTTP2,
hasFallback: true,
expectedFallback: H2mux,
fetchFunc: mockFetcher(0),
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}),
namedTunnelConfig: testNamedTunnelConfig,
},
{
name: "named tunnel http2 disabled",
protocol: "http2",
expectedProtocol: H2mux,
fetchFunc: mockFetcher(-1),
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}),
namedTunnelConfig: testNamedTunnelConfig,
},
{
name: "named tunnel quic disabled",
protocol: "quic",
expectedProtocol: HTTP2,
// Hasfallback true is because if http2 fails, then we further fallback to h2mux.
hasFallback: true,
expectedFallback: H2mux,
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}, edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: -1}),
namedTunnelConfig: testNamedTunnelConfig,
},
{
name: "named tunnel auto all http2 disabled",
protocol: "auto",
expectedProtocol: H2mux,
fetchFunc: mockFetcher(-1),
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}),
namedTunnelConfig: testNamedTunnelConfig,
},
{
name: "named tunnel auto to h2mux",
protocol: "auto",
expectedProtocol: H2mux,
fetchFunc: mockFetcher(0),
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}),
namedTunnelConfig: testNamedTunnelConfig,
},
{
@@ -107,36 +114,71 @@ func TestNewProtocolSelector(t *testing.T) {
expectedProtocol: HTTP2,
hasFallback: true,
expectedFallback: H2mux,
fetchFunc: mockFetcher(100),
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}),
namedTunnelConfig: testNamedTunnelConfig,
},
{
name: "named tunnel auto to quic",
protocol: "auto",
expectedProtocol: QUIC,
hasFallback: true,
expectedFallback: HTTP2,
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 100}),
namedTunnelConfig: testNamedTunnelConfig,
},
{
name: "warp routing requesting h2mux",
protocol: "h2mux",
expectedProtocol: HTTP2,
expectedProtocol: HTTP2Warp,
hasFallback: false,
expectedFallback: H2mux,
fetchFunc: mockFetcher(100),
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}),
warpRoutingEnabled: true,
namedTunnelConfig: testNamedTunnelConfig,
},
{
name: "warp routing requesting h2mux picks HTTP2 even if http2 percent is -1",
protocol: "h2mux",
expectedProtocol: HTTP2Warp,
hasFallback: false,
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}),
warpRoutingEnabled: true,
namedTunnelConfig: testNamedTunnelConfig,
},
{
name: "warp routing http2",
protocol: "http2",
expectedProtocol: HTTP2,
expectedProtocol: HTTP2Warp,
hasFallback: false,
expectedFallback: H2mux,
fetchFunc: mockFetcher(100),
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}),
warpRoutingEnabled: true,
namedTunnelConfig: testNamedTunnelConfig,
},
{
name: "warp routing quic",
protocol: "quic",
expectedProtocol: QUICWarp,
hasFallback: true,
expectedFallback: HTTP2Warp,
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 100}),
warpRoutingEnabled: true,
namedTunnelConfig: testNamedTunnelConfig,
},
{
name: "warp routing auto",
protocol: "auto",
expectedProtocol: HTTP2,
expectedProtocol: HTTP2Warp,
hasFallback: false,
expectedFallback: H2mux,
fetchFunc: mockFetcher(100),
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}),
warpRoutingEnabled: true,
namedTunnelConfig: testNamedTunnelConfig,
},
{
name: "warp routing auto- quic",
protocol: "auto",
expectedProtocol: QUICWarp,
hasFallback: true,
expectedFallback: HTTP2Warp,
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}, edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 100}),
warpRoutingEnabled: true,
namedTunnelConfig: testNamedTunnelConfig,
},
@@ -149,14 +191,14 @@ func TestNewProtocolSelector(t *testing.T) {
{
name: "named tunnel unknown protocol",
protocol: "unknown",
fetchFunc: mockFetcher(100),
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}),
namedTunnelConfig: testNamedTunnelConfig,
wantErr: true,
},
{
name: "named tunnel fetch error",
protocol: "unknown",
fetchFunc: mockFetcherWithError(),
protocol: "auto",
fetchFunc: mockFetcher(true),
namedTunnelConfig: testNamedTunnelConfig,
expectedProtocol: HTTP2,
wantErr: false,
@@ -164,18 +206,20 @@ func TestNewProtocolSelector(t *testing.T) {
}
for _, test := range tests {
selector, err := NewProtocolSelector(test.protocol, test.warpRoutingEnabled, test.namedTunnelConfig, test.fetchFunc, testNoTTL, &log)
if test.wantErr {
assert.Error(t, err, fmt.Sprintf("test %s failed", test.name))
} else {
assert.NoError(t, err, fmt.Sprintf("test %s failed", test.name))
assert.Equal(t, test.expectedProtocol, selector.Current(), fmt.Sprintf("test %s failed", test.name))
fallback, ok := selector.Fallback()
assert.Equal(t, test.hasFallback, ok, fmt.Sprintf("test %s failed", test.name))
if test.hasFallback {
assert.Equal(t, test.expectedFallback, fallback, fmt.Sprintf("test %s failed", test.name))
t.Run(test.name, func(t *testing.T) {
selector, err := NewProtocolSelector(test.protocol, test.warpRoutingEnabled, test.namedTunnelConfig, test.fetchFunc, testNoTTL, &log)
if test.wantErr {
assert.Error(t, err, fmt.Sprintf("test %s failed", test.name))
} else {
assert.NoError(t, err, fmt.Sprintf("test %s failed", test.name))
assert.Equal(t, test.expectedProtocol, selector.Current(), fmt.Sprintf("test %s failed", test.name))
fallback, ok := selector.Fallback()
assert.Equal(t, test.hasFallback, ok, fmt.Sprintf("test %s failed", test.name))
if test.hasFallback {
assert.Equal(t, test.expectedFallback, fallback, fmt.Sprintf("test %s failed", test.name))
}
}
}
})
}
}
@@ -185,27 +229,27 @@ func TestAutoProtocolSelectorRefresh(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, H2mux, selector.Current())
fetcher.percentage = 100
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}}
assert.Equal(t, HTTP2, selector.Current())
fetcher.percentage = 0
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}}
assert.Equal(t, H2mux, selector.Current())
fetcher.percentage = 100
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}}
assert.Equal(t, HTTP2, selector.Current())
fetcher.err = fmt.Errorf("failed to fetch")
assert.Equal(t, HTTP2, selector.Current())
fetcher.percentage = -1
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}}
fetcher.err = nil
assert.Equal(t, H2mux, selector.Current())
fetcher.percentage = 0
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}}
assert.Equal(t, H2mux, selector.Current())
fetcher.percentage = 100
assert.Equal(t, HTTP2, selector.Current())
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 100}}
assert.Equal(t, QUIC, selector.Current())
}
func TestHTTP2ProtocolSelectorRefresh(t *testing.T) {
@@ -214,35 +258,36 @@ func TestHTTP2ProtocolSelectorRefresh(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, HTTP2, selector.Current())
fetcher.percentage = 100
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}}
assert.Equal(t, HTTP2, selector.Current())
fetcher.percentage = 0
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}}
assert.Equal(t, HTTP2, selector.Current())
fetcher.err = fmt.Errorf("failed to fetch")
assert.Equal(t, HTTP2, selector.Current())
fetcher.percentage = -1
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}}
fetcher.err = nil
assert.Equal(t, H2mux, selector.Current())
fetcher.percentage = 0
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}}
assert.Equal(t, HTTP2, selector.Current())
fetcher.percentage = 100
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}}
assert.Equal(t, HTTP2, selector.Current())
fetcher.percentage = -1
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}}
assert.Equal(t, H2mux, selector.Current())
}
func TestProtocolSelectorRefreshTTL(t *testing.T) {
fetcher := dynamicMockFetcher{percentage: 100}
fetcher := dynamicMockFetcher{}
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 100}}
selector, err := NewProtocolSelector("auto", noWarpRoutingEnabled, testNamedTunnelConfig, fetcher.fetch(), time.Hour, &log)
assert.NoError(t, err)
assert.Equal(t, HTTP2, selector.Current())
assert.Equal(t, QUIC, selector.Current())
fetcher.percentage = 0
assert.Equal(t, HTTP2, selector.Current())
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 0}}
assert.Equal(t, QUIC, selector.Current())
}
+5 -5
View File
@@ -61,7 +61,7 @@ func TestQUICServer(t *testing.T) {
// This is simply a sample websocket frame message.
wsBuf := &bytes.Buffer{}
wsutil.WriteClientText(wsBuf, []byte("Hello"))
wsutil.WriteClientBinary(wsBuf, []byte("Hello"))
var tests = []struct {
desc string
@@ -118,7 +118,7 @@ func TestQUICServer(t *testing.T) {
},
{
desc: "test ws proxy",
dest: "/ok",
dest: "/ws/echo",
connectionType: quicpogs.ConnectionTypeWebsocket,
metadata: []quicpogs.Metadata{
quicpogs.Metadata{
@@ -139,7 +139,7 @@ func TestQUICServer(t *testing.T) {
},
},
message: wsBuf.Bytes(),
expectedResponse: []byte{0x81, 0x5, 0x48, 0x65, 0x6c, 0x6c, 0x6f},
expectedResponse: []byte{0x82, 0x5, 0x48, 0x65, 0x6c, 0x6c, 0x6f},
},
{
desc: "test tcp proxy",
@@ -154,8 +154,8 @@ func TestQUICServer(t *testing.T) {
t.Run(test.desc, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Add(1)
defer wg.Done()
quicServer(
t, udpListener, tlsConfig, quicConfig,
@@ -278,7 +278,7 @@ func (moc *mockOriginProxyWithRequest) ProxyHTTP(w ResponseWriter, r *http.Reque
}
if isWebsocket {
return wsEndpoint(w, r)
return wsEchoEndpoint(w, r)
}
switch r.URL.Path {
case "/ok":
+28 -23
View File
@@ -1,45 +1,50 @@
package edgediscovery
import (
"encoding/json"
"fmt"
"net"
"strconv"
"strings"
)
const (
protocolRecord = "protocol.argotunnel.com"
protocolRecord = "protocol-v2.argotunnel.com"
)
var (
errNoProtocolRecord = fmt.Errorf("No TXT record found for %s to determine connection protocol", protocolRecord)
)
func HTTP2Percentage() (int32, error) {
// ProtocolPercent represents a single Protocol Percentage combination.
type ProtocolPercent struct {
Protocol string `json:"protocol"`
Percentage int32 `json:"percentage"`
}
// ProtocolPercents represents the preferred distribution ratio of protocols when protocol isn't specified.
type ProtocolPercents []ProtocolPercent
// GetPercentage returns the threshold percentage of a single protocol.
func (p ProtocolPercents) GetPercentage(protocol string) int32 {
for _, protocolPercent := range p {
if strings.ToLower(protocolPercent.Protocol) == strings.ToLower(protocol) {
return protocolPercent.Percentage
}
}
return 0
}
// ProtocolPercentage returns the ratio of protocols and a specification ratio for their selection.
func ProtocolPercentage() (ProtocolPercents, error) {
records, err := net.LookupTXT(protocolRecord)
if err != nil {
return 0, err
return nil, err
}
if len(records) == 0 {
return 0, errNoProtocolRecord
return nil, errNoProtocolRecord
}
return parseHTTP2Precentage(records[0])
}
// The record looks like http2=percentage
func parseHTTP2Precentage(record string) (int32, error) {
const key = "http2"
slices := strings.Split(record, "=")
if len(slices) != 2 {
return 0, fmt.Errorf("Malformed TXT record %s, expect http2=percentage", record)
}
if slices[0] != key {
return 0, fmt.Errorf("Incorrect key %s, expect %s", slices[0], key)
}
percentage, err := strconv.ParseInt(slices[1], 10, 32)
if err != nil {
return 0, err
}
return int32(percentage), nil
var protocolsWithPercent ProtocolPercents
err = json.Unmarshal([]byte(records[0]), &protocolsWithPercent)
return protocolsWithPercent, err
}
+2 -70
View File
@@ -6,75 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)
func TestHTTP2Percentage(t *testing.T) {
_, err := HTTP2Percentage()
func TestProtocolPercentage(t *testing.T) {
_, err := ProtocolPercentage()
assert.NoError(t, err)
}
func TestParseHTTP2Precentage(t *testing.T) {
tests := []struct {
record string
percentage int32
wantErr bool
}{
{
record: "http2=-1",
percentage: -1,
wantErr: false,
},
{
record: "http2=0",
percentage: 0,
wantErr: false,
},
{
record: "http2=50",
percentage: 50,
wantErr: false,
},
{
record: "http2=100",
percentage: 100,
wantErr: false,
},
{
record: "http2=1000",
percentage: 1000,
wantErr: false,
},
{
record: "http2=10.5",
wantErr: true,
},
{
record: "http2=10 h2mux=90",
wantErr: true,
},
{
record: "http2=ten",
wantErr: true,
},
{
record: "h2mux=100",
wantErr: true,
},
{
record: "http2",
wantErr: true,
},
{
record: "http2=",
wantErr: true,
},
}
for _, test := range tests {
p, err := parseHTTP2Precentage(test.record)
if test.wantErr {
assert.Error(t, err)
} else {
assert.Equal(t, test.percentage, p)
}
}
}
+3 -2
View File
@@ -45,11 +45,11 @@ require (
github.com/stretchr/testify v1.6.0
github.com/urfave/cli/v2 v2.2.0
go.uber.org/automaxprocs v1.4.0
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210510120138-977fb7262007
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf
google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d // indirect
google.golang.org/grpc v1.32.0 // indirect
@@ -70,6 +70,7 @@ require (
github.com/cheekybits/genny v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
+5
View File
@@ -197,6 +197,7 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/francoispqt/gojay v1.2.13 h1:d2m3sFjloqoIUQU3TsHBgj6qg/BVGlTBeHDUmyJnXKk=
github.com/francoispqt/gojay v1.2.13/go.mod h1:ehT5mTG4ua4581f1++1WLG0vPdaA9HaiDsoyrBGkyDY=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=
github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20=
@@ -732,6 +733,8 @@ golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a h1:vclmkQCjlDX5OydZ9wv8rBCcS0QyQY66Mpf/7BZbInM=
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -899,6 +902,8 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf h1:MZ2shdL+ZM/XzY3ZGOnh4Nlpnxz5GSOhOmtHo3iPU6M=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+2 -2
View File
@@ -53,7 +53,7 @@ func (wc *tcpOverWSConnection) Stream(ctx context.Context, tunnelConn io.ReadWri
wc.streamHandler(wsConn, wc.conn, log)
cancel()
// Makes sure wsConn stops sending ping before terminating the stream
wsConn.WaitForShutdown()
wsConn.Close()
}
func (wc *tcpOverWSConnection) Close() {
@@ -73,7 +73,7 @@ func (sp *socksProxyOverWSConnection) Stream(ctx context.Context, tunnelConn io.
socks.StreamNetHandler(wsConn, sp.accessPolicy, log)
cancel()
// Makes sure wsConn stops sending ping before terminating the stream
wsConn.WaitForShutdown()
wsConn.Close()
}
func (sp *socksProxyOverWSConnection) Close() {
+6 -15
View File
@@ -20,6 +20,7 @@ import (
"github.com/cloudflare/cloudflared/edgediscovery"
"github.com/cloudflare/cloudflared/edgediscovery/allregions"
"github.com/cloudflare/cloudflared/h2mux"
quicpogs "github.com/cloudflare/cloudflared/quic"
"github.com/cloudflare/cloudflared/retry"
"github.com/cloudflare/cloudflared/signal"
"github.com/cloudflare/cloudflared/tunnelrpc"
@@ -351,17 +352,18 @@ func serveTunnel(
)
switch protocol {
case connection.QUIC:
case connection.QUIC, connection.QUICWarp:
connOptions := config.ConnectionOptions(addr.UDP.String(), uint8(backoff.Retries()))
return ServeQUIC(ctx,
addr.UDP,
config,
connOptions,
controlStream,
connIndex,
reconnectCh,
gracefulShutdownC)
case connection.HTTP2:
case connection.HTTP2, connection.HTTP2Warp:
edgeConn, err := edgediscovery.DialEdge(ctx, dialTimeout, config.EdgeTLSConfigs[protocol], addr.TCP)
if err != nil {
connLog.Err(err).Msg("Unable to establish connection with Cloudflare edge")
@@ -508,6 +510,7 @@ func ServeQUIC(
config *TunnelConfig,
connOptions *tunnelpogs.ConnectionOptions,
controlStreamHandler connection.ControlStreamHandler,
connIndex uint8,
reconnectCh chan ReconnectSignal,
gracefulShutdownC <-chan struct{},
) (err error, recoverable bool) {
@@ -518,6 +521,7 @@ func ServeQUIC(
MaxIncomingStreams: connection.MaxConcurrentStreams,
MaxIncomingUniStreams: connection.MaxConcurrentStreams,
KeepAlive: true,
Tracer: quicpogs.NewClientTracer(config.Log, connIndex),
}
for {
select {
@@ -559,19 +563,6 @@ func ServeQUIC(
}
}
type quicLogger struct {
*zerolog.Logger
}
func (ql *quicLogger) Write(p []byte) (n int, err error) {
ql.Debug().Msgf("quic log: %v", string(p))
return len(p), nil
}
func (ql *quicLogger) Close() error {
return nil
}
func listenReconnect(ctx context.Context, reconnectCh <-chan ReconnectSignal, gracefulShutdownCh <-chan struct{}) error {
select {
case reconnect := <-reconnectCh:
+6 -8
View File
@@ -8,20 +8,18 @@ import (
"github.com/stretchr/testify/assert"
"github.com/cloudflare/cloudflared/connection"
"github.com/cloudflare/cloudflared/edgediscovery"
"github.com/cloudflare/cloudflared/retry"
)
type dynamicMockFetcher struct {
percentage int32
err error
protocolPercents edgediscovery.ProtocolPercents
err error
}
func (dmf *dynamicMockFetcher) fetch() connection.PercentageFetcher {
return func() (int32, error) {
if dmf.err != nil {
return 0, dmf.err
}
return dmf.percentage, nil
return func() (edgediscovery.ProtocolPercents, error) {
return dmf.protocolPercents, dmf.err
}
}
@@ -39,7 +37,7 @@ func TestWaitForBackoffFallback(t *testing.T) {
},
}
mockFetcher := dynamicMockFetcher{
percentage: 0,
protocolPercents: edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}},
}
warpRoutingEnabled := false
protocolSelector, err := connection.NewProtocolSelector(
+99
View File
@@ -0,0 +1,99 @@
package quic
import (
"strconv"
"time"
"github.com/lucas-clemente/quic-go/logging"
)
func perspectiveString(p logging.Perspective) string {
switch p {
case logging.PerspectiveClient:
return "client"
case logging.PerspectiveServer:
return "server"
default:
return ""
}
}
// Helper to convert logging.ByteCount(alias for int64) to float64 used in prometheus
func byteCountToPromCount(count logging.ByteCount) float64 {
return float64(count)
}
// Helper to convert Duration to float64 used in prometheus
func durationToPromGauge(duration time.Duration) float64 {
return float64(duration.Milliseconds())
}
// Helper to convert https://pkg.go.dev/github.com/lucas-clemente/quic-go@v0.23.0/logging#PacketType into string
func packetTypeString(pt logging.PacketType) string {
switch pt {
case logging.PacketTypeInitial:
return "initial"
case logging.PacketTypeHandshake:
return "handshake"
case logging.PacketTypeRetry:
return "retry"
case logging.PacketType0RTT:
return "0_rtt"
case logging.PacketTypeVersionNegotiation:
return "version_negotiation"
case logging.PacketType1RTT:
return "1_rtt"
case logging.PacketTypeStatelessReset:
return "stateless_reset"
case logging.PacketTypeNotDetermined:
return "undetermined"
default:
return "unknown_packet_type"
}
}
// Helper to convert https://pkg.go.dev/github.com/lucas-clemente/quic-go@v0.23.0/logging#PacketDropReason into string
func packetDropReasonString(reason logging.PacketDropReason) string {
switch reason {
case logging.PacketDropKeyUnavailable:
return "key_unavailable"
case logging.PacketDropUnknownConnectionID:
return "unknown_conn_id"
case logging.PacketDropHeaderParseError:
return "header_parse_err"
case logging.PacketDropPayloadDecryptError:
return "payload_decrypt_err"
case logging.PacketDropProtocolViolation:
return "protocol_violation"
case logging.PacketDropDOSPrevention:
return "dos_prevention"
case logging.PacketDropUnsupportedVersion:
return "unsupported_version"
case logging.PacketDropUnexpectedPacket:
return "unexpected_packet"
case logging.PacketDropUnexpectedSourceConnectionID:
return "unexpected_src_conn_id"
case logging.PacketDropUnexpectedVersion:
return "unexpected_version"
case logging.PacketDropDuplicate:
return "duplicate"
default:
return "unknown_reason"
}
}
// Helper to convert https://pkg.go.dev/github.com/lucas-clemente/quic-go@v0.23.0/logging#PacketLossReason into string
func packetLossReasonString(reason logging.PacketLossReason) string {
switch reason {
case logging.PacketLossReorderingThreshold:
return "reordering"
case logging.PacketLossTimeThreshold:
return "timeout"
default:
return "unknown_loss_reason"
}
}
func uint8ToString(input uint8) string {
return strconv.FormatUint(uint64(input), 10)
}
+373
View File
@@ -0,0 +1,373 @@
package quic
import (
"sync"
"github.com/lucas-clemente/quic-go/logging"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "quic"
)
var (
clientConnLabels = []string{"conn_index"}
clientMetrics = struct {
totalConnections prometheus.Counter
closedConnections *prometheus.CounterVec
sentPackets *prometheus.CounterVec
sentBytes *prometheus.CounterVec
receivePackets *prometheus.CounterVec
receiveBytes *prometheus.CounterVec
bufferedPackets *prometheus.CounterVec
droppedPackets *prometheus.CounterVec
lostPackets *prometheus.CounterVec
minRTT *prometheus.GaugeVec
latestRTT *prometheus.GaugeVec
smoothedRTT *prometheus.GaugeVec
}{
totalConnections: prometheus.NewCounter(
totalConnectionsOpts(logging.PerspectiveClient),
),
closedConnections: prometheus.NewCounterVec(
closedConnectionsOpts(logging.PerspectiveClient),
[]string{"error"},
),
sentPackets: prometheus.NewCounterVec(
sentPacketsOpts(logging.PerspectiveClient),
clientConnLabels,
),
sentBytes: prometheus.NewCounterVec(
sentBytesOpts(logging.PerspectiveClient),
clientConnLabels,
),
receivePackets: prometheus.NewCounterVec(
receivePacketsOpts(logging.PerspectiveClient),
clientConnLabels,
),
receiveBytes: prometheus.NewCounterVec(
receiveBytesOpts(logging.PerspectiveClient),
clientConnLabels,
),
bufferedPackets: prometheus.NewCounterVec(
bufferedPacketsOpts(logging.PerspectiveClient),
append(clientConnLabels, "packet_type"),
),
droppedPackets: prometheus.NewCounterVec(
droppedPacketsOpts(logging.PerspectiveClient),
append(clientConnLabels, "packet_type", "reason"),
),
lostPackets: prometheus.NewCounterVec(
lostPacketsOpts(logging.PerspectiveClient),
append(clientConnLabels, "reason"),
),
minRTT: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: perspectiveString(logging.PerspectiveClient),
Name: "min_rtt",
Help: "Lowest RTT measured on a connection in millisec",
},
clientConnLabels,
),
latestRTT: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: perspectiveString(logging.PerspectiveClient),
Name: "latest_rtt",
Help: "Latest RTT measured on a connection",
},
clientConnLabels,
),
smoothedRTT: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: perspectiveString(logging.PerspectiveClient),
Name: "smoothed_rtt",
Help: "Calculated smoothed RTT measured on a connection in millisec",
},
clientConnLabels,
),
}
// The server has many QUIC connections. Adding per connection label incurs high memory cost
serverMetrics = struct {
totalConnections prometheus.Counter
closedConnections prometheus.Counter
sentPackets prometheus.Counter
sentBytes prometheus.Counter
receivePackets prometheus.Counter
receiveBytes prometheus.Counter
bufferedPackets *prometheus.CounterVec
droppedPackets *prometheus.CounterVec
lostPackets *prometheus.CounterVec
rtt prometheus.Histogram
}{
totalConnections: prometheus.NewCounter(
totalConnectionsOpts(logging.PerspectiveServer),
),
closedConnections: prometheus.NewCounter(
closedConnectionsOpts(logging.PerspectiveServer),
),
sentPackets: prometheus.NewCounter(
sentPacketsOpts(logging.PerspectiveServer),
),
sentBytes: prometheus.NewCounter(
sentBytesOpts(logging.PerspectiveServer),
),
receivePackets: prometheus.NewCounter(
receivePacketsOpts(logging.PerspectiveServer),
),
receiveBytes: prometheus.NewCounter(
receiveBytesOpts(logging.PerspectiveServer),
),
bufferedPackets: prometheus.NewCounterVec(
bufferedPacketsOpts(logging.PerspectiveServer),
[]string{"packet_type"},
),
droppedPackets: prometheus.NewCounterVec(
droppedPacketsOpts(logging.PerspectiveServer),
[]string{"packet_type", "reason"},
),
lostPackets: prometheus.NewCounterVec(
lostPacketsOpts(logging.PerspectiveServer),
[]string{"reason"},
),
rtt: prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: perspectiveString(logging.PerspectiveServer),
Name: "rtt",
Buckets: []float64{5, 10, 20, 30, 40, 50, 75, 100},
},
),
}
registerClient = sync.Once{}
registerServer = sync.Once{}
)
// MetricsCollector abstracts the difference between client and server metrics from connTracer
type MetricsCollector interface {
startedConnection()
closedConnection(err error)
sentPackets(logging.ByteCount)
receivedPackets(logging.ByteCount)
bufferedPackets(logging.PacketType)
droppedPackets(logging.PacketType, logging.ByteCount, logging.PacketDropReason)
lostPackets(logging.PacketLossReason)
updatedRTT(*logging.RTTStats)
}
func totalConnectionsOpts(p logging.Perspective) prometheus.CounterOpts {
var help string
if p == logging.PerspectiveClient {
help = "Number of connections initiated. For all quic metrics, client means the side initiating the connection"
} else {
help = "Number of connections accepted. For all quic metrics, server means the side accepting connections"
}
return prometheus.CounterOpts{
Namespace: namespace,
Subsystem: perspectiveString(p),
Name: "total_connections",
Help: help,
}
}
func closedConnectionsOpts(p logging.Perspective) prometheus.CounterOpts {
return prometheus.CounterOpts{
Namespace: namespace,
Subsystem: perspectiveString(p),
Name: "closed_connections",
Help: "Number of connections that has been closed",
}
}
func sentPacketsOpts(p logging.Perspective) prometheus.CounterOpts {
return prometheus.CounterOpts{
Namespace: namespace,
Subsystem: perspectiveString(p),
Name: "sent_packets",
Help: "Number of packets that have been sent through a connection",
}
}
func sentBytesOpts(p logging.Perspective) prometheus.CounterOpts {
return prometheus.CounterOpts{
Namespace: namespace,
Subsystem: perspectiveString(p),
Name: "sent_bytes",
Help: "Number of bytes that have been sent through a connection",
}
}
func receivePacketsOpts(p logging.Perspective) prometheus.CounterOpts {
return prometheus.CounterOpts{
Namespace: namespace,
Subsystem: perspectiveString(p),
Name: "receive_packets",
Help: "Number of packets that have been received through a connection",
}
}
func receiveBytesOpts(p logging.Perspective) prometheus.CounterOpts {
return prometheus.CounterOpts{
Namespace: namespace,
Subsystem: perspectiveString(p),
Name: "receive_bytes",
Help: "Number of bytes that have been received through a connection",
}
}
func bufferedPacketsOpts(p logging.Perspective) prometheus.CounterOpts {
return prometheus.CounterOpts{
Namespace: namespace,
Subsystem: perspectiveString(p),
Name: "buffered_packets",
Help: "Number of bytes that have been buffered on a connection",
}
}
func droppedPacketsOpts(p logging.Perspective) prometheus.CounterOpts {
return prometheus.CounterOpts{
Namespace: namespace,
Subsystem: perspectiveString(p),
Name: "dropped_packets",
Help: "Number of bytes that have been dropped on a connection",
}
}
func lostPacketsOpts(p logging.Perspective) prometheus.CounterOpts {
return prometheus.CounterOpts{
Namespace: namespace,
Subsystem: perspectiveString(p),
Name: "lost_packets",
Help: "Number of packets that have been lost from a connection",
}
}
type clientCollector struct {
index string
}
func newClientCollector(index uint8) MetricsCollector {
registerClient.Do(func() {
prometheus.MustRegister(
clientMetrics.totalConnections,
clientMetrics.closedConnections,
clientMetrics.sentPackets,
clientMetrics.sentBytes,
clientMetrics.receivePackets,
clientMetrics.receiveBytes,
clientMetrics.bufferedPackets,
clientMetrics.droppedPackets,
clientMetrics.lostPackets,
clientMetrics.minRTT,
clientMetrics.latestRTT,
clientMetrics.smoothedRTT,
)
})
return &clientCollector{
index: uint8ToString(index),
}
}
func (cc *clientCollector) startedConnection() {
clientMetrics.totalConnections.Inc()
}
func (cc *clientCollector) closedConnection(err error) {
clientMetrics.closedConnections.WithLabelValues(err.Error()).Inc()
}
func (cc *clientCollector) sentPackets(size logging.ByteCount) {
clientMetrics.sentPackets.WithLabelValues(cc.index).Inc()
clientMetrics.sentBytes.WithLabelValues(cc.index).Add(byteCountToPromCount(size))
}
func (cc *clientCollector) receivedPackets(size logging.ByteCount) {
clientMetrics.receivePackets.WithLabelValues(cc.index).Inc()
clientMetrics.receiveBytes.WithLabelValues(cc.index).Add(byteCountToPromCount(size))
}
func (cc *clientCollector) bufferedPackets(packetType logging.PacketType) {
clientMetrics.bufferedPackets.WithLabelValues(cc.index, packetTypeString(packetType)).Inc()
}
func (cc *clientCollector) droppedPackets(packetType logging.PacketType, size logging.ByteCount, reason logging.PacketDropReason) {
clientMetrics.droppedPackets.WithLabelValues(
cc.index,
packetTypeString(packetType),
packetDropReasonString(reason),
).Add(byteCountToPromCount(size))
}
func (cc *clientCollector) lostPackets(reason logging.PacketLossReason) {
clientMetrics.lostPackets.WithLabelValues(cc.index, packetLossReasonString(reason)).Inc()
}
func (cc *clientCollector) updatedRTT(rtt *logging.RTTStats) {
clientMetrics.minRTT.WithLabelValues(cc.index).Set(durationToPromGauge(rtt.MinRTT()))
clientMetrics.latestRTT.WithLabelValues(cc.index).Set(durationToPromGauge(rtt.LatestRTT()))
clientMetrics.smoothedRTT.WithLabelValues(cc.index).Set(durationToPromGauge(rtt.SmoothedRTT()))
}
type serverCollector struct{}
func newServiceCollector() MetricsCollector {
registerServer.Do(func() {
prometheus.MustRegister(
serverMetrics.totalConnections,
serverMetrics.closedConnections,
serverMetrics.sentPackets,
serverMetrics.sentBytes,
serverMetrics.receivePackets,
serverMetrics.receiveBytes,
serverMetrics.bufferedPackets,
serverMetrics.droppedPackets,
serverMetrics.lostPackets,
serverMetrics.rtt,
)
})
return &serverCollector{}
}
func (sc *serverCollector) startedConnection() {
serverMetrics.totalConnections.Inc()
}
func (sc *serverCollector) closedConnection(err error) {
serverMetrics.closedConnections.Inc()
}
func (sc *serverCollector) sentPackets(size logging.ByteCount) {
serverMetrics.sentPackets.Inc()
serverMetrics.sentBytes.Add(byteCountToPromCount(size))
}
func (sc *serverCollector) receivedPackets(size logging.ByteCount) {
serverMetrics.receivePackets.Inc()
serverMetrics.receiveBytes.Add(byteCountToPromCount(size))
}
func (sc *serverCollector) bufferedPackets(packetType logging.PacketType) {
serverMetrics.bufferedPackets.WithLabelValues(packetTypeString(packetType)).Inc()
}
func (sc *serverCollector) droppedPackets(packetType logging.PacketType, size logging.ByteCount, reason logging.PacketDropReason) {
serverMetrics.droppedPackets.WithLabelValues(
packetTypeString(packetType),
packetDropReasonString(reason),
).Add(byteCountToPromCount(size))
}
func (sc *serverCollector) lostPackets(reason logging.PacketLossReason) {
serverMetrics.lostPackets.WithLabelValues(packetLossReasonString(reason)).Inc()
}
func (sc *serverCollector) updatedRTT(rtt *logging.RTTStats) {
latestRTT := rtt.LatestRTT()
// May return 0 if no valid updates have occured
if latestRTT > 0 {
serverMetrics.rtt.Observe(durationToPromGauge(latestRTT))
}
}
+126
View File
@@ -0,0 +1,126 @@
package quic
import (
"context"
"net"
"github.com/lucas-clemente/quic-go/logging"
"github.com/lucas-clemente/quic-go/qlog"
"github.com/rs/zerolog"
)
// QUICTracer is a wrapper to create new quicConnTracer
type tracer struct {
logger *zerolog.Logger
config *tracerConfig
}
type tracerConfig struct {
isClient bool
// Only client has an index
index uint8
}
func NewClientTracer(logger *zerolog.Logger, index uint8) logging.Tracer {
return &tracer{
logger: logger,
config: &tracerConfig{
isClient: true,
index: index,
},
}
}
func NewServerTracer(logger *zerolog.Logger) logging.Tracer {
return &tracer{
logger: logger,
config: &tracerConfig{
isClient: false,
},
}
}
func (t *tracer) TracerForConnection(_ context.Context, p logging.Perspective, odcid logging.ConnectionID) logging.ConnectionTracer {
connID := logging.ConnectionID(odcid).String()
ql := &quicLogger{
logger: t.logger,
connectionID: connID,
}
if t.config.isClient {
return newConnTracer(ql, p, odcid, newClientCollector(t.config.index))
}
return newConnTracer(ql, p, odcid, newServiceCollector())
}
func (*tracer) SentPacket(net.Addr, *logging.Header, logging.ByteCount, []logging.Frame) {}
func (*tracer) DroppedPacket(net.Addr, logging.PacketType, logging.ByteCount, logging.PacketDropReason) {
}
// connTracer is a wrapper around https://pkg.go.dev/github.com/lucas-clemente/quic-go@v0.23.0/qlog#NewConnectionTracer to collect metrics
type connTracer struct {
logging.ConnectionTracer
metricsCollector MetricsCollector
connectionID string
}
func newConnTracer(ql *quicLogger, p logging.Perspective, odcid logging.ConnectionID, metricsCollector MetricsCollector) logging.ConnectionTracer {
return &connTracer{
qlog.NewConnectionTracer(ql, p, odcid),
metricsCollector,
logging.ConnectionID(odcid).String(),
}
}
func (ct *connTracer) StartedConnection(local, remote net.Addr, srcConnID, destConnID logging.ConnectionID) {
ct.metricsCollector.startedConnection()
ct.ConnectionTracer.StartedConnection(local, remote, srcConnID, destConnID)
}
func (ct *connTracer) ClosedConnection(err error) {
ct.metricsCollector.closedConnection(err)
ct.ConnectionTracer.ClosedConnection(err)
}
func (ct *connTracer) SentPacket(hdr *logging.ExtendedHeader, packetSize logging.ByteCount, ack *logging.AckFrame, frames []logging.Frame) {
ct.metricsCollector.sentPackets(packetSize)
ct.ConnectionTracer.SentPacket(hdr, packetSize, ack, frames)
}
func (ct *connTracer) ReceivedPacket(hdr *logging.ExtendedHeader, size logging.ByteCount, frames []logging.Frame) {
ct.metricsCollector.receivedPackets(size)
ct.ConnectionTracer.ReceivedPacket(hdr, size, frames)
}
func (ct *connTracer) BufferedPacket(pt logging.PacketType) {
ct.metricsCollector.bufferedPackets(pt)
ct.ConnectionTracer.BufferedPacket(pt)
}
func (ct *connTracer) DroppedPacket(pt logging.PacketType, size logging.ByteCount, reason logging.PacketDropReason) {
ct.metricsCollector.droppedPackets(pt, size, reason)
ct.ConnectionTracer.DroppedPacket(pt, size, reason)
}
func (ct *connTracer) LostPacket(level logging.EncryptionLevel, number logging.PacketNumber, reason logging.PacketLossReason) {
ct.metricsCollector.lostPackets(reason)
ct.ConnectionTracer.LostPacket(level, number, reason)
}
func (ct *connTracer) UpdatedMetrics(rttStats *logging.RTTStats, cwnd, bytesInFlight logging.ByteCount, packetsInFlight int) {
ct.metricsCollector.updatedRTT(rttStats)
ct.ConnectionTracer.UpdatedMetrics(rttStats, cwnd, bytesInFlight, packetsInFlight)
}
type quicLogger struct {
logger *zerolog.Logger
connectionID string
}
func (qt *quicLogger) Write(p []byte) (n int, err error) {
qt.logger.Trace().Str("quicConnection", qt.connectionID).RawJSON("event", p).Msg("Quic event")
return len(p), nil
}
func (*quicLogger) Close() error {
return nil
}
+5
View File
@@ -2,6 +2,7 @@ package tunnelstore
import (
"net/url"
"strconv"
"time"
"github.com/google/uuid"
@@ -45,6 +46,10 @@ func (f *Filter) ByTunnelID(tunnelID uuid.UUID) {
f.queryParams.Set("uuid", tunnelID.String())
}
func (f *Filter) MaxFetchSize(max uint) {
f.queryParams.Set("per_page", strconv.Itoa(int(max)))
}
func (f Filter) encode() string {
return f.queryParams.Encode()
}
+5
View File
@@ -0,0 +1,5 @@
vendor
*.out
*.log
*.test
.vscode
+15
View File
@@ -0,0 +1,15 @@
language: go
go:
- "1.10.x"
- "1.11.x"
- "1.12.x"
script:
- go get github.com/golang/dep/cmd/dep github.com/stretchr/testify
- dep ensure -v -vendor-only
- go test ./gojay/codegen/test/... -race
- go test -race -coverprofile=coverage.txt -covermode=atomic
after_success:
- bash <(curl -s https://codecov.io/bash)
+163
View File
@@ -0,0 +1,163 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
digest = "1:1a37f9f2ae10d161d9688fb6008ffa14e1631e5068cc3e9698008b9e8d40d575"
name = "cloud.google.com/go"
packages = ["compute/metadata"]
pruneopts = ""
revision = "457ea5c15ccf3b87db582c450e80101989da35f7"
version = "v0.40.0"
[[projects]]
digest = "1:968d8903d598e3fae738325d3410f33f07ea6a2b9ee5591e9c262ee37df6845a"
name = "github.com/go-errors/errors"
packages = ["."]
pruneopts = ""
revision = "a6af135bd4e28680facf08a3d206b454abc877a4"
version = "v1.0.1"
[[projects]]
digest = "1:529d738b7976c3848cae5cf3a8036440166835e389c1f617af701eeb12a0518d"
name = "github.com/golang/protobuf"
packages = ["proto"]
pruneopts = ""
revision = "b5d812f8a3706043e23a9cd5babf2e5423744d30"
version = "v1.3.1"
[[projects]]
branch = "master"
digest = "1:cae59d7b8243c671c9f544965522ba35c0fec48ee80adb9f1400cd2f33abbbec"
name = "github.com/mailru/easyjson"
packages = [
".",
"buffer",
"jlexer",
"jwriter",
]
pruneopts = ""
revision = "1ea4449da9834f4d333f1cc461c374aea217d249"
[[projects]]
digest = "1:1d7e1867c49a6dd9856598ef7c3123604ea3daabf5b83f303ff457bcbc410b1d"
name = "github.com/pkg/errors"
packages = ["."]
pruneopts = ""
revision = "ba968bfe8b2f7e042a574c888954fccecfa385b4"
version = "v0.8.1"
[[projects]]
digest = "1:8d4bbd8ab012efc77ab6b97286f2aff262bcdeac9803bb57d75cf7d0a5e6a877"
name = "github.com/viant/assertly"
packages = ["."]
pruneopts = ""
revision = "04f45e0aeb6f3455884877b047a97bcc95dc9493"
version = "v0.4.8"
[[projects]]
digest = "1:5913451bc2d274673c0716efe226a137625740cd9380641f4d8300ff4f2d82a0"
name = "github.com/viant/toolbox"
packages = [
".",
"cred",
"data",
"storage",
"url",
]
pruneopts = ""
revision = "1be8e4d172138324f40d55ea61a2aeab0c5ce864"
version = "v0.24.0"
[[projects]]
branch = "master"
digest = "1:9d150270ca2c3356f2224a0878daa1652e4d0b25b345f18b4f6e156cc4b8ec5e"
name = "golang.org/x/crypto"
packages = [
"blowfish",
"curve25519",
"ed25519",
"ed25519/internal/edwards25519",
"internal/chacha20",
"internal/subtle",
"poly1305",
"ssh",
]
pruneopts = ""
revision = "f99c8df09eb5bff426315721bfa5f16a99cad32c"
[[projects]]
branch = "master"
digest = "1:5a56f211e7c12a65c5585c629457a2fb91d8719844ee8fab92727ea8adb5721c"
name = "golang.org/x/net"
packages = [
"context",
"context/ctxhttp",
"websocket",
]
pruneopts = ""
revision = "461777fb6f67e8cb9d70cda16573678d085a74cf"
[[projects]]
branch = "master"
digest = "1:01bdbbc604dcd5afb6f66a717f69ad45e9643c72d5bc11678d44ffa5c50f9e42"
name = "golang.org/x/oauth2"
packages = [
".",
"google",
"internal",
"jws",
"jwt",
]
pruneopts = ""
revision = "0f29369cfe4552d0e4bcddc57cc75f4d7e672a33"
[[projects]]
branch = "master"
digest = "1:8ddb956f67d4c176abbbc42b7514aaeaf9ea30daa24e27d2cf30ad82f9334a2c"
name = "golang.org/x/sys"
packages = ["cpu"]
pruneopts = ""
revision = "1e42afee0f762ed3d76e6dd942e4181855fd1849"
[[projects]]
digest = "1:47f391ee443f578f01168347818cb234ed819521e49e4d2c8dd2fb80d48ee41a"
name = "google.golang.org/appengine"
packages = [
".",
"internal",
"internal/app_identity",
"internal/base",
"internal/datastore",
"internal/log",
"internal/modules",
"internal/remote_api",
"internal/urlfetch",
"urlfetch",
]
pruneopts = ""
revision = "b2f4a3cf3c67576a2ee09e1fe62656a5086ce880"
version = "v1.6.1"
[[projects]]
digest = "1:cedccf16b71e86db87a24f8d4c70b0a855872eb967cb906a66b95de56aefbd0d"
name = "gopkg.in/yaml.v2"
packages = ["."]
pruneopts = ""
revision = "51d6538a90f86fe93ac480b35f37b2be17fef232"
version = "v2.2.2"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
input-imports = [
"github.com/go-errors/errors",
"github.com/mailru/easyjson",
"github.com/mailru/easyjson/jlexer",
"github.com/mailru/easyjson/jwriter",
"github.com/viant/assertly",
"github.com/viant/toolbox",
"github.com/viant/toolbox/url",
"golang.org/x/net/websocket",
]
solver-name = "gps-cdcl"
solver-version = 1
+23
View File
@@ -0,0 +1,23 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
ignored = ["github.com/francoispqt/benchmarks*","github.com/stretchr/testify*","github.com/stretchr/testify","github.com/json-iterator/go","github.com/buger/jsonparser"]
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2016 gojay
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+11
View File
@@ -0,0 +1,11 @@
.PHONY: test
test:
go test -race -run=^Test -v
.PHONY: cover
cover:
go test -coverprofile=coverage.out -covermode=atomic
.PHONY: coverhtml
coverhtml:
go tool cover -html=coverage.out
+855
View File
@@ -0,0 +1,855 @@
[![Build Status](https://travis-ci.org/francoispqt/gojay.svg?branch=master)](https://travis-ci.org/francoispqt/gojay)
[![codecov](https://codecov.io/gh/francoispqt/gojay/branch/master/graph/badge.svg)](https://codecov.io/gh/francoispqt/gojay)
[![Go Report Card](https://goreportcard.com/badge/github.com/francoispqt/gojay)](https://goreportcard.com/report/github.com/francoispqt/gojay)
[![Go doc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square
)](https://godoc.org/github.com/francoispqt/gojay)
![MIT License](https://img.shields.io/badge/license-mit-blue.svg?style=flat-square)
[![Sourcegraph](https://sourcegraph.com/github.com/francoispqt/gojay/-/badge.svg)](https://sourcegraph.com/github.com/francoispqt/gojay)
![stability-stable](https://img.shields.io/badge/stability-stable-green.svg)
# GoJay
<img src="https://github.com/francoispqt/gojay/raw/master/gojay.png" width="200px">
GoJay is a performant JSON encoder/decoder for Golang (currently the most performant, [see benchmarks](#benchmark-results)).
It has a simple API and doesn't use reflection. It relies on small interfaces to decode/encode structures and slices.
Gojay also comes with powerful stream decoding features and an even faster [Unsafe](#unsafe-api) API.
There is also a [code generation tool](https://github.com/francoispqt/gojay/tree/master/gojay) to make usage easier and faster.
# Why another JSON parser?
I looked at other fast decoder/encoder and realised it was mostly hardly readable static code generation or a lot of reflection, poor streaming features, and not so fast in the end.
Also, I wanted to build a decoder that could consume an io.Reader of line or comma delimited JSON, in a JIT way. To consume a flow of JSON objects from a TCP connection for example or from a standard output. Same way I wanted to build an encoder that could encode a flow of data to a io.Writer.
This is how GoJay aims to be a very fast, JIT stream parser with 0 reflection, low allocation with a friendly API.
# Get started
```bash
go get github.com/francoispqt/gojay
```
* [Encoder](#encoding)
* [Decoder](#decoding)
* [Stream API](#stream-api)
* [Code Generation](https://github.com/francoispqt/gojay/tree/master/gojay)
## Decoding
Decoding is done through two different API similar to standard `encoding/json`:
* [Unmarshal](#unmarshal-api)
* [Decode](#decode-api)
Example of basic stucture decoding with Unmarshal:
```go
import "github.com/francoispqt/gojay"
type user struct {
id int
name string
email string
}
// implement gojay.UnmarshalerJSONObject
func (u *user) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
switch key {
case "id":
return dec.Int(&u.id)
case "name":
return dec.String(&u.name)
case "email":
return dec.String(&u.email)
}
return nil
}
func (u *user) NKeys() int {
return 3
}
func main() {
u := &user{}
d := []byte(`{"id":1,"name":"gojay","email":"gojay@email.com"}`)
err := gojay.UnmarshalJSONObject(d, u)
if err != nil {
log.Fatal(err)
}
}
```
with Decode:
```go
func main() {
u := &user{}
dec := gojay.NewDecoder(bytes.NewReader([]byte(`{"id":1,"name":"gojay","email":"gojay@email.com"}`)))
err := dec.DecodeObject(d, u)
if err != nil {
log.Fatal(err)
}
}
```
### Unmarshal API
Unmarshal API decodes a `[]byte` to a given pointer with a single function.
Behind the doors, Unmarshal API borrows a `*gojay.Decoder` resets its settings and decodes the data to the given pointer and releases the `*gojay.Decoder` to the pool when it finishes, whether it encounters an error or not.
If it cannot find the right Decoding strategy for the type of the given pointer, it returns an `InvalidUnmarshalError`. You can test the error returned by doing `if ok := err.(InvalidUnmarshalError); ok {}`.
Unmarshal API comes with three functions:
* Unmarshal
```go
func Unmarshal(data []byte, v interface{}) error
```
* UnmarshalJSONObject
```go
func UnmarshalJSONObject(data []byte, v gojay.UnmarshalerJSONObject) error
```
* UnmarshalJSONArray
```go
func UnmarshalJSONArray(data []byte, v gojay.UnmarshalerJSONArray) error
```
### Decode API
Decode API decodes a `[]byte` to a given pointer by creating or borrowing a `*gojay.Decoder` with an `io.Reader` and calling `Decode` methods.
__Getting a *gojay.Decoder or Borrowing__
You can either get a fresh `*gojay.Decoder` calling `dec := gojay.NewDecoder(io.Reader)` or borrow one from the pool by calling `dec := gojay.BorrowDecoder(io.Reader)`.
After using a decoder, you can release it by calling `dec.Release()`. Beware, if you reuse the decoder after releasing it, it will panic with an error of type `InvalidUsagePooledDecoderError`. If you want to fully benefit from the pooling, you must release your decoders after using.
Example getting a fresh an releasing:
```go
str := ""
dec := gojay.NewDecoder(strings.NewReader(`"test"`))
defer dec.Release()
if err := dec.Decode(&str); err != nil {
log.Fatal(err)
}
```
Example borrowing a decoder and releasing:
```go
str := ""
dec := gojay.BorrowDecoder(strings.NewReader(`"test"`))
defer dec.Release()
if err := dec.Decode(&str); err != nil {
log.Fatal(err)
}
```
`*gojay.Decoder` has multiple methods to decode to specific types:
* Decode
```go
func (dec *gojay.Decoder) Decode(v interface{}) error
```
* DecodeObject
```go
func (dec *gojay.Decoder) DecodeObject(v gojay.UnmarshalerJSONObject) error
```
* DecodeArray
```go
func (dec *gojay.Decoder) DecodeArray(v gojay.UnmarshalerJSONArray) error
```
* DecodeInt
```go
func (dec *gojay.Decoder) DecodeInt(v *int) error
```
* DecodeBool
```go
func (dec *gojay.Decoder) DecodeBool(v *bool) error
```
* DecodeString
```go
func (dec *gojay.Decoder) DecodeString(v *string) error
```
All DecodeXxx methods are used to decode top level JSON values. If you are decoding keys or items of a JSON object or array, don't use the Decode methods.
Example:
```go
reader := strings.NewReader(`"John Doe"`)
dec := NewDecoder(reader)
var str string
err := dec.DecodeString(&str)
if err != nil {
log.Fatal(err)
}
fmt.Println(str) // John Doe
```
### Structs and Maps
#### UnmarshalerJSONObject Interface
To unmarshal a JSON object to a structure, the structure must implement the `UnmarshalerJSONObject` interface:
```go
type UnmarshalerJSONObject interface {
UnmarshalJSONObject(*gojay.Decoder, string) error
NKeys() int
}
```
`UnmarshalJSONObject` method takes two arguments, the first one is a pointer to the Decoder (*gojay.Decoder) and the second one is the string value of the current key being parsed. If the JSON data is not an object, the UnmarshalJSONObject method will never be called.
`NKeys` method must return the number of keys to Unmarshal in the JSON object or 0. If zero is returned, all keys will be parsed.
Example of implementation for a struct:
```go
type user struct {
id int
name string
email string
}
// implement UnmarshalerJSONObject
func (u *user) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
switch key {
case "id":
return dec.Int(&u.id)
case "name":
return dec.String(&u.name)
case "email":
return dec.String(&u.email)
}
return nil
}
func (u *user) NKeys() int {
return 3
}
```
Example of implementation for a `map[string]string`:
```go
// define our custom map type implementing UnmarshalerJSONObject
type message map[string]string
// Implementing Unmarshaler
func (m message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
str := ""
err := dec.String(&str)
if err != nil {
return err
}
m[k] = str
return nil
}
// we return 0, it tells the Decoder to decode all keys
func (m message) NKeys() int {
return 0
}
```
### Arrays, Slices and Channels
To unmarshal a JSON object to a slice an array or a channel, it must implement the UnmarshalerJSONArray interface:
```go
type UnmarshalerJSONArray interface {
UnmarshalJSONArray(*gojay.Decoder) error
}
```
UnmarshalJSONArray method takes one argument, a pointer to the Decoder (*gojay.Decoder). If the JSON data is not an array, the Unmarshal method will never be called.
Example of implementation with a slice:
```go
type testSlice []string
// implement UnmarshalerJSONArray
func (t *testSlice) UnmarshalJSONArray(dec *gojay.Decoder) error {
str := ""
if err := dec.String(&str); err != nil {
return err
}
*t = append(*t, str)
return nil
}
func main() {
dec := gojay.BorrowDecoder(strings.NewReader(`["Tom", "Jim"]`))
var slice testSlice
err := dec.DecodeArray(&slice)
if err != nil {
log.Fatal(err)
}
fmt.Println(slice) // [Tom Jim]
dec.Release()
}
```
Example of implementation with a channel:
```go
type testChannel chan string
// implement UnmarshalerJSONArray
func (c testChannel) UnmarshalJSONArray(dec *gojay.Decoder) error {
str := ""
if err := dec.String(&str); err != nil {
return err
}
c <- str
return nil
}
func main() {
dec := gojay.BorrowDecoder(strings.NewReader(`["Tom", "Jim"]`))
c := make(testChannel, 2)
err := dec.DecodeArray(c)
if err != nil {
log.Fatal(err)
}
for i := 0; i < 2; i++ {
fmt.Println(<-c)
}
close(c)
dec.Release()
}
```
Example of implementation with an array:
```go
type testArray [3]string
// implement UnmarshalerJSONArray
func (a *testArray) UnmarshalJSONArray(dec *Decoder) error {
var str string
if err := dec.String(&str); err != nil {
return err
}
a[dec.Index()] = str
return nil
}
func main() {
dec := gojay.BorrowDecoder(strings.NewReader(`["Tom", "Jim", "Bob"]`))
var a testArray
err := dec.DecodeArray(&a)
fmt.Println(a) // [Tom Jim Bob]
dec.Release()
}
```
### Other types
To decode other types (string, int, int32, int64, uint32, uint64, float, booleans), you don't need to implement any interface.
Example of encoding strings:
```go
func main() {
json := []byte(`"Jay"`)
var v string
err := gojay.Unmarshal(json, &v)
if err != nil {
log.Fatal(err)
}
fmt.Println(v) // Jay
}
```
### Decode values methods
When decoding a JSON object of a JSON array using `UnmarshalerJSONObject` or `UnmarshalerJSONArray` interface, the `gojay.Decoder` provides dozens of methods to Decode multiple types.
Non exhaustive list of methods available (to see all methods, check the godoc):
```go
dec.Int
dec.Int8
dec.Int16
dec.Int32
dec.Int64
dec.Uint8
dec.Uint16
dec.Uint32
dec.Uint64
dec.String
dec.Time
dec.Bool
dec.SQLNullString
dec.SQLNullInt64
```
## Encoding
Encoding is done through two different API similar to standard `encoding/json`:
* [Marshal](#marshal-api)
* [Encode](#encode-api)
Example of basic structure encoding with Marshal:
```go
import "github.com/francoispqt/gojay"
type user struct {
id int
name string
email string
}
// implement MarshalerJSONObject
func (u *user) MarshalJSONObject(enc *gojay.Encoder) {
enc.IntKey("id", u.id)
enc.StringKey("name", u.name)
enc.StringKey("email", u.email)
}
func (u *user) IsNil() bool {
return u == nil
}
func main() {
u := &user{1, "gojay", "gojay@email.com"}
b, err := gojay.MarshalJSONObject(u)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b)) // {"id":1,"name":"gojay","email":"gojay@email.com"}
}
```
with Encode:
```go
func main() {
u := &user{1, "gojay", "gojay@email.com"}
b := strings.Builder{}
enc := gojay.NewEncoder(&b)
if err := enc.Encode(u); err != nil {
log.Fatal(err)
}
fmt.Println(b.String()) // {"id":1,"name":"gojay","email":"gojay@email.com"}
}
```
### Marshal API
Marshal API encodes a value to a JSON `[]byte` with a single function.
Behind the doors, Marshal API borrows a `*gojay.Encoder` resets its settings and encodes the data to an internal byte buffer and releases the `*gojay.Encoder` to the pool when it finishes, whether it encounters an error or not.
If it cannot find the right Encoding strategy for the type of the given value, it returns an `InvalidMarshalError`. You can test the error returned by doing `if ok := err.(InvalidMarshalError); ok {}`.
Marshal API comes with three functions:
* Marshal
```go
func Marshal(v interface{}) ([]byte, error)
```
* MarshalJSONObject
```go
func MarshalJSONObject(v gojay.MarshalerJSONObject) ([]byte, error)
```
* MarshalJSONArray
```go
func MarshalJSONArray(v gojay.MarshalerJSONArray) ([]byte, error)
```
### Encode API
Encode API decodes a value to JSON by creating or borrowing a `*gojay.Encoder` sending it to an `io.Writer` and calling `Encode` methods.
__Getting a *gojay.Encoder or Borrowing__
You can either get a fresh `*gojay.Encoder` calling `enc := gojay.NewEncoder(io.Writer)` or borrow one from the pool by calling `enc := gojay.BorrowEncoder(io.Writer)`.
After using an encoder, you can release it by calling `enc.Release()`. Beware, if you reuse the encoder after releasing it, it will panic with an error of type `InvalidUsagePooledEncoderError`. If you want to fully benefit from the pooling, you must release your encoders after using.
Example getting a fresh encoder an releasing:
```go
str := "test"
b := strings.Builder{}
enc := gojay.NewEncoder(&b)
defer enc.Release()
if err := enc.Encode(str); err != nil {
log.Fatal(err)
}
```
Example borrowing an encoder and releasing:
```go
str := "test"
b := strings.Builder{}
enc := gojay.BorrowEncoder(b)
defer enc.Release()
if err := enc.Encode(str); err != nil {
log.Fatal(err)
}
```
`*gojay.Encoder` has multiple methods to encoder specific types to JSON:
* Encode
```go
func (enc *gojay.Encoder) Encode(v interface{}) error
```
* EncodeObject
```go
func (enc *gojay.Encoder) EncodeObject(v gojay.MarshalerJSONObject) error
```
* EncodeArray
```go
func (enc *gojay.Encoder) EncodeArray(v gojay.MarshalerJSONArray) error
```
* EncodeInt
```go
func (enc *gojay.Encoder) EncodeInt(n int) error
```
* EncodeInt64
```go
func (enc *gojay.Encoder) EncodeInt64(n int64) error
```
* EncodeFloat
```go
func (enc *gojay.Encoder) EncodeFloat(n float64) error
```
* EncodeBool
```go
func (enc *gojay.Encoder) EncodeBool(v bool) error
```
* EncodeString
```go
func (enc *gojay.Encoder) EncodeString(s string) error
```
### Structs and Maps
To encode a structure, the structure must implement the MarshalerJSONObject interface:
```go
type MarshalerJSONObject interface {
MarshalJSONObject(enc *gojay.Encoder)
IsNil() bool
}
```
`MarshalJSONObject` method takes one argument, a pointer to the Encoder (*gojay.Encoder). The method must add all the keys in the JSON Object by calling Decoder's methods.
IsNil method returns a boolean indicating if the interface underlying value is nil or not. It is used to safely ensure that the underlying value is not nil without using Reflection.
Example of implementation for a struct:
```go
type user struct {
id int
name string
email string
}
// implement MarshalerJSONObject
func (u *user) MarshalJSONObject(enc *gojay.Encoder) {
enc.IntKey("id", u.id)
enc.StringKey("name", u.name)
enc.StringKey("email", u.email)
}
func (u *user) IsNil() bool {
return u == nil
}
```
Example of implementation for a `map[string]string`:
```go
// define our custom map type implementing MarshalerJSONObject
type message map[string]string
// Implementing Marshaler
func (m message) MarshalJSONObject(enc *gojay.Encoder) {
for k, v := range m {
enc.StringKey(k, v)
}
}
func (m message) IsNil() bool {
return m == nil
}
```
### Arrays and Slices
To encode an array or a slice, the slice/array must implement the MarshalerJSONArray interface:
```go
type MarshalerJSONArray interface {
MarshalJSONArray(enc *gojay.Encoder)
IsNil() bool
}
```
`MarshalJSONArray` method takes one argument, a pointer to the Encoder (*gojay.Encoder). The method must add all element in the JSON Array by calling Decoder's methods.
`IsNil` method returns a boolean indicating if the interface underlying value is nil(empty) or not. It is used to safely ensure that the underlying value is not nil without using Reflection and also to in `OmitEmpty` feature.
Example of implementation:
```go
type users []*user
// implement MarshalerJSONArray
func (u *users) MarshalJSONArray(enc *gojay.Encoder) {
for _, e := range u {
enc.Object(e)
}
}
func (u *users) IsNil() bool {
return len(u) == 0
}
```
### Other types
To encode other types (string, int, float, booleans), you don't need to implement any interface.
Example of encoding strings:
```go
func main() {
name := "Jay"
b, err := gojay.Marshal(name)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b)) // "Jay"
}
```
# Stream API
### Stream Decoding
GoJay ships with a powerful stream decoder.
It allows to read continuously from an io.Reader stream and do JIT decoding writing unmarshalled JSON to a channel to allow async consuming.
When using the Stream API, the Decoder implements context.Context to provide graceful cancellation.
To decode a stream of JSON, you must call `gojay.Stream.DecodeStream` and pass it a `UnmarshalerStream` implementation.
```go
type UnmarshalerStream interface {
UnmarshalStream(*StreamDecoder) error
}
```
Example of implementation of stream reading from a WebSocket connection:
```go
// implement UnmarshalerStream
type ChannelStream chan *user
func (c ChannelStream) UnmarshalStream(dec *gojay.StreamDecoder) error {
u := &user{}
if err := dec.Object(u); err != nil {
return err
}
c <- u
return nil
}
func main() {
// get our websocket connection
origin := "http://localhost/"
url := "ws://localhost:12345/ws"
ws, err := websocket.Dial(url, "", origin)
if err != nil {
log.Fatal(err)
}
// create our channel which will receive our objects
streamChan := ChannelStream(make(chan *user))
// borrow a decoder
dec := gojay.Stream.BorrowDecoder(ws)
// start decoding, it will block until a JSON message is decoded from the WebSocket
// or until Done channel is closed
go dec.DecodeStream(streamChan)
for {
select {
case v := <-streamChan:
// Got something from my websocket!
log.Println(v)
case <-dec.Done():
log.Println("finished reading from WebSocket")
os.Exit(0)
}
}
}
```
### Stream Encoding
GoJay ships with a powerful stream encoder part of the Stream API.
It allows to write continuously to an io.Writer and do JIT encoding of data fed to a channel to allow async consuming. You can set multiple consumers on the channel to be as performant as possible. Consumers are non blocking and are scheduled individually in their own go routine.
When using the Stream API, the Encoder implements context.Context to provide graceful cancellation.
To encode a stream of data, you must call `EncodeStream` and pass it a `MarshalerStream` implementation.
```go
type MarshalerStream interface {
MarshalStream(enc *gojay.StreamEncoder)
}
```
Example of implementation of stream writing to a WebSocket:
```go
// Our structure which will be pushed to our stream
type user struct {
id int
name string
email string
}
func (u *user) MarshalJSONObject(enc *gojay.Encoder) {
enc.IntKey("id", u.id)
enc.StringKey("name", u.name)
enc.StringKey("email", u.email)
}
func (u *user) IsNil() bool {
return u == nil
}
// Our MarshalerStream implementation
type StreamChan chan *user
func (s StreamChan) MarshalStream(enc *gojay.StreamEncoder) {
select {
case <-enc.Done():
return
case o := <-s:
enc.Object(o)
}
}
// Our main function
func main() {
// get our websocket connection
origin := "http://localhost/"
url := "ws://localhost:12345/ws"
ws, err := websocket.Dial(url, "", origin)
if err != nil {
log.Fatal(err)
}
// we borrow an encoder set stdout as the writer,
// set the number of consumer to 10
// and tell the encoder to separate each encoded element
// added to the channel by a new line character
enc := gojay.Stream.BorrowEncoder(ws).NConsumer(10).LineDelimited()
// instantiate our MarshalerStream
s := StreamChan(make(chan *user))
// start the stream encoder
// will block its goroutine until enc.Cancel(error) is called
// or until something is written to the channel
go enc.EncodeStream(s)
// write to our MarshalerStream
for i := 0; i < 1000; i++ {
s <- &user{i, "username", "user@email.com"}
}
// Wait
<-enc.Done()
}
```
# Unsafe API
Unsafe API has the same functions than the regular API, it only has `Unmarshal API` for now. It is unsafe because it makes assumptions on the quality of the given JSON.
If you are not sure if your JSON is valid, don't use the Unsafe API.
Also, the `Unsafe` API does not copy the buffer when using Unmarshal API, which, in case of string decoding, can lead to data corruption if a byte buffer is reused. Using the `Decode` API makes `Unsafe` API safer as the io.Reader relies on `copy` builtin method and `Decoder` will have its own internal buffer :)
Access the `Unsafe` API this way:
```go
gojay.Unsafe.Unmarshal(b, v)
```
# Benchmarks
Benchmarks encode and decode three different data based on size (small, medium, large).
To run benchmark for decoder:
```bash
cd $GOPATH/src/github.com/francoispqt/gojay/benchmarks/decoder && make bench
```
To run benchmark for encoder:
```bash
cd $GOPATH/src/github.com/francoispqt/gojay/benchmarks/encoder && make bench
```
# Benchmark Results
## Decode
<img src="https://images2.imgbox.com/78/01/49OExcPh_o.png" width="500px">
### Small Payload
[benchmark code is here](https://github.com/francoispqt/gojay/blob/master/benchmarks/decoder/decoder_bench_small_test.go)
[benchmark data is here](https://github.com/francoispqt/gojay/blob/master/benchmarks/benchmarks_small.go)
| | ns/op | bytes/op | allocs/op |
|-----------------|-----------|--------------|-----------|
| Std Library | 2547 | 496 | 4 |
| JsonIter | 2046 | 312 | 12 |
| JsonParser | 1408 | 0 | 0 |
| EasyJson | 929 | 240 | 2 |
| **GoJay** | **807** | **256** | **2** |
| **GoJay-unsafe**| **712** | **112** | **1** |
### Medium Payload
[benchmark code is here](https://github.com/francoispqt/gojay/blob/master/benchmarks/decoder/decoder_bench_medium_test.go)
[benchmark data is here](https://github.com/francoispqt/gojay/blob/master/benchmarks/benchmarks_medium.go)
| | ns/op | bytes/op | allocs/op |
|-----------------|-----------|----------|-----------|
| Std Library | 30148 | 2152 | 496 |
| JsonIter | 16309 | 2976 | 80 |
| JsonParser | 7793 | 0 | 0 |
| EasyJson | 7957 | 232 | 6 |
| **GoJay** | **4984** | **2448** | **8** |
| **GoJay-unsafe**| **4809** | **144** | **7** |
### Large Payload
[benchmark code is here](https://github.com/francoispqt/gojay/blob/master/benchmarks/decoder/decoder_bench_large_test.go)
[benchmark data is here](https://github.com/francoispqt/gojay/blob/master/benchmarks/benchmarks_large.go)
| | ns/op | bytes/op | allocs/op |
|-----------------|-----------|-------------|-----------|
| JsonIter | 210078 | 41712 | 1136 |
| EasyJson | 106626 | 160 | 2 |
| JsonParser | 66813 | 0 | 0 |
| **GoJay** | **52153** | **31241** | **77** |
| **GoJay-unsafe**| **48277** | **2561** | **76** |
## Encode
<img src="https://images2.imgbox.com/e9/cc/pnM8c7Gf_o.png" width="500px">
### Small Struct
[benchmark code is here](https://github.com/francoispqt/gojay/blob/master/benchmarks/encoder/encoder_bench_small_test.go)
[benchmark data is here](https://github.com/francoispqt/gojay/blob/master/benchmarks/benchmarks_small.go)
| | ns/op | bytes/op | allocs/op |
|----------------|----------|--------------|-----------|
| Std Library | 1280 | 464 | 3 |
| EasyJson | 871 | 944 | 6 |
| JsonIter | 866 | 272 | 3 |
| **GoJay** | **543** | **112** | **1** |
| **GoJay-func** | **347** | **0** | **0** |
### Medium Struct
[benchmark code is here](https://github.com/francoispqt/gojay/blob/master/benchmarks/encoder/encoder_bench_medium_test.go)
[benchmark data is here](https://github.com/francoispqt/gojay/blob/master/benchmarks/benchmarks_medium.go)
| | ns/op | bytes/op | allocs/op |
|-------------|----------|--------------|-----------|
| Std Library | 5006 | 1496 | 25 |
| JsonIter | 2232 | 1544 | 20 |
| EasyJson | 1997 | 1544 | 19 |
| **GoJay** | **1522** | **312** | **14** |
### Large Struct
[benchmark code is here](https://github.com/francoispqt/gojay/blob/master/benchmarks/encoder/encoder_bench_large_test.go)
[benchmark data is here](https://github.com/francoispqt/gojay/blob/master/benchmarks/benchmarks_large.go)
| | ns/op | bytes/op | allocs/op |
|-------------|-----------|--------------|-----------|
| Std Library | 66441 | 20576 | 332 |
| JsonIter | 35247 | 20255 | 328 |
| EasyJson | 32053 | 15474 | 327 |
| **GoJay** | **27847** | **9802** | **318** |
# Contributing
Contributions are welcome :)
If you encounter issues please report it in Github and/or send an email at [francois@parquet.ninja](mailto:francois@parquet.ninja)
+386
View File
@@ -0,0 +1,386 @@
package gojay
import (
"fmt"
"io"
)
// UnmarshalJSONArray parses the JSON-encoded data and stores the result in the value pointed to by v.
//
// v must implement UnmarshalerJSONArray.
//
// If a JSON value is not appropriate for a given target type, or if a JSON number
// overflows the target type, UnmarshalJSONArray skips that field and completes the unmarshaling as best it can.
func UnmarshalJSONArray(data []byte, v UnmarshalerJSONArray) error {
dec := borrowDecoder(nil, 0)
defer dec.Release()
dec.data = make([]byte, len(data))
copy(dec.data, data)
dec.length = len(data)
_, err := dec.decodeArray(v)
if err != nil {
return err
}
if dec.err != nil {
return dec.err
}
return nil
}
// UnmarshalJSONObject parses the JSON-encoded data and stores the result in the value pointed to by v.
//
// v must implement UnmarshalerJSONObject.
//
// If a JSON value is not appropriate for a given target type, or if a JSON number
// overflows the target type, UnmarshalJSONObject skips that field and completes the unmarshaling as best it can.
func UnmarshalJSONObject(data []byte, v UnmarshalerJSONObject) error {
dec := borrowDecoder(nil, 0)
defer dec.Release()
dec.data = make([]byte, len(data))
copy(dec.data, data)
dec.length = len(data)
_, err := dec.decodeObject(v)
if err != nil {
return err
}
if dec.err != nil {
return dec.err
}
return nil
}
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
// If v is nil, not an implementation of UnmarshalerJSONObject or UnmarshalerJSONArray or not one of the following types:
// *string, **string, *int, **int, *int8, **int8, *int16, **int16, *int32, **int32, *int64, **int64, *uint8, **uint8, *uint16, **uint16,
// *uint32, **uint32, *uint64, **uint64, *float64, **float64, *float32, **float32, *bool, **bool
// Unmarshal returns an InvalidUnmarshalError.
//
//
// If a JSON value is not appropriate for a given target type, or if a JSON number
// overflows the target type, Unmarshal skips that field and completes the unmarshaling as best it can.
// If no more serious errors are encountered, Unmarshal returns an UnmarshalTypeError describing the earliest such error.
// In any case, it's not guaranteed that all the remaining fields following the problematic one will be unmarshaled into the target object.
func Unmarshal(data []byte, v interface{}) error {
var err error
var dec *Decoder
switch vt := v.(type) {
case *string:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeString(vt)
case **string:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeStringNull(vt)
case *int:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt(vt)
case **int:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeIntNull(vt)
case *int8:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt8(vt)
case **int8:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt8Null(vt)
case *int16:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt16(vt)
case **int16:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt16Null(vt)
case *int32:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt32(vt)
case **int32:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt32Null(vt)
case *int64:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt64(vt)
case **int64:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt64Null(vt)
case *uint8:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint8(vt)
case **uint8:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint8Null(vt)
case *uint16:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint16(vt)
case **uint16:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint16Null(vt)
case *uint32:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint32(vt)
case **uint32:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint32Null(vt)
case *uint64:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint64(vt)
case **uint64:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint64Null(vt)
case *float64:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeFloat64(vt)
case **float64:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeFloat64Null(vt)
case *float32:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeFloat32(vt)
case **float32:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeFloat32Null(vt)
case *bool:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeBool(vt)
case **bool:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeBoolNull(vt)
case UnmarshalerJSONObject:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = make([]byte, len(data))
copy(dec.data, data)
_, err = dec.decodeObject(vt)
case UnmarshalerJSONArray:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = make([]byte, len(data))
copy(dec.data, data)
_, err = dec.decodeArray(vt)
case *interface{}:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = make([]byte, len(data))
copy(dec.data, data)
err = dec.decodeInterface(vt)
default:
return InvalidUnmarshalError(fmt.Sprintf(invalidUnmarshalErrorMsg, vt))
}
defer dec.Release()
if err != nil {
return err
}
return dec.err
}
// UnmarshalerJSONObject is the interface to implement to decode a JSON Object.
type UnmarshalerJSONObject interface {
UnmarshalJSONObject(*Decoder, string) error
NKeys() int
}
// UnmarshalerJSONArray is the interface to implement to decode a JSON Array.
type UnmarshalerJSONArray interface {
UnmarshalJSONArray(*Decoder) error
}
// A Decoder reads and decodes JSON values from an input stream.
type Decoder struct {
r io.Reader
data []byte
err error
isPooled byte
called byte
child byte
cursor int
length int
keysDone int
arrayIndex int
}
// Decode reads the next JSON-encoded value from the decoder's input (io.Reader) and stores it in the value pointed to by v.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
// The differences between Decode and Unmarshal are:
// - Decode reads from an io.Reader in the Decoder, whereas Unmarshal reads from a []byte
// - Decode leaves to the user the option of borrowing and releasing a Decoder, whereas Unmarshal internally always borrows a Decoder and releases it when the unmarshaling is completed
func (dec *Decoder) Decode(v interface{}) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
var err error
switch vt := v.(type) {
case *string:
err = dec.decodeString(vt)
case **string:
err = dec.decodeStringNull(vt)
case *int:
err = dec.decodeInt(vt)
case **int:
err = dec.decodeIntNull(vt)
case *int8:
err = dec.decodeInt8(vt)
case **int8:
err = dec.decodeInt8Null(vt)
case *int16:
err = dec.decodeInt16(vt)
case **int16:
err = dec.decodeInt16Null(vt)
case *int32:
err = dec.decodeInt32(vt)
case **int32:
err = dec.decodeInt32Null(vt)
case *int64:
err = dec.decodeInt64(vt)
case **int64:
err = dec.decodeInt64Null(vt)
case *uint8:
err = dec.decodeUint8(vt)
case **uint8:
err = dec.decodeUint8Null(vt)
case *uint16:
err = dec.decodeUint16(vt)
case **uint16:
err = dec.decodeUint16Null(vt)
case *uint32:
err = dec.decodeUint32(vt)
case **uint32:
err = dec.decodeUint32Null(vt)
case *uint64:
err = dec.decodeUint64(vt)
case **uint64:
err = dec.decodeUint64Null(vt)
case *float64:
err = dec.decodeFloat64(vt)
case **float64:
err = dec.decodeFloat64Null(vt)
case *float32:
err = dec.decodeFloat32(vt)
case **float32:
err = dec.decodeFloat32Null(vt)
case *bool:
err = dec.decodeBool(vt)
case **bool:
err = dec.decodeBoolNull(vt)
case UnmarshalerJSONObject:
_, err = dec.decodeObject(vt)
case UnmarshalerJSONArray:
_, err = dec.decodeArray(vt)
case *EmbeddedJSON:
err = dec.decodeEmbeddedJSON(vt)
case *interface{}:
err = dec.decodeInterface(vt)
default:
return InvalidUnmarshalError(fmt.Sprintf(invalidUnmarshalErrorMsg, vt))
}
if err != nil {
return err
}
return dec.err
}
// Non exported
func isDigit(b byte) bool {
switch b {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return true
default:
return false
}
}
func (dec *Decoder) read() bool {
if dec.r != nil {
// if we reach the end, double the buffer to ensure there's always more space
if len(dec.data) == dec.length {
nLen := dec.length * 2
if nLen == 0 {
nLen = 512
}
Buf := make([]byte, nLen, nLen)
copy(Buf, dec.data)
dec.data = Buf
}
var n int
var err error
for n == 0 {
n, err = dec.r.Read(dec.data[dec.length:])
if err != nil {
if err != io.EOF {
dec.err = err
return false
}
if n == 0 {
return false
}
dec.length = dec.length + n
return true
}
}
dec.length = dec.length + n
return true
}
return false
}
func (dec *Decoder) nextChar() byte {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
continue
}
d := dec.data[dec.cursor]
return d
}
return 0
}
+247
View File
@@ -0,0 +1,247 @@
package gojay
import "reflect"
// DecodeArray reads the next JSON-encoded value from the decoder's input (io.Reader)
// and stores it in the value pointed to by v.
//
// v must implement UnmarshalerJSONArray.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
func (dec *Decoder) DecodeArray(v UnmarshalerJSONArray) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
_, err := dec.decodeArray(v)
return err
}
func (dec *Decoder) decodeArray(arr UnmarshalerJSONArray) (int, error) {
// remember last array index in case of nested arrays
lastArrayIndex := dec.arrayIndex
dec.arrayIndex = 0
defer func() {
dec.arrayIndex = lastArrayIndex
}()
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
continue
case '[':
dec.cursor = dec.cursor + 1
// array is open, char is not space start readings
for dec.nextChar() != 0 {
// closing array
if dec.data[dec.cursor] == ']' {
dec.cursor = dec.cursor + 1
return dec.cursor, nil
}
// calling unmarshall function for each element of the slice
err := arr.UnmarshalJSONArray(dec)
if err != nil {
return 0, err
}
dec.arrayIndex++
}
return 0, dec.raiseInvalidJSONErr(dec.cursor)
case 'n':
// is null
dec.cursor++
err := dec.assertNull()
if err != nil {
return 0, err
}
return dec.cursor, nil
case '{', '"', 'f', 't', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
// can't unmarshall to struct
// we skip array and set Error
dec.err = dec.makeInvalidUnmarshalErr(arr)
err := dec.skipData()
if err != nil {
return 0, err
}
return dec.cursor, nil
default:
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
}
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) decodeArrayNull(v interface{}) (int, error) {
// remember last array index in case of nested arrays
lastArrayIndex := dec.arrayIndex
dec.arrayIndex = 0
defer func() {
dec.arrayIndex = lastArrayIndex
}()
vv := reflect.ValueOf(v)
vvt := vv.Type()
if vvt.Kind() != reflect.Ptr || vvt.Elem().Kind() != reflect.Ptr {
dec.err = ErrUnmarshalPtrExpected
return 0, dec.err
}
// not an array not an error, but do not know what to do
// do not check syntax
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
continue
case '[':
dec.cursor = dec.cursor + 1
// create our new type
elt := vv.Elem()
n := reflect.New(elt.Type().Elem())
var arr UnmarshalerJSONArray
var ok bool
if arr, ok = n.Interface().(UnmarshalerJSONArray); !ok {
dec.err = dec.makeInvalidUnmarshalErr((UnmarshalerJSONArray)(nil))
return 0, dec.err
}
// array is open, char is not space start readings
for dec.nextChar() != 0 {
// closing array
if dec.data[dec.cursor] == ']' {
elt.Set(n)
dec.cursor = dec.cursor + 1
return dec.cursor, nil
}
// calling unmarshall function for each element of the slice
err := arr.UnmarshalJSONArray(dec)
if err != nil {
return 0, err
}
dec.arrayIndex++
}
return 0, dec.raiseInvalidJSONErr(dec.cursor)
case 'n':
// is null
dec.cursor++
err := dec.assertNull()
if err != nil {
return 0, err
}
return dec.cursor, nil
case '{', '"', 'f', 't', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
// can't unmarshall to struct
// we skip array and set Error
dec.err = dec.makeInvalidUnmarshalErr((UnmarshalerJSONArray)(nil))
err := dec.skipData()
if err != nil {
return 0, err
}
return dec.cursor, nil
default:
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
}
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) skipArray() (int, error) {
var arraysOpen = 1
var arraysClosed = 0
// var stringOpen byte = 0
for j := dec.cursor; j < dec.length || dec.read(); j++ {
switch dec.data[j] {
case ']':
arraysClosed++
// everything is closed return
if arraysOpen == arraysClosed {
// add char to object data
return j + 1, nil
}
case '[':
arraysOpen++
case '"':
j++
var isInEscapeSeq bool
var isFirstQuote = true
for ; j < dec.length || dec.read(); j++ {
if dec.data[j] != '"' {
continue
}
if dec.data[j-1] != '\\' || (!isInEscapeSeq && !isFirstQuote) {
break
} else {
isInEscapeSeq = false
}
if isFirstQuote {
isFirstQuote = false
}
// loop backward and count how many anti slash found
// to see if string is effectively escaped
ct := 0
for i := j - 1; i > 0; i-- {
if dec.data[i] != '\\' {
break
}
ct++
}
// is pair number of slashes, quote is not escaped
if ct&1 == 0 {
break
}
isInEscapeSeq = true
}
default:
continue
}
}
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
// DecodeArrayFunc is a func type implementing UnmarshalerJSONArray.
// Use it to cast a `func(*Decoder) error` to Unmarshal an array on the fly.
type DecodeArrayFunc func(*Decoder) error
// UnmarshalJSONArray implements UnmarshalerJSONArray.
func (f DecodeArrayFunc) UnmarshalJSONArray(dec *Decoder) error {
return f(dec)
}
// IsNil implements UnmarshalerJSONArray.
func (f DecodeArrayFunc) IsNil() bool {
return f == nil
}
// Add Values functions
// AddArray decodes the JSON value within an object or an array to a UnmarshalerJSONArray.
func (dec *Decoder) AddArray(v UnmarshalerJSONArray) error {
return dec.Array(v)
}
// AddArrayNull decodes the JSON value within an object or an array to a UnmarshalerJSONArray.
func (dec *Decoder) AddArrayNull(v interface{}) error {
return dec.ArrayNull(v)
}
// Array decodes the JSON value within an object or an array to a UnmarshalerJSONArray.
func (dec *Decoder) Array(v UnmarshalerJSONArray) error {
newCursor, err := dec.decodeArray(v)
if err != nil {
return err
}
dec.cursor = newCursor
dec.called |= 1
return nil
}
// ArrayNull decodes the JSON value within an object or an array to a UnmarshalerJSONArray.
// v should be a pointer to an UnmarshalerJSONArray,
// if `null` value is encountered in JSON, it will leave the value v untouched,
// else it will create a new instance of the UnmarshalerJSONArray behind v.
func (dec *Decoder) ArrayNull(v interface{}) error {
newCursor, err := dec.decodeArrayNull(v)
if err != nil {
return err
}
dec.cursor = newCursor
dec.called |= 1
return nil
}
// Index returns the index of an array being decoded.
func (dec *Decoder) Index() int {
return dec.arrayIndex
}
+241
View File
@@ -0,0 +1,241 @@
package gojay
// DecodeBool reads the next JSON-encoded value from the decoder's input (io.Reader)
// and stores it in the boolean pointed to by v.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
func (dec *Decoder) DecodeBool(v *bool) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeBool(v)
}
func (dec *Decoder) decodeBool(v *bool) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
continue
case 't':
dec.cursor++
err := dec.assertTrue()
if err != nil {
return err
}
*v = true
return nil
case 'f':
dec.cursor++
err := dec.assertFalse()
if err != nil {
return err
}
*v = false
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
*v = false
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return nil
}
func (dec *Decoder) decodeBoolNull(v **bool) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
continue
case 't':
dec.cursor++
err := dec.assertTrue()
if err != nil {
return err
}
if *v == nil {
*v = new(bool)
}
**v = true
return nil
case 'f':
dec.cursor++
err := dec.assertFalse()
if err != nil {
return err
}
if *v == nil {
*v = new(bool)
}
**v = false
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return nil
}
func (dec *Decoder) assertTrue() error {
i := 0
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch i {
case 0:
if dec.data[dec.cursor] != 'r' {
return dec.raiseInvalidJSONErr(dec.cursor)
}
case 1:
if dec.data[dec.cursor] != 'u' {
return dec.raiseInvalidJSONErr(dec.cursor)
}
case 2:
if dec.data[dec.cursor] != 'e' {
return dec.raiseInvalidJSONErr(dec.cursor)
}
case 3:
switch dec.data[dec.cursor] {
case ' ', '\b', '\t', '\n', ',', ']', '}':
// dec.cursor--
return nil
default:
return dec.raiseInvalidJSONErr(dec.cursor)
}
}
i++
}
if i == 3 {
return nil
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) assertNull() error {
i := 0
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch i {
case 0:
if dec.data[dec.cursor] != 'u' {
return dec.raiseInvalidJSONErr(dec.cursor)
}
case 1:
if dec.data[dec.cursor] != 'l' {
return dec.raiseInvalidJSONErr(dec.cursor)
}
case 2:
if dec.data[dec.cursor] != 'l' {
return dec.raiseInvalidJSONErr(dec.cursor)
}
case 3:
switch dec.data[dec.cursor] {
case ' ', '\t', '\n', ',', ']', '}':
// dec.cursor--
return nil
default:
return dec.raiseInvalidJSONErr(dec.cursor)
}
}
i++
}
if i == 3 {
return nil
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) assertFalse() error {
i := 0
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch i {
case 0:
if dec.data[dec.cursor] != 'a' {
return dec.raiseInvalidJSONErr(dec.cursor)
}
case 1:
if dec.data[dec.cursor] != 'l' {
return dec.raiseInvalidJSONErr(dec.cursor)
}
case 2:
if dec.data[dec.cursor] != 's' {
return dec.raiseInvalidJSONErr(dec.cursor)
}
case 3:
if dec.data[dec.cursor] != 'e' {
return dec.raiseInvalidJSONErr(dec.cursor)
}
case 4:
switch dec.data[dec.cursor] {
case ' ', '\t', '\n', ',', ']', '}':
// dec.cursor--
return nil
default:
return dec.raiseInvalidJSONErr(dec.cursor)
}
}
i++
}
if i == 4 {
return nil
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
// Add Values functions
// AddBool decodes the JSON value within an object or an array to a *bool.
// If next key is neither null nor a JSON boolean, an InvalidUnmarshalError will be returned.
// If next key is null, bool will be false.
func (dec *Decoder) AddBool(v *bool) error {
return dec.Bool(v)
}
// AddBoolNull decodes the JSON value within an object or an array to a *bool.
// If next key is neither null nor a JSON boolean, an InvalidUnmarshalError will be returned.
// If next key is null, bool will be false.
// If a `null` is encountered, gojay does not change the value of the pointer.
func (dec *Decoder) AddBoolNull(v **bool) error {
return dec.BoolNull(v)
}
// Bool decodes the JSON value within an object or an array to a *bool.
// If next key is neither null nor a JSON boolean, an InvalidUnmarshalError will be returned.
// If next key is null, bool will be false.
func (dec *Decoder) Bool(v *bool) error {
err := dec.decodeBool(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
// BoolNull decodes the JSON value within an object or an array to a *bool.
// If next key is neither null nor a JSON boolean, an InvalidUnmarshalError will be returned.
// If next key is null, bool will be false.
func (dec *Decoder) BoolNull(v **bool) error {
err := dec.decodeBoolNull(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
+85
View File
@@ -0,0 +1,85 @@
package gojay
// EmbeddedJSON is a raw encoded JSON value.
// It can be used to delay JSON decoding or precompute a JSON encoding.
type EmbeddedJSON []byte
func (dec *Decoder) decodeEmbeddedJSON(ej *EmbeddedJSON) error {
var err error
if ej == nil {
return InvalidUnmarshalError("Invalid nil pointer given")
}
var beginOfEmbeddedJSON int
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
continue
// is null
case 'n':
beginOfEmbeddedJSON = dec.cursor
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
case 't':
beginOfEmbeddedJSON = dec.cursor
dec.cursor++
err := dec.assertTrue()
if err != nil {
return err
}
// is false
case 'f':
beginOfEmbeddedJSON = dec.cursor
dec.cursor++
err := dec.assertFalse()
if err != nil {
return err
}
// is an object
case '{':
beginOfEmbeddedJSON = dec.cursor
dec.cursor = dec.cursor + 1
dec.cursor, err = dec.skipObject()
// is string
case '"':
beginOfEmbeddedJSON = dec.cursor
dec.cursor = dec.cursor + 1
err = dec.skipString() // why no new dec.cursor in result?
// is array
case '[':
beginOfEmbeddedJSON = dec.cursor
dec.cursor = dec.cursor + 1
dec.cursor, err = dec.skipArray()
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
beginOfEmbeddedJSON = dec.cursor
dec.cursor, err = dec.skipNumber()
}
break
}
if err == nil {
if dec.cursor-1 >= beginOfEmbeddedJSON {
*ej = append(*ej, dec.data[beginOfEmbeddedJSON:dec.cursor]...)
}
dec.called |= 1
}
return err
}
// AddEmbeddedJSON adds an EmbeddedsJSON to the value pointed by v.
// It can be used to delay JSON decoding or precompute a JSON encoding.
func (dec *Decoder) AddEmbeddedJSON(v *EmbeddedJSON) error {
return dec.EmbeddedJSON(v)
}
// EmbeddedJSON adds an EmbeddedsJSON to the value pointed by v.
// It can be used to delay JSON decoding or precompute a JSON encoding.
func (dec *Decoder) EmbeddedJSON(v *EmbeddedJSON) error {
err := dec.decodeEmbeddedJSON(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
+130
View File
@@ -0,0 +1,130 @@
package gojay
// TODO @afiune for now we are using the standard json unmarshaling but in
// the future it would be great to implement one here inside this repo
import "encoding/json"
// DecodeInterface reads the next JSON-encoded value from the decoder's input (io.Reader) and stores it in the value pointed to by i.
//
// i must be an interface poiter
func (dec *Decoder) DecodeInterface(i *interface{}) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
err := dec.decodeInterface(i)
return err
}
func (dec *Decoder) decodeInterface(i *interface{}) error {
start, end, err := dec.getObject()
if err != nil {
dec.cursor = start
return err
}
// if start & end are equal the object is a null, don't unmarshal
if start == end {
return nil
}
object := dec.data[start:end]
if err = json.Unmarshal(object, i); err != nil {
return err
}
dec.cursor = end
return nil
}
// @afiune Maybe return the type as well?
func (dec *Decoder) getObject() (start int, end int, err error) {
// start cursor
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
continue
// is null
case 'n':
dec.cursor++
err = dec.assertNull()
if err != nil {
return
}
// Set start & end to the same cursor to indicate the object
// is a null and should not be unmarshal
start = dec.cursor
end = dec.cursor
return
case 't':
start = dec.cursor
dec.cursor++
err = dec.assertTrue()
if err != nil {
return
}
end = dec.cursor
dec.cursor++
return
// is false
case 'f':
start = dec.cursor
dec.cursor++
err = dec.assertFalse()
if err != nil {
return
}
end = dec.cursor
dec.cursor++
return
// is an object
case '{':
start = dec.cursor
dec.cursor++
end, err = dec.skipObject()
dec.cursor = end
return
// is string
case '"':
start = dec.cursor
dec.cursor++
start, end, err = dec.getString()
start--
dec.cursor = end
return
// is array
case '[':
start = dec.cursor
dec.cursor++
end, err = dec.skipArray()
dec.cursor = end
return
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
start = dec.cursor
end, err = dec.skipNumber()
dec.cursor = end
return
default:
err = dec.raiseInvalidJSONErr(dec.cursor)
return
}
}
err = dec.raiseInvalidJSONErr(dec.cursor)
return
}
// Add Values functions
// AddInterface decodes the JSON value within an object or an array to a interface{}.
func (dec *Decoder) AddInterface(v *interface{}) error {
return dec.Interface(v)
}
// Interface decodes the JSON value within an object or an array to an interface{}.
func (dec *Decoder) Interface(value *interface{}) error {
err := dec.decodeInterface(value)
if err != nil {
return err
}
dec.called |= 1
return nil
}
+118
View File
@@ -0,0 +1,118 @@
package gojay
import (
"math"
)
var digits []int8
const maxInt64toMultiply = math.MaxInt64 / 10
const maxInt32toMultiply = math.MaxInt32 / 10
const maxInt16toMultiply = math.MaxInt16 / 10
const maxInt8toMultiply = math.MaxInt8 / 10
const maxUint8toMultiply = math.MaxUint8 / 10
const maxUint16toMultiply = math.MaxUint16 / 10
const maxUint32toMultiply = math.MaxUint32 / 10
const maxUint64toMultiply = math.MaxUint64 / 10
const maxUint32Length = 10
const maxUint64Length = 20
const maxUint16Length = 5
const maxUint8Length = 3
const maxInt32Length = 10
const maxInt64Length = 19
const maxInt16Length = 5
const maxInt8Length = 3
const invalidNumber = int8(-1)
var pow10uint64 = [21]uint64{
0,
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000,
10000000000000,
100000000000000,
1000000000000000,
10000000000000000,
100000000000000000,
1000000000000000000,
10000000000000000000,
}
var skipNumberEndCursorIncrement [256]int
func init() {
digits = make([]int8, 256)
for i := 0; i < len(digits); i++ {
digits[i] = invalidNumber
}
for i := int8('0'); i <= int8('9'); i++ {
digits[i] = i - int8('0')
}
for i := 0; i < 256; i++ {
switch i {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'E', '+', '-':
skipNumberEndCursorIncrement[i] = 1
}
}
}
func (dec *Decoder) skipNumber() (int, error) {
end := dec.cursor + 1
// look for following numbers
for j := dec.cursor + 1; j < dec.length || dec.read(); j++ {
end += skipNumberEndCursorIncrement[dec.data[j]]
switch dec.data[j] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'E', '+', '-', ' ', '\n', '\t', '\r':
continue
case ',', '}', ']':
return end, nil
default:
// invalid json we expect numbers, dot (single one), comma, or spaces
return end, dec.raiseInvalidJSONErr(dec.cursor)
}
}
return end, nil
}
func (dec *Decoder) getExponent() (int64, error) {
start := dec.cursor
end := dec.cursor
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] { // is positive
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
end = dec.cursor + 1
case '-':
dec.cursor++
exp, err := dec.getExponent()
return -exp, err
case '+':
dec.cursor++
return dec.getExponent()
default:
// if nothing return 0
// could raise error
if start == end {
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
return dec.atoi64(start, end-1), nil
}
}
if start == end {
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
return dec.atoi64(start, end-1), nil
}
+516
View File
@@ -0,0 +1,516 @@
package gojay
// DecodeFloat64 reads the next JSON-encoded value from the decoder's input (io.Reader) and stores it in the float64 pointed to by v.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
func (dec *Decoder) DecodeFloat64(v *float64) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeFloat64(v)
}
func (dec *Decoder) decodeFloat64(v *float64) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch c := dec.data[dec.cursor]; c {
case ' ', '\n', '\t', '\r', ',':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
val, err := dec.getFloat()
if err != nil {
return err
}
*v = val
return nil
case '-':
dec.cursor = dec.cursor + 1
val, err := dec.getFloatNegative()
if err != nil {
return err
}
*v = -val
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) decodeFloat64Null(v **float64) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch c := dec.data[dec.cursor]; c {
case ' ', '\n', '\t', '\r', ',':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
val, err := dec.getFloat()
if err != nil {
return err
}
if *v == nil {
*v = new(float64)
}
**v = val
return nil
case '-':
dec.cursor = dec.cursor + 1
val, err := dec.getFloatNegative()
if err != nil {
return err
}
if *v == nil {
*v = new(float64)
}
**v = -val
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) getFloatNegative() (float64, error) {
// look for following numbers
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return dec.getFloat()
default:
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
}
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) getFloat() (float64, error) {
var end = dec.cursor
var start = dec.cursor
// look for following numbers
for j := dec.cursor + 1; j < dec.length || dec.read(); j++ {
switch dec.data[j] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
end = j
continue
case '.':
// we get part before decimal as integer
beforeDecimal := dec.atoi64(start, end)
// then we get part after decimal as integer
start = j + 1
// get number after the decimal point
for i := j + 1; i < dec.length || dec.read(); i++ {
c := dec.data[i]
if isDigit(c) {
end = i
// multiply the before decimal point portion by 10 using bitwise
// make sure it doesn't overflow
if end-start < 18 {
beforeDecimal = (beforeDecimal << 3) + (beforeDecimal << 1)
}
continue
} else if (c == 'e' || c == 'E') && j < i-1 {
// we have an exponent, convert first the value we got before the exponent
var afterDecimal int64
expI := end - start + 2
// if exp is too long, it means number is too long, just truncate the number
if expI >= len(pow10uint64) || expI < 0 {
expI = len(pow10uint64) - 2
afterDecimal = dec.atoi64(start, start+expI-2)
} else {
// then we add both integers
// then we divide the number by the power found
afterDecimal = dec.atoi64(start, end)
}
dec.cursor = i + 1
pow := pow10uint64[expI]
floatVal := float64(beforeDecimal+afterDecimal) / float64(pow)
exp, err := dec.getExponent()
if err != nil {
return 0, err
}
pExp := (exp + (exp >> 31)) ^ (exp >> 31) + 1 // absolute exponent
if pExp >= int64(len(pow10uint64)) || pExp < 0 {
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
// if exponent is negative
if exp < 0 {
return float64(floatVal) * (1 / float64(pow10uint64[pExp])), nil
}
return float64(floatVal) * float64(pow10uint64[pExp]), nil
}
dec.cursor = i
break
}
if end >= dec.length || end < start {
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
var afterDecimal int64
expI := end - start + 2
// if exp is too long, it means number is too long, just truncate the number
if expI >= len(pow10uint64) || expI < 0 {
expI = 19
afterDecimal = dec.atoi64(start, start+expI-2)
} else {
afterDecimal = dec.atoi64(start, end)
}
pow := pow10uint64[expI]
// then we add both integers
// then we divide the number by the power found
return float64(beforeDecimal+afterDecimal) / float64(pow), nil
case 'e', 'E':
dec.cursor = j + 1
// we get part before decimal as integer
beforeDecimal := uint64(dec.atoi64(start, end))
// get exponent
exp, err := dec.getExponent()
if err != nil {
return 0, err
}
pExp := (exp + (exp >> 31)) ^ (exp >> 31) + 1 // abs
if pExp >= int64(len(pow10uint64)) || pExp < 0 {
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
// if exponent is negative
if exp < 0 {
return float64(beforeDecimal) * (1 / float64(pow10uint64[pExp])), nil
}
return float64(beforeDecimal) * float64(pow10uint64[pExp]), nil
case ' ', '\n', '\t', '\r', ',', '}', ']': // does not have decimal
dec.cursor = j
return float64(dec.atoi64(start, end)), nil
}
// invalid json we expect numbers, dot (single one), comma, or spaces
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
return float64(dec.atoi64(start, end)), nil
}
// DecodeFloat32 reads the next JSON-encoded value from the decoder's input (io.Reader) and stores it in the float32 pointed to by v.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
func (dec *Decoder) DecodeFloat32(v *float32) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeFloat32(v)
}
func (dec *Decoder) decodeFloat32(v *float32) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch c := dec.data[dec.cursor]; c {
case ' ', '\n', '\t', '\r', ',':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
val, err := dec.getFloat32()
if err != nil {
return err
}
*v = val
return nil
case '-':
dec.cursor = dec.cursor + 1
val, err := dec.getFloat32Negative()
if err != nil {
return err
}
*v = -val
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) decodeFloat32Null(v **float32) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch c := dec.data[dec.cursor]; c {
case ' ', '\n', '\t', '\r', ',':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
val, err := dec.getFloat32()
if err != nil {
return err
}
if *v == nil {
*v = new(float32)
}
**v = val
return nil
case '-':
dec.cursor = dec.cursor + 1
val, err := dec.getFloat32Negative()
if err != nil {
return err
}
if *v == nil {
*v = new(float32)
}
**v = -val
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) getFloat32Negative() (float32, error) {
// look for following numbers
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return dec.getFloat32()
default:
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
}
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) getFloat32() (float32, error) {
var end = dec.cursor
var start = dec.cursor
// look for following numbers
for j := dec.cursor + 1; j < dec.length || dec.read(); j++ {
switch dec.data[j] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
end = j
continue
case '.':
// we get part before decimal as integer
beforeDecimal := dec.atoi64(start, end)
// then we get part after decimal as integer
start = j + 1
// get number after the decimal point
// multiple the before decimal point portion by 10 using bitwise
for i := j + 1; i < dec.length || dec.read(); i++ {
c := dec.data[i]
if isDigit(c) {
end = i
// multiply the before decimal point portion by 10 using bitwise
// make sure it desn't overflow
if end-start < 9 {
beforeDecimal = (beforeDecimal << 3) + (beforeDecimal << 1)
}
continue
} else if (c == 'e' || c == 'E') && j < i-1 {
// we get the number before decimal
var afterDecimal int64
expI := end - start + 2
// if exp is too long, it means number is too long, just truncate the number
if expI >= 12 || expI < 0 {
expI = 10
afterDecimal = dec.atoi64(start, start+expI-2)
} else {
afterDecimal = dec.atoi64(start, end)
}
dec.cursor = i + 1
pow := pow10uint64[expI]
// then we add both integers
// then we divide the number by the power found
floatVal := float32(beforeDecimal+afterDecimal) / float32(pow)
exp, err := dec.getExponent()
if err != nil {
return 0, err
}
pExp := (exp + (exp >> 31)) ^ (exp >> 31) + 1 // abs
if pExp >= int64(len(pow10uint64)) || pExp < 0 {
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
// if exponent is negative
if exp < 0 {
return float32(floatVal) * (1 / float32(pow10uint64[pExp])), nil
}
return float32(floatVal) * float32(pow10uint64[pExp]), nil
}
dec.cursor = i
break
}
if end >= dec.length || end < start {
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
// then we add both integers
// then we divide the number by the power found
var afterDecimal int64
expI := end - start + 2
// if exp is too long, it means number is too long, just truncate the number
if expI >= 12 || expI < 0 {
expI = 10
afterDecimal = dec.atoi64(start, start+expI-2)
} else {
// then we add both integers
// then we divide the number by the power found
afterDecimal = dec.atoi64(start, end)
}
pow := pow10uint64[expI]
return float32(beforeDecimal+afterDecimal) / float32(pow), nil
case 'e', 'E':
dec.cursor = j + 1
// we get part before decimal as integer
beforeDecimal := dec.atoi64(start, end)
// get exponent
exp, err := dec.getExponent()
if err != nil {
return 0, err
}
pExp := (exp + (exp >> 31)) ^ (exp >> 31) + 1
if pExp >= int64(len(pow10uint64)) || pExp < 0 {
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
// if exponent is negative
if exp < 0 {
return float32(beforeDecimal) * (1 / float32(pow10uint64[pExp])), nil
}
return float32(beforeDecimal) * float32(pow10uint64[pExp]), nil
case ' ', '\n', '\t', '\r', ',', '}', ']': // does not have decimal
dec.cursor = j
return float32(dec.atoi64(start, end)), nil
}
// invalid json we expect numbers, dot (single one), comma, or spaces
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
return float32(dec.atoi64(start, end)), nil
}
// Add Values functions
// AddFloat decodes the JSON value within an object or an array to a *float64.
// If next key value overflows float64, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) AddFloat(v *float64) error {
return dec.Float64(v)
}
// AddFloatNull decodes the JSON value within an object or an array to a *float64.
// If next key value overflows float64, an InvalidUnmarshalError error will be returned.
// If a `null` is encountered, gojay does not change the value of the pointer.
func (dec *Decoder) AddFloatNull(v **float64) error {
return dec.Float64Null(v)
}
// AddFloat64 decodes the JSON value within an object or an array to a *float64.
// If next key value overflows float64, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) AddFloat64(v *float64) error {
return dec.Float64(v)
}
// AddFloat64Null decodes the JSON value within an object or an array to a *float64.
// If next key value overflows float64, an InvalidUnmarshalError error will be returned.
// If a `null` is encountered, gojay does not change the value of the pointer.
func (dec *Decoder) AddFloat64Null(v **float64) error {
return dec.Float64Null(v)
}
// AddFloat32 decodes the JSON value within an object or an array to a *float64.
// If next key value overflows float64, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) AddFloat32(v *float32) error {
return dec.Float32(v)
}
// AddFloat32Null decodes the JSON value within an object or an array to a *float64.
// If next key value overflows float64, an InvalidUnmarshalError error will be returned.
// If a `null` is encountered, gojay does not change the value of the pointer.
func (dec *Decoder) AddFloat32Null(v **float32) error {
return dec.Float32Null(v)
}
// Float decodes the JSON value within an object or an array to a *float64.
// If next key value overflows float64, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Float(v *float64) error {
return dec.Float64(v)
}
// FloatNull decodes the JSON value within an object or an array to a *float64.
// If next key value overflows float64, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) FloatNull(v **float64) error {
return dec.Float64Null(v)
}
// Float64 decodes the JSON value within an object or an array to a *float64.
// If next key value overflows float64, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Float64(v *float64) error {
err := dec.decodeFloat64(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
// Float64Null decodes the JSON value within an object or an array to a *float64.
// If next key value overflows float64, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Float64Null(v **float64) error {
err := dec.decodeFloat64Null(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
// Float32 decodes the JSON value within an object or an array to a *float64.
// If next key value overflows float64, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Float32(v *float32) error {
err := dec.decodeFloat32(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
// Float32Null decodes the JSON value within an object or an array to a *float64.
// If next key value overflows float64, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Float32Null(v **float32) error {
err := dec.decodeFloat32Null(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
File diff suppressed because it is too large Load Diff
+715
View File
@@ -0,0 +1,715 @@
package gojay
import (
"math"
)
// DecodeUint8 reads the next JSON-encoded value from the decoder's input (io.Reader) and stores it in the uint8 pointed to by v.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
func (dec *Decoder) DecodeUint8(v *uint8) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeUint8(v)
}
func (dec *Decoder) decodeUint8(v *uint8) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch c := dec.data[dec.cursor]; c {
case ' ', '\n', '\t', '\r', ',':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
val, err := dec.getUint8()
if err != nil {
return err
}
*v = val
return nil
case '-': // if negative, we just set it to 0 and set error
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) decodeUint8Null(v **uint8) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch c := dec.data[dec.cursor]; c {
case ' ', '\n', '\t', '\r', ',':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
val, err := dec.getUint8()
if err != nil {
return err
}
if *v == nil {
*v = new(uint8)
}
**v = val
return nil
case '-': // if negative, we just set it to 0 and set error
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
if *v == nil {
*v = new(uint8)
}
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) getUint8() (uint8, error) {
var end = dec.cursor
var start = dec.cursor
// look for following numbers
for j := dec.cursor + 1; j < dec.length || dec.read(); j++ {
switch dec.data[j] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
end = j
continue
case ' ', '\n', '\t', '\r':
continue
case '.', ',', '}', ']':
dec.cursor = j
return dec.atoui8(start, end), nil
}
// invalid json we expect numbers, dot (single one), comma, or spaces
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
return dec.atoui8(start, end), nil
}
// DecodeUint16 reads the next JSON-encoded value from the decoder's input (io.Reader) and stores it in the uint16 pointed to by v.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
func (dec *Decoder) DecodeUint16(v *uint16) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeUint16(v)
}
func (dec *Decoder) decodeUint16(v *uint16) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch c := dec.data[dec.cursor]; c {
case ' ', '\n', '\t', '\r', ',':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
val, err := dec.getUint16()
if err != nil {
return err
}
*v = val
return nil
case '-':
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) decodeUint16Null(v **uint16) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch c := dec.data[dec.cursor]; c {
case ' ', '\n', '\t', '\r', ',':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
val, err := dec.getUint16()
if err != nil {
return err
}
if *v == nil {
*v = new(uint16)
}
**v = val
return nil
case '-':
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
if *v == nil {
*v = new(uint16)
}
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) getUint16() (uint16, error) {
var end = dec.cursor
var start = dec.cursor
// look for following numbers
for j := dec.cursor + 1; j < dec.length || dec.read(); j++ {
switch dec.data[j] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
end = j
continue
case ' ', '\n', '\t', '\r':
continue
case '.', ',', '}', ']':
dec.cursor = j
return dec.atoui16(start, end), nil
}
// invalid json we expect numbers, dot (single one), comma, or spaces
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
return dec.atoui16(start, end), nil
}
// DecodeUint32 reads the next JSON-encoded value from the decoder's input (io.Reader) and stores it in the uint32 pointed to by v.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
func (dec *Decoder) DecodeUint32(v *uint32) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeUint32(v)
}
func (dec *Decoder) decodeUint32(v *uint32) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch c := dec.data[dec.cursor]; c {
case ' ', '\n', '\t', '\r', ',':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
val, err := dec.getUint32()
if err != nil {
return err
}
*v = val
return nil
case '-':
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) decodeUint32Null(v **uint32) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch c := dec.data[dec.cursor]; c {
case ' ', '\n', '\t', '\r', ',':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
val, err := dec.getUint32()
if err != nil {
return err
}
if *v == nil {
*v = new(uint32)
}
**v = val
return nil
case '-':
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
if *v == nil {
*v = new(uint32)
}
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) getUint32() (uint32, error) {
var end = dec.cursor
var start = dec.cursor
// look for following numbers
for j := dec.cursor + 1; j < dec.length || dec.read(); j++ {
switch dec.data[j] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
end = j
continue
case ' ', '\n', '\t', '\r':
continue
case '.', ',', '}', ']':
dec.cursor = j
return dec.atoui32(start, end), nil
}
// invalid json we expect numbers, dot (single one), comma, or spaces
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
return dec.atoui32(start, end), nil
}
// DecodeUint64 reads the next JSON-encoded value from the decoder's input (io.Reader) and stores it in the uint64 pointed to by v.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
func (dec *Decoder) DecodeUint64(v *uint64) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeUint64(v)
}
func (dec *Decoder) decodeUint64(v *uint64) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch c := dec.data[dec.cursor]; c {
case ' ', '\n', '\t', '\r', ',':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
val, err := dec.getUint64()
if err != nil {
return err
}
*v = val
return nil
case '-':
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) decodeUint64Null(v **uint64) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch c := dec.data[dec.cursor]; c {
case ' ', '\n', '\t', '\r', ',':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
val, err := dec.getUint64()
if err != nil {
return err
}
if *v == nil {
*v = new(uint64)
}
**v = val
return nil
case '-':
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
if *v == nil {
*v = new(uint64)
}
return nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) getUint64() (uint64, error) {
var end = dec.cursor
var start = dec.cursor
// look for following numbers
for j := dec.cursor + 1; j < dec.length || dec.read(); j++ {
switch dec.data[j] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
end = j
continue
case ' ', '\n', '\t', '\r', '.', ',', '}', ']':
dec.cursor = j
return dec.atoui64(start, end), nil
}
// invalid json we expect numbers, dot (single one), comma, or spaces
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
return dec.atoui64(start, end), nil
}
func (dec *Decoder) atoui64(start, end int) uint64 {
var ll = end + 1 - start
var val = uint64(digits[dec.data[start]])
end = end + 1
if ll < maxUint64Length {
for i := start + 1; i < end; i++ {
uintv := uint64(digits[dec.data[i]])
val = (val << 3) + (val << 1) + uintv
}
} else if ll == maxUint64Length {
for i := start + 1; i < end; i++ {
uintv := uint64(digits[dec.data[i]])
if val > maxUint64toMultiply {
dec.err = dec.makeInvalidUnmarshalErr(val)
return 0
}
val = (val << 3) + (val << 1)
if math.MaxUint64-val < uintv {
dec.err = dec.makeInvalidUnmarshalErr(val)
return 0
}
val += uintv
}
} else {
dec.err = dec.makeInvalidUnmarshalErr(val)
return 0
}
return val
}
func (dec *Decoder) atoui32(start, end int) uint32 {
var ll = end + 1 - start
var val uint32
val = uint32(digits[dec.data[start]])
end = end + 1
if ll < maxUint32Length {
for i := start + 1; i < end; i++ {
uintv := uint32(digits[dec.data[i]])
val = (val << 3) + (val << 1) + uintv
}
} else if ll == maxUint32Length {
for i := start + 1; i < end; i++ {
uintv := uint32(digits[dec.data[i]])
if val > maxUint32toMultiply {
dec.err = dec.makeInvalidUnmarshalErr(val)
return 0
}
val = (val << 3) + (val << 1)
if math.MaxUint32-val < uintv {
dec.err = dec.makeInvalidUnmarshalErr(val)
return 0
}
val += uintv
}
} else if ll > maxUint32Length {
dec.err = dec.makeInvalidUnmarshalErr(val)
val = 0
}
return val
}
func (dec *Decoder) atoui16(start, end int) uint16 {
var ll = end + 1 - start
var val uint16
val = uint16(digits[dec.data[start]])
end = end + 1
if ll < maxUint16Length {
for i := start + 1; i < end; i++ {
uintv := uint16(digits[dec.data[i]])
val = (val << 3) + (val << 1) + uintv
}
} else if ll == maxUint16Length {
for i := start + 1; i < end; i++ {
uintv := uint16(digits[dec.data[i]])
if val > maxUint16toMultiply {
dec.err = dec.makeInvalidUnmarshalErr(val)
return 0
}
val = (val << 3) + (val << 1)
if math.MaxUint16-val < uintv {
dec.err = dec.makeInvalidUnmarshalErr(val)
return 0
}
val += uintv
}
} else if ll > maxUint16Length {
dec.err = dec.makeInvalidUnmarshalErr(val)
val = 0
}
return val
}
func (dec *Decoder) atoui8(start, end int) uint8 {
var ll = end + 1 - start
var val uint8
val = uint8(digits[dec.data[start]])
end = end + 1
if ll < maxUint8Length {
for i := start + 1; i < end; i++ {
uintv := uint8(digits[dec.data[i]])
val = (val << 3) + (val << 1) + uintv
}
} else if ll == maxUint8Length {
for i := start + 1; i < end; i++ {
uintv := uint8(digits[dec.data[i]])
if val > maxUint8toMultiply {
dec.err = dec.makeInvalidUnmarshalErr(val)
return 0
}
val = (val << 3) + (val << 1)
if math.MaxUint8-val < uintv {
dec.err = dec.makeInvalidUnmarshalErr(val)
return 0
}
val += uintv
}
} else if ll > maxUint8Length {
dec.err = dec.makeInvalidUnmarshalErr(val)
val = 0
}
return val
}
// Add Values functions
// AddUint8 decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint8, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) AddUint8(v *uint8) error {
return dec.Uint8(v)
}
// AddUint8Null decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint8, an InvalidUnmarshalError error will be returned.
// If a `null` is encountered, gojay does not change the value of the pointer.
func (dec *Decoder) AddUint8Null(v **uint8) error {
return dec.Uint8Null(v)
}
// AddUint16 decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint16, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) AddUint16(v *uint16) error {
return dec.Uint16(v)
}
// AddUint16Null decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint16, an InvalidUnmarshalError error will be returned.
// If a `null` is encountered, gojay does not change the value of the pointer.
func (dec *Decoder) AddUint16Null(v **uint16) error {
return dec.Uint16Null(v)
}
// AddUint32 decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint32, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) AddUint32(v *uint32) error {
return dec.Uint32(v)
}
// AddUint32Null decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint32, an InvalidUnmarshalError error will be returned.
// If a `null` is encountered, gojay does not change the value of the pointer.
func (dec *Decoder) AddUint32Null(v **uint32) error {
return dec.Uint32Null(v)
}
// AddUint64 decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint64, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) AddUint64(v *uint64) error {
return dec.Uint64(v)
}
// AddUint64Null decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint64, an InvalidUnmarshalError error will be returned.
// If a `null` is encountered, gojay does not change the value of the pointer.
func (dec *Decoder) AddUint64Null(v **uint64) error {
return dec.Uint64Null(v)
}
// Uint8 decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint8, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Uint8(v *uint8) error {
err := dec.decodeUint8(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
// Uint8Null decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint8, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Uint8Null(v **uint8) error {
err := dec.decodeUint8Null(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
// Uint16 decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint16, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Uint16(v *uint16) error {
err := dec.decodeUint16(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
// Uint16Null decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint16, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Uint16Null(v **uint16) error {
err := dec.decodeUint16Null(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
// Uint32 decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint32, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Uint32(v *uint32) error {
err := dec.decodeUint32(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
// Uint32Null decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint32, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Uint32Null(v **uint32) error {
err := dec.decodeUint32Null(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
// Uint64 decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint64, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Uint64(v *uint64) error {
err := dec.decodeUint64(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
// Uint64Null decodes the JSON value within an object or an array to an *int.
// If next key value overflows uint64, an InvalidUnmarshalError error will be returned.
func (dec *Decoder) Uint64Null(v **uint64) error {
err := dec.decodeUint64Null(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
+407
View File
@@ -0,0 +1,407 @@
package gojay
import (
"reflect"
"unsafe"
)
// DecodeObject reads the next JSON-encoded value from the decoder's input (io.Reader) and stores it in the value pointed to by v.
//
// v must implement UnmarshalerJSONObject.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
func (dec *Decoder) DecodeObject(j UnmarshalerJSONObject) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
_, err := dec.decodeObject(j)
return err
}
func (dec *Decoder) decodeObject(j UnmarshalerJSONObject) (int, error) {
keys := j.NKeys()
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
case '{':
dec.cursor = dec.cursor + 1
// if keys is zero we will parse all keys
// we run two loops for micro optimization
if keys == 0 {
for dec.cursor < dec.length || dec.read() {
k, done, err := dec.nextKey()
if err != nil {
return 0, err
} else if done {
return dec.cursor, nil
}
err = j.UnmarshalJSONObject(dec, k)
if err != nil {
dec.err = err
return 0, err
} else if dec.called&1 == 0 {
err := dec.skipData()
if err != nil {
return 0, err
}
} else {
dec.keysDone++
}
dec.called &= 0
}
} else {
for (dec.cursor < dec.length || dec.read()) && dec.keysDone < keys {
k, done, err := dec.nextKey()
if err != nil {
return 0, err
} else if done {
return dec.cursor, nil
}
err = j.UnmarshalJSONObject(dec, k)
if err != nil {
dec.err = err
return 0, err
} else if dec.called&1 == 0 {
err := dec.skipData()
if err != nil {
return 0, err
}
} else {
dec.keysDone++
}
dec.called &= 0
}
}
// will get to that point when keysDone is not lower than keys anymore
// in that case, we make sure cursor goes to the end of object, but we skip
// unmarshalling
if dec.child&1 != 0 {
end, err := dec.skipObject()
dec.cursor = end
return dec.cursor, err
}
return dec.cursor, nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return 0, err
}
return dec.cursor, nil
default:
// can't unmarshal to struct
dec.err = dec.makeInvalidUnmarshalErr(j)
err := dec.skipData()
if err != nil {
return 0, err
}
return dec.cursor, nil
}
}
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) decodeObjectNull(v interface{}) (int, error) {
// make sure the value is a pointer
vv := reflect.ValueOf(v)
vvt := vv.Type()
if vvt.Kind() != reflect.Ptr || vvt.Elem().Kind() != reflect.Ptr {
dec.err = ErrUnmarshalPtrExpected
return 0, dec.err
}
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
case '{':
elt := vv.Elem()
n := reflect.New(elt.Type().Elem())
elt.Set(n)
var j UnmarshalerJSONObject
var ok bool
if j, ok = n.Interface().(UnmarshalerJSONObject); !ok {
dec.err = dec.makeInvalidUnmarshalErr((UnmarshalerJSONObject)(nil))
return 0, dec.err
}
keys := j.NKeys()
dec.cursor = dec.cursor + 1
// if keys is zero we will parse all keys
// we run two loops for micro optimization
if keys == 0 {
for dec.cursor < dec.length || dec.read() {
k, done, err := dec.nextKey()
if err != nil {
return 0, err
} else if done {
return dec.cursor, nil
}
err = j.UnmarshalJSONObject(dec, k)
if err != nil {
dec.err = err
return 0, err
} else if dec.called&1 == 0 {
err := dec.skipData()
if err != nil {
return 0, err
}
} else {
dec.keysDone++
}
dec.called &= 0
}
} else {
for (dec.cursor < dec.length || dec.read()) && dec.keysDone < keys {
k, done, err := dec.nextKey()
if err != nil {
return 0, err
} else if done {
return dec.cursor, nil
}
err = j.UnmarshalJSONObject(dec, k)
if err != nil {
dec.err = err
return 0, err
} else if dec.called&1 == 0 {
err := dec.skipData()
if err != nil {
return 0, err
}
} else {
dec.keysDone++
}
dec.called &= 0
}
}
// will get to that point when keysDone is not lower than keys anymore
// in that case, we make sure cursor goes to the end of object, but we skip
// unmarshalling
if dec.child&1 != 0 {
end, err := dec.skipObject()
dec.cursor = end
return dec.cursor, err
}
return dec.cursor, nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return 0, err
}
return dec.cursor, nil
default:
// can't unmarshal to struct
dec.err = dec.makeInvalidUnmarshalErr((UnmarshalerJSONObject)(nil))
err := dec.skipData()
if err != nil {
return 0, err
}
return dec.cursor, nil
}
}
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) skipObject() (int, error) {
var objectsOpen = 1
var objectsClosed = 0
for j := dec.cursor; j < dec.length || dec.read(); j++ {
switch dec.data[j] {
case '}':
objectsClosed++
// everything is closed return
if objectsOpen == objectsClosed {
// add char to object data
return j + 1, nil
}
case '{':
objectsOpen++
case '"':
j++
var isInEscapeSeq bool
var isFirstQuote = true
for ; j < dec.length || dec.read(); j++ {
if dec.data[j] != '"' {
continue
}
if dec.data[j-1] != '\\' || (!isInEscapeSeq && !isFirstQuote) {
break
} else {
isInEscapeSeq = false
}
if isFirstQuote {
isFirstQuote = false
}
// loop backward and count how many anti slash found
// to see if string is effectively escaped
ct := 0
for i := j - 1; i > 0; i-- {
if dec.data[i] != '\\' {
break
}
ct++
}
// is pair number of slashes, quote is not escaped
if ct&1 == 0 {
break
}
isInEscapeSeq = true
}
default:
continue
}
}
return 0, dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) nextKey() (string, bool, error) {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
continue
case '"':
dec.cursor = dec.cursor + 1
start, end, err := dec.getString()
if err != nil {
return "", false, err
}
var found byte
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
if dec.data[dec.cursor] == ':' {
found |= 1
break
}
}
if found&1 != 0 {
dec.cursor++
d := dec.data[start : end-1]
return *(*string)(unsafe.Pointer(&d)), false, nil
}
return "", false, dec.raiseInvalidJSONErr(dec.cursor)
case '}':
dec.cursor = dec.cursor + 1
return "", true, nil
default:
// can't unmarshall to struct
return "", false, dec.raiseInvalidJSONErr(dec.cursor)
}
}
return "", false, dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) skipData() error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
continue
// is null
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
case 't':
dec.cursor++
err := dec.assertTrue()
if err != nil {
return err
}
return nil
// is false
case 'f':
dec.cursor++
err := dec.assertFalse()
if err != nil {
return err
}
return nil
// is an object
case '{':
dec.cursor = dec.cursor + 1
end, err := dec.skipObject()
dec.cursor = end
return err
// is string
case '"':
dec.cursor = dec.cursor + 1
err := dec.skipString()
return err
// is array
case '[':
dec.cursor = dec.cursor + 1
end, err := dec.skipArray()
dec.cursor = end
return err
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
end, err := dec.skipNumber()
dec.cursor = end
return err
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
// DecodeObjectFunc is a func type implementing UnmarshalerJSONObject.
// Use it to cast a `func(*Decoder, k string) error` to Unmarshal an object on the fly.
type DecodeObjectFunc func(*Decoder, string) error
// UnmarshalJSONObject implements UnmarshalerJSONObject.
func (f DecodeObjectFunc) UnmarshalJSONObject(dec *Decoder, k string) error {
return f(dec, k)
}
// NKeys implements UnmarshalerJSONObject.
func (f DecodeObjectFunc) NKeys() int {
return 0
}
// Add Values functions
// AddObject decodes the JSON value within an object or an array to a UnmarshalerJSONObject.
func (dec *Decoder) AddObject(v UnmarshalerJSONObject) error {
return dec.Object(v)
}
// AddObjectNull decodes the JSON value within an object or an array to a UnmarshalerJSONObject.
func (dec *Decoder) AddObjectNull(v interface{}) error {
return dec.ObjectNull(v)
}
// Object decodes the JSON value within an object or an array to a UnmarshalerJSONObject.
func (dec *Decoder) Object(value UnmarshalerJSONObject) error {
initialKeysDone := dec.keysDone
initialChild := dec.child
dec.keysDone = 0
dec.called = 0
dec.child |= 1
newCursor, err := dec.decodeObject(value)
if err != nil {
return err
}
dec.cursor = newCursor
dec.keysDone = initialKeysDone
dec.child = initialChild
dec.called |= 1
return nil
}
// ObjectNull decodes the JSON value within an object or an array to a UnmarshalerJSONObject.
// v should be a pointer to an UnmarshalerJSONObject,
// if `null` value is encountered in JSON, it will leave the value v untouched,
// else it will create a new instance of the UnmarshalerJSONObject behind v.
func (dec *Decoder) ObjectNull(v interface{}) error {
initialKeysDone := dec.keysDone
initialChild := dec.child
dec.keysDone = 0
dec.called = 0
dec.child |= 1
newCursor, err := dec.decodeObjectNull(v)
if err != nil {
return err
}
dec.cursor = newCursor
dec.keysDone = initialKeysDone
dec.child = initialChild
dec.called |= 1
return nil
}
+64
View File
@@ -0,0 +1,64 @@
package gojay
import (
"io"
"sync"
)
var decPool = sync.Pool{
New: newDecoderPool,
}
func init() {
for i := 0; i < 32; i++ {
decPool.Put(NewDecoder(nil))
}
}
// NewDecoder returns a new decoder.
// It takes an io.Reader implementation as data input.
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{
called: 0,
cursor: 0,
keysDone: 0,
err: nil,
r: r,
data: make([]byte, 512),
length: 0,
isPooled: 0,
}
}
func newDecoderPool() interface{} {
return NewDecoder(nil)
}
// BorrowDecoder borrows a Decoder from the pool.
// It takes an io.Reader implementation as data input.
//
// In order to benefit from the pool, a borrowed decoder must be released after usage.
func BorrowDecoder(r io.Reader) *Decoder {
return borrowDecoder(r, 512)
}
func borrowDecoder(r io.Reader, bufSize int) *Decoder {
dec := decPool.Get().(*Decoder)
dec.called = 0
dec.keysDone = 0
dec.cursor = 0
dec.err = nil
dec.r = r
dec.length = 0
dec.isPooled = 0
if bufSize > 0 {
dec.data = make([]byte, bufSize)
}
return dec
}
// Release sends back a Decoder to the pool.
// If a decoder is used after calling Release
// a panic will be raised with an InvalidUsagePooledDecoderError error.
func (dec *Decoder) Release() {
dec.isPooled = 1
decPool.Put(dec)
}
+89
View File
@@ -0,0 +1,89 @@
package gojay
// AddSliceString unmarshals the next JSON array of strings to the given *[]string s
func (dec *Decoder) AddSliceString(s *[]string) error {
return dec.SliceString(s)
}
// SliceString unmarshals the next JSON array of strings to the given *[]string s
func (dec *Decoder) SliceString(s *[]string) error {
err := dec.Array(DecodeArrayFunc(func(dec *Decoder) error {
var str string
if err := dec.String(&str); err != nil {
return err
}
*s = append(*s, str)
return nil
}))
if err != nil {
return err
}
return nil
}
// AddSliceInt unmarshals the next JSON array of integers to the given *[]int s
func (dec *Decoder) AddSliceInt(s *[]int) error {
return dec.SliceInt(s)
}
// SliceInt unmarshals the next JSON array of integers to the given *[]int s
func (dec *Decoder) SliceInt(s *[]int) error {
err := dec.Array(DecodeArrayFunc(func(dec *Decoder) error {
var i int
if err := dec.Int(&i); err != nil {
return err
}
*s = append(*s, i)
return nil
}))
if err != nil {
return err
}
return nil
}
// AddFloat64 unmarshals the next JSON array of floats to the given *[]float64 s
func (dec *Decoder) AddSliceFloat64(s *[]float64) error {
return dec.SliceFloat64(s)
}
// SliceFloat64 unmarshals the next JSON array of floats to the given *[]float64 s
func (dec *Decoder) SliceFloat64(s *[]float64) error {
err := dec.Array(DecodeArrayFunc(func(dec *Decoder) error {
var i float64
if err := dec.Float64(&i); err != nil {
return err
}
*s = append(*s, i)
return nil
}))
if err != nil {
return err
}
return nil
}
// AddBool unmarshals the next JSON array of boolegers to the given *[]bool s
func (dec *Decoder) AddSliceBool(s *[]bool) error {
return dec.SliceBool(s)
}
// SliceBool unmarshals the next JSON array of boolegers to the given *[]bool s
func (dec *Decoder) SliceBool(s *[]bool) error {
err := dec.Array(DecodeArrayFunc(func(dec *Decoder) error {
var b bool
if err := dec.Bool(&b); err != nil {
return err
}
*s = append(*s, b)
return nil
}))
if err != nil {
return err
}
return nil
}
+157
View File
@@ -0,0 +1,157 @@
package gojay
import "database/sql"
// DecodeSQLNullString decodes a sql.NullString
func (dec *Decoder) DecodeSQLNullString(v *sql.NullString) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeSQLNullString(v)
}
func (dec *Decoder) decodeSQLNullString(v *sql.NullString) error {
var str string
if err := dec.decodeString(&str); err != nil {
return err
}
v.String = str
v.Valid = true
return nil
}
// DecodeSQLNullInt64 decodes a sql.NullInt64
func (dec *Decoder) DecodeSQLNullInt64(v *sql.NullInt64) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeSQLNullInt64(v)
}
func (dec *Decoder) decodeSQLNullInt64(v *sql.NullInt64) error {
var i int64
if err := dec.decodeInt64(&i); err != nil {
return err
}
v.Int64 = i
v.Valid = true
return nil
}
// DecodeSQLNullFloat64 decodes a sql.NullString with the given format
func (dec *Decoder) DecodeSQLNullFloat64(v *sql.NullFloat64) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeSQLNullFloat64(v)
}
func (dec *Decoder) decodeSQLNullFloat64(v *sql.NullFloat64) error {
var i float64
if err := dec.decodeFloat64(&i); err != nil {
return err
}
v.Float64 = i
v.Valid = true
return nil
}
// DecodeSQLNullBool decodes a sql.NullString with the given format
func (dec *Decoder) DecodeSQLNullBool(v *sql.NullBool) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeSQLNullBool(v)
}
func (dec *Decoder) decodeSQLNullBool(v *sql.NullBool) error {
var b bool
if err := dec.decodeBool(&b); err != nil {
return err
}
v.Bool = b
v.Valid = true
return nil
}
// Add Values functions
// AddSQLNullString decodes the JSON value within an object or an array to qn *sql.NullString
func (dec *Decoder) AddSQLNullString(v *sql.NullString) error {
return dec.SQLNullString(v)
}
// SQLNullString decodes the JSON value within an object or an array to an *sql.NullString
func (dec *Decoder) SQLNullString(v *sql.NullString) error {
var b *string
if err := dec.StringNull(&b); err != nil {
return err
}
if b == nil {
v.Valid = false
} else {
v.String = *b
v.Valid = true
}
return nil
}
// AddSQLNullInt64 decodes the JSON value within an object or an array to qn *sql.NullInt64
func (dec *Decoder) AddSQLNullInt64(v *sql.NullInt64) error {
return dec.SQLNullInt64(v)
}
// SQLNullInt64 decodes the JSON value within an object or an array to an *sql.NullInt64
func (dec *Decoder) SQLNullInt64(v *sql.NullInt64) error {
var b *int64
if err := dec.Int64Null(&b); err != nil {
return err
}
if b == nil {
v.Valid = false
} else {
v.Int64 = *b
v.Valid = true
}
return nil
}
// AddSQLNullFloat64 decodes the JSON value within an object or an array to qn *sql.NullFloat64
func (dec *Decoder) AddSQLNullFloat64(v *sql.NullFloat64) error {
return dec.SQLNullFloat64(v)
}
// SQLNullFloat64 decodes the JSON value within an object or an array to an *sql.NullFloat64
func (dec *Decoder) SQLNullFloat64(v *sql.NullFloat64) error {
var b *float64
if err := dec.Float64Null(&b); err != nil {
return err
}
if b == nil {
v.Valid = false
} else {
v.Float64 = *b
v.Valid = true
}
return nil
}
// AddSQLNullBool decodes the JSON value within an object or an array to an *sql.NullBool
func (dec *Decoder) AddSQLNullBool(v *sql.NullBool) error {
return dec.SQLNullBool(v)
}
// SQLNullBool decodes the JSON value within an object or an array to an *sql.NullBool
func (dec *Decoder) SQLNullBool(v *sql.NullBool) error {
var b *bool
if err := dec.BoolNull(&b); err != nil {
return err
}
if b == nil {
v.Valid = false
} else {
v.Bool = *b
v.Valid = true
}
return nil
}
+115
View File
@@ -0,0 +1,115 @@
package gojay
import (
"sync"
"time"
)
// UnmarshalerStream is the interface to implement for a slice, an array or a slice
// to decode a line delimited JSON to.
type UnmarshalerStream interface {
UnmarshalStream(*StreamDecoder) error
}
// Stream is a struct holding the Stream api
var Stream = stream{}
type stream struct{}
// A StreamDecoder reads and decodes JSON values from an input stream.
//
// It implements conext.Context and provide a channel to notify interruption.
type StreamDecoder struct {
mux sync.RWMutex
*Decoder
done chan struct{}
deadline *time.Time
}
// DecodeStream reads the next line delimited JSON-encoded value from the decoder's input (io.Reader) and stores it in the value pointed to by c.
//
// c must implement UnmarshalerStream. Ideally c is a channel. See example for implementation.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
func (dec *StreamDecoder) DecodeStream(c UnmarshalerStream) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
if dec.r == nil {
dec.err = NoReaderError("No reader given to decode stream")
close(dec.done)
return dec.err
}
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
continue
default:
// char is not space start reading
for dec.nextChar() != 0 {
// calling unmarshal stream
err := c.UnmarshalStream(dec)
if err != nil {
dec.err = err
close(dec.done)
return err
}
// garbage collects buffer
// we don't want the buffer to grow extensively
dec.data = dec.data[dec.cursor:]
dec.length = dec.length - dec.cursor
dec.cursor = 0
}
// close the done channel to signal the end of the job
close(dec.done)
return nil
}
}
close(dec.done)
dec.mux.Lock()
err := dec.raiseInvalidJSONErr(dec.cursor)
dec.mux.Unlock()
return err
}
// context.Context implementation
// Done returns a channel that's closed when work is done.
// It implements context.Context
func (dec *StreamDecoder) Done() <-chan struct{} {
return dec.done
}
// Deadline returns the time when work done on behalf of this context
// should be canceled. Deadline returns ok==false when no deadline is
// set. Successive calls to Deadline return the same results.
func (dec *StreamDecoder) Deadline() (time.Time, bool) {
if dec.deadline != nil {
return *dec.deadline, true
}
return time.Time{}, false
}
// SetDeadline sets the deadline
func (dec *StreamDecoder) SetDeadline(t time.Time) {
dec.deadline = &t
}
// Err returns nil if Done is not yet closed.
// If Done is closed, Err returns a non-nil error explaining why.
// It implements context.Context
func (dec *StreamDecoder) Err() error {
select {
case <-dec.done:
dec.mux.RLock()
defer dec.mux.RUnlock()
return dec.err
default:
return nil
}
}
// Value implements context.Context
func (dec *StreamDecoder) Value(key interface{}) interface{} {
return nil
}
+59
View File
@@ -0,0 +1,59 @@
package gojay
import (
"io"
"sync"
)
var streamDecPool = sync.Pool{
New: newStreamDecoderPool,
}
// NewDecoder returns a new StreamDecoder.
// It takes an io.Reader implementation as data input.
// It initiates the done channel returned by Done().
func (s stream) NewDecoder(r io.Reader) *StreamDecoder {
dec := NewDecoder(r)
streamDec := &StreamDecoder{
Decoder: dec,
done: make(chan struct{}, 1),
mux: sync.RWMutex{},
}
return streamDec
}
func newStreamDecoderPool() interface{} {
return Stream.NewDecoder(nil)
}
// BorrowDecoder borrows a StreamDecoder from the pool.
// It takes an io.Reader implementation as data input.
// It initiates the done channel returned by Done().
//
// If no StreamEncoder is available in the pool, it returns a fresh one
func (s stream) BorrowDecoder(r io.Reader) *StreamDecoder {
return s.borrowDecoder(r, 512)
}
func (s stream) borrowDecoder(r io.Reader, bufSize int) *StreamDecoder {
streamDec := streamDecPool.Get().(*StreamDecoder)
streamDec.called = 0
streamDec.keysDone = 0
streamDec.cursor = 0
streamDec.err = nil
streamDec.r = r
streamDec.length = 0
streamDec.isPooled = 0
streamDec.done = make(chan struct{}, 1)
if bufSize > 0 {
streamDec.data = make([]byte, bufSize)
}
return streamDec
}
// Release sends back a Decoder to the pool.
// If a decoder is used after calling Release
// a panic will be raised with an InvalidUsagePooledDecoderError error.
func (dec *StreamDecoder) Release() {
dec.isPooled = 1
streamDecPool.Put(dec)
}
+260
View File
@@ -0,0 +1,260 @@
package gojay
import (
"unsafe"
)
// DecodeString reads the next JSON-encoded value from the decoder's input (io.Reader) and stores it in the string pointed to by v.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
func (dec *Decoder) DecodeString(v *string) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeString(v)
}
func (dec *Decoder) decodeString(v *string) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
// is string
continue
case '"':
dec.cursor++
start, end, err := dec.getString()
if err != nil {
return err
}
// we do minus one to remove the last quote
d := dec.data[start : end-1]
*v = *(*string)(unsafe.Pointer(&d))
dec.cursor = end
return nil
// is nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return nil
}
func (dec *Decoder) decodeStringNull(v **string) error {
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
// is string
continue
case '"':
dec.cursor++
start, end, err := dec.getString()
if err != nil {
return err
}
if *v == nil {
*v = new(string)
}
// we do minus one to remove the last quote
d := dec.data[start : end-1]
**v = *(*string)(unsafe.Pointer(&d))
dec.cursor = end
return nil
// is nil
case 'n':
dec.cursor++
err := dec.assertNull()
if err != nil {
return err
}
return nil
default:
dec.err = dec.makeInvalidUnmarshalErr(v)
err := dec.skipData()
if err != nil {
return err
}
return nil
}
}
return nil
}
func (dec *Decoder) parseEscapedString() error {
if dec.cursor >= dec.length && !dec.read() {
return dec.raiseInvalidJSONErr(dec.cursor)
}
switch dec.data[dec.cursor] {
case '"':
dec.data[dec.cursor] = '"'
case '\\':
dec.data[dec.cursor] = '\\'
case '/':
dec.data[dec.cursor] = '/'
case 'b':
dec.data[dec.cursor] = '\b'
case 'f':
dec.data[dec.cursor] = '\f'
case 'n':
dec.data[dec.cursor] = '\n'
case 'r':
dec.data[dec.cursor] = '\r'
case 't':
dec.data[dec.cursor] = '\t'
case 'u':
start := dec.cursor
dec.cursor++
str, err := dec.parseUnicode()
if err != nil {
return err
}
diff := dec.cursor - start
dec.data = append(append(dec.data[:start-1], str...), dec.data[dec.cursor:]...)
dec.length = len(dec.data)
dec.cursor += len(str) - diff - 1
return nil
default:
return dec.raiseInvalidJSONErr(dec.cursor)
}
dec.data = append(dec.data[:dec.cursor-1], dec.data[dec.cursor:]...)
dec.length--
// Since we've lost a character, our dec.cursor offset is now
// 1 past the escaped character which is precisely where we
// want it.
return nil
}
func (dec *Decoder) getString() (int, int, error) {
// extract key
var keyStart = dec.cursor
// var str *Builder
for dec.cursor < dec.length || dec.read() {
switch dec.data[dec.cursor] {
// string found
case '"':
dec.cursor = dec.cursor + 1
return keyStart, dec.cursor, nil
// slash found
case '\\':
dec.cursor = dec.cursor + 1
err := dec.parseEscapedString()
if err != nil {
return 0, 0, err
}
default:
dec.cursor = dec.cursor + 1
continue
}
}
return 0, 0, dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) skipEscapedString() error {
start := dec.cursor
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
if dec.data[dec.cursor] != '\\' {
d := dec.data[dec.cursor]
dec.cursor = dec.cursor + 1
nSlash := dec.cursor - start
switch d {
case '"':
// nSlash must be odd
if nSlash&1 != 1 {
return dec.raiseInvalidJSONErr(dec.cursor)
}
return nil
case 'u': // is unicode, we skip the following characters and place the cursor one one byte backward to avoid it breaking when returning to skipString
if err := dec.skipString(); err != nil {
return err
}
dec.cursor--
return nil
case 'n', 'r', 't', '/', 'f', 'b':
return nil
default:
// nSlash must be even
if nSlash&1 == 1 {
return dec.raiseInvalidJSONErr(dec.cursor)
}
return nil
}
}
}
return dec.raiseInvalidJSONErr(dec.cursor)
}
func (dec *Decoder) skipString() error {
for dec.cursor < dec.length || dec.read() {
switch dec.data[dec.cursor] {
// found the closing quote
// let's return
case '"':
dec.cursor = dec.cursor + 1
return nil
// solidus found start parsing an escaped string
case '\\':
dec.cursor = dec.cursor + 1
err := dec.skipEscapedString()
if err != nil {
return err
}
default:
dec.cursor = dec.cursor + 1
continue
}
}
return dec.raiseInvalidJSONErr(len(dec.data) - 1)
}
// Add Values functions
// AddString decodes the JSON value within an object or an array to a *string.
// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned.
func (dec *Decoder) AddString(v *string) error {
return dec.String(v)
}
// AddStringNull decodes the JSON value within an object or an array to a *string.
// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned.
// If a `null` is encountered, gojay does not change the value of the pointer.
func (dec *Decoder) AddStringNull(v **string) error {
return dec.StringNull(v)
}
// String decodes the JSON value within an object or an array to a *string.
// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned.
func (dec *Decoder) String(v *string) error {
err := dec.decodeString(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
// StringNull decodes the JSON value within an object or an array to a **string.
// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned.
// If a `null` is encountered, gojay does not change the value of the pointer.
func (dec *Decoder) StringNull(v **string) error {
err := dec.decodeStringNull(v)
if err != nil {
return err
}
dec.called |= 1
return nil
}
+98
View File
@@ -0,0 +1,98 @@
package gojay
import (
"unicode/utf16"
"unicode/utf8"
)
func (dec *Decoder) getUnicode() (rune, error) {
i := 0
r := rune(0)
for ; (dec.cursor < dec.length || dec.read()) && i < 4; dec.cursor++ {
c := dec.data[dec.cursor]
if c >= '0' && c <= '9' {
r = r*16 + rune(c-'0')
} else if c >= 'a' && c <= 'f' {
r = r*16 + rune(c-'a'+10)
} else if c >= 'A' && c <= 'F' {
r = r*16 + rune(c-'A'+10)
} else {
return 0, InvalidJSONError("Invalid unicode code point")
}
i++
}
return r, nil
}
func (dec *Decoder) appendEscapeChar(str []byte, c byte) ([]byte, error) {
switch c {
case 't':
str = append(str, '\t')
case 'n':
str = append(str, '\n')
case 'r':
str = append(str, '\r')
case 'b':
str = append(str, '\b')
case 'f':
str = append(str, '\f')
case '\\':
str = append(str, '\\')
default:
return nil, InvalidJSONError("Invalid JSON")
}
return str, nil
}
func (dec *Decoder) parseUnicode() ([]byte, error) {
// get unicode after u
r, err := dec.getUnicode()
if err != nil {
return nil, err
}
// no error start making new string
str := make([]byte, 16, 16)
i := 0
// check if code can be a surrogate utf16
if utf16.IsSurrogate(r) {
if dec.cursor >= dec.length && !dec.read() {
return nil, dec.raiseInvalidJSONErr(dec.cursor)
}
c := dec.data[dec.cursor]
if c != '\\' {
i += utf8.EncodeRune(str, r)
return str[:i], nil
}
dec.cursor++
if dec.cursor >= dec.length && !dec.read() {
return nil, dec.raiseInvalidJSONErr(dec.cursor)
}
c = dec.data[dec.cursor]
if c != 'u' {
i += utf8.EncodeRune(str, r)
str, err = dec.appendEscapeChar(str[:i], c)
if err != nil {
dec.err = err
return nil, err
}
i++
dec.cursor++
return str[:i], nil
}
dec.cursor++
r2, err := dec.getUnicode()
if err != nil {
return nil, err
}
combined := utf16.DecodeRune(r, r2)
if combined == '\uFFFD' {
i += utf8.EncodeRune(str, r)
i += utf8.EncodeRune(str, r2)
} else {
i += utf8.EncodeRune(str, combined)
}
return str[:i], nil
}
i += utf8.EncodeRune(str, r)
return str[:i], nil
}
+53
View File
@@ -0,0 +1,53 @@
package gojay
import (
"time"
)
// DecodeTime decodes time with the given format
func (dec *Decoder) DecodeTime(v *time.Time, format string) error {
if dec.isPooled == 1 {
panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
}
return dec.decodeTime(v, format)
}
func (dec *Decoder) decodeTime(v *time.Time, format string) error {
if format == time.RFC3339 {
var ej = make(EmbeddedJSON, 0, 20)
if err := dec.decodeEmbeddedJSON(&ej); err != nil {
return err
}
if err := v.UnmarshalJSON(ej); err != nil {
return err
}
return nil
}
var str string
if err := dec.decodeString(&str); err != nil {
return err
}
tt, err := time.Parse(format, str)
if err != nil {
return err
}
*v = tt
return nil
}
// Add Values functions
// AddTime decodes the JSON value within an object or an array to a *time.Time with the given format
func (dec *Decoder) AddTime(v *time.Time, format string) error {
return dec.Time(v, format)
}
// Time decodes the JSON value within an object or an array to a *time.Time with the given format
func (dec *Decoder) Time(v *time.Time, format string) error {
err := dec.decodeTime(v, format)
if err != nil {
return err
}
dec.called |= 1
return nil
}
+120
View File
@@ -0,0 +1,120 @@
package gojay
import (
"fmt"
)
// Unsafe is the structure holding the unsafe version of the API.
// The difference between unsafe api and regular api is that the regular API
// copies the buffer passed to Unmarshal functions to a new internal buffer.
// Making it safer because internally GoJay uses unsafe.Pointer to transform slice of bytes into a string.
var Unsafe = decUnsafe{}
type decUnsafe struct{}
func (u decUnsafe) UnmarshalJSONArray(data []byte, v UnmarshalerJSONArray) error {
dec := borrowDecoder(nil, 0)
defer dec.Release()
dec.data = data
dec.length = len(data)
_, err := dec.decodeArray(v)
return err
}
func (u decUnsafe) UnmarshalJSONObject(data []byte, v UnmarshalerJSONObject) error {
dec := borrowDecoder(nil, 0)
defer dec.Release()
dec.data = data
dec.length = len(data)
_, err := dec.decodeObject(v)
return err
}
func (u decUnsafe) Unmarshal(data []byte, v interface{}) error {
var err error
var dec *Decoder
switch vt := v.(type) {
case *string:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeString(vt)
case *int:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt(vt)
case *int8:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt8(vt)
case *int16:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt16(vt)
case *int32:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt32(vt)
case *int64:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt64(vt)
case *uint8:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint8(vt)
case *uint16:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint16(vt)
case *uint32:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint32(vt)
case *uint64:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint64(vt)
case *float64:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeFloat64(vt)
case *float32:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeFloat32(vt)
case *bool:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeBool(vt)
case UnmarshalerJSONObject:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
_, err = dec.decodeObject(vt)
case UnmarshalerJSONArray:
dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
_, err = dec.decodeArray(vt)
default:
return InvalidUnmarshalError(fmt.Sprintf(invalidUnmarshalErrorMsg, vt))
}
defer dec.Release()
if err != nil {
return err
}
return dec.err
}
+202
View File
@@ -0,0 +1,202 @@
package gojay
import (
"encoding/json"
"fmt"
"io"
)
var nullBytes = []byte("null")
// MarshalJSONArray returns the JSON encoding of v, an implementation of MarshalerJSONArray.
//
//
// Example:
// type TestSlice []*TestStruct
//
// func (t TestSlice) MarshalJSONArray(enc *Encoder) {
// for _, e := range t {
// enc.AddObject(e)
// }
// }
//
// func main() {
// test := &TestSlice{
// &TestStruct{123456},
// &TestStruct{7890},
// }
// b, _ := Marshal(test)
// fmt.Println(b) // [{"id":123456},{"id":7890}]
// }
func MarshalJSONArray(v MarshalerJSONArray) ([]byte, error) {
enc := BorrowEncoder(nil)
enc.grow(512)
enc.writeByte('[')
v.(MarshalerJSONArray).MarshalJSONArray(enc)
enc.writeByte(']')
defer func() {
enc.buf = make([]byte, 0, 512)
enc.Release()
}()
return enc.buf, nil
}
// MarshalJSONObject returns the JSON encoding of v, an implementation of MarshalerJSONObject.
//
// Example:
// type Object struct {
// id int
// }
// func (s *Object) MarshalJSONObject(enc *gojay.Encoder) {
// enc.IntKey("id", s.id)
// }
// func (s *Object) IsNil() bool {
// return s == nil
// }
//
// func main() {
// test := &Object{
// id: 123456,
// }
// b, _ := gojay.Marshal(test)
// fmt.Println(b) // {"id":123456}
// }
func MarshalJSONObject(v MarshalerJSONObject) ([]byte, error) {
enc := BorrowEncoder(nil)
enc.grow(512)
defer func() {
enc.buf = make([]byte, 0, 512)
enc.Release()
}()
return enc.encodeObject(v)
}
// Marshal returns the JSON encoding of v.
//
// If v is nil, not an implementation MarshalerJSONObject or MarshalerJSONArray or not one of the following types:
// string, int, int8, int16, int32, int64, uint8, uint16, uint32, uint64, float64, float32, bool
// Marshal returns an InvalidMarshalError.
func Marshal(v interface{}) ([]byte, error) {
return marshal(v, false)
}
// MarshalAny returns the JSON encoding of v.
//
// If v is nil, not an implementation MarshalerJSONObject or MarshalerJSONArray or not one of the following types:
// string, int, int8, int16, int32, int64, uint8, uint16, uint32, uint64, float64, float32, bool
// MarshalAny falls back to "json/encoding" package to marshal the value.
func MarshalAny(v interface{}) ([]byte, error) {
return marshal(v, true)
}
func marshal(v interface{}, any bool) ([]byte, error) {
var (
enc = BorrowEncoder(nil)
buf []byte
err error
)
defer func() {
enc.buf = make([]byte, 0, 512)
enc.Release()
}()
buf, err = func() ([]byte, error) {
switch vt := v.(type) {
case MarshalerJSONObject:
return enc.encodeObject(vt)
case MarshalerJSONArray:
return enc.encodeArray(vt)
case string:
return enc.encodeString(vt)
case bool:
return enc.encodeBool(vt)
case int:
return enc.encodeInt(vt)
case int64:
return enc.encodeInt64(vt)
case int32:
return enc.encodeInt(int(vt))
case int16:
return enc.encodeInt(int(vt))
case int8:
return enc.encodeInt(int(vt))
case uint64:
return enc.encodeInt(int(vt))
case uint32:
return enc.encodeInt(int(vt))
case uint16:
return enc.encodeInt(int(vt))
case uint8:
return enc.encodeInt(int(vt))
case float64:
return enc.encodeFloat(vt)
case float32:
return enc.encodeFloat32(vt)
case *EmbeddedJSON:
return enc.encodeEmbeddedJSON(vt)
default:
if any {
return json.Marshal(vt)
}
return nil, InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt))
}
}()
return buf, err
}
// MarshalerJSONObject is the interface to implement for struct to be encoded
type MarshalerJSONObject interface {
MarshalJSONObject(enc *Encoder)
IsNil() bool
}
// MarshalerJSONArray is the interface to implement
// for a slice or an array to be encoded
type MarshalerJSONArray interface {
MarshalJSONArray(enc *Encoder)
IsNil() bool
}
// An Encoder writes JSON values to an output stream.
type Encoder struct {
buf []byte
isPooled byte
w io.Writer
err error
hasKeys bool
keys []string
}
// AppendBytes allows a modular usage by appending bytes manually to the current state of the buffer.
func (enc *Encoder) AppendBytes(b []byte) {
enc.writeBytes(b)
}
// AppendByte allows a modular usage by appending a single byte manually to the current state of the buffer.
func (enc *Encoder) AppendByte(b byte) {
enc.writeByte(b)
}
// Buf returns the Encoder's buffer.
func (enc *Encoder) Buf() []byte {
return enc.buf
}
// Write writes to the io.Writer and resets the buffer.
func (enc *Encoder) Write() (int, error) {
i, err := enc.w.Write(enc.buf)
enc.buf = enc.buf[:0]
return i, err
}
func (enc *Encoder) getPreviousRune() byte {
last := len(enc.buf) - 1
return enc.buf[last]
}
+212
View File
@@ -0,0 +1,212 @@
package gojay
// EncodeArray encodes an implementation of MarshalerJSONArray to JSON
func (enc *Encoder) EncodeArray(v MarshalerJSONArray) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeArray(v)
_, err := enc.Write()
if err != nil {
enc.err = err
return err
}
return nil
}
func (enc *Encoder) encodeArray(v MarshalerJSONArray) ([]byte, error) {
enc.grow(200)
enc.writeByte('[')
v.MarshalJSONArray(enc)
enc.writeByte(']')
return enc.buf, enc.err
}
// AddArray adds an implementation of MarshalerJSONArray to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement Marshaler
func (enc *Encoder) AddArray(v MarshalerJSONArray) {
enc.Array(v)
}
// AddArrayOmitEmpty adds an array or slice to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerAddArrayOmitEmpty
func (enc *Encoder) AddArrayOmitEmpty(v MarshalerJSONArray) {
enc.ArrayOmitEmpty(v)
}
// AddArrayNullEmpty adds an array or slice to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement Marshaler, if v is empty, `null` will be encoded`
func (enc *Encoder) AddArrayNullEmpty(v MarshalerJSONArray) {
enc.ArrayNullEmpty(v)
}
// AddArrayKey adds an array or slice to be encoded, must be used inside an object as it will encode a key
// value must implement Marshaler
func (enc *Encoder) AddArrayKey(key string, v MarshalerJSONArray) {
enc.ArrayKey(key, v)
}
// AddArrayKeyOmitEmpty adds an array or slice to be encoded and skips it if it is nil.
// Must be called inside an object as it will encode a key.
func (enc *Encoder) AddArrayKeyOmitEmpty(key string, v MarshalerJSONArray) {
enc.ArrayKeyOmitEmpty(key, v)
}
// AddArrayKeyNullEmpty adds an array or slice to be encoded and skips it if it is nil.
// Must be called inside an object as it will encode a key. `null` will be encoded`
func (enc *Encoder) AddArrayKeyNullEmpty(key string, v MarshalerJSONArray) {
enc.ArrayKeyNullEmpty(key, v)
}
// Array adds an implementation of MarshalerJSONArray to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement Marshaler
func (enc *Encoder) Array(v MarshalerJSONArray) {
if v.IsNil() {
enc.grow(3)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeByte('[')
enc.writeByte(']')
return
}
enc.grow(100)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeByte('[')
v.MarshalJSONArray(enc)
enc.writeByte(']')
}
// ArrayOmitEmpty adds an array or slice to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement Marshaler
func (enc *Encoder) ArrayOmitEmpty(v MarshalerJSONArray) {
if v.IsNil() {
return
}
enc.grow(4)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeByte('[')
v.MarshalJSONArray(enc)
enc.writeByte(']')
}
// ArrayNullEmpty adds an array or slice to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement Marshaler
func (enc *Encoder) ArrayNullEmpty(v MarshalerJSONArray) {
enc.grow(4)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
if v.IsNil() {
enc.writeBytes(nullBytes)
return
}
enc.writeByte('[')
v.MarshalJSONArray(enc)
enc.writeByte(']')
}
// ArrayKey adds an array or slice to be encoded, must be used inside an object as it will encode a key
// value must implement Marshaler
func (enc *Encoder) ArrayKey(key string, v MarshalerJSONArray) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v.IsNil() {
enc.grow(2 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyArr)
enc.writeByte(']')
return
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyArr)
v.MarshalJSONArray(enc)
enc.writeByte(']')
}
// ArrayKeyOmitEmpty adds an array or slice to be encoded and skips if it is nil.
// Must be called inside an object as it will encode a key.
func (enc *Encoder) ArrayKeyOmitEmpty(key string, v MarshalerJSONArray) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v.IsNil() {
return
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyArr)
v.MarshalJSONArray(enc)
enc.writeByte(']')
}
// ArrayKeyNullEmpty adds an array or slice to be encoded and encodes `null`` if it is nil.
// Must be called inside an object as it will encode a key.
func (enc *Encoder) ArrayKeyNullEmpty(key string, v MarshalerJSONArray) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
if v.IsNil() {
enc.writeBytes(nullBytes)
return
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyArr)
v.MarshalJSONArray(enc)
enc.writeByte(']')
}
// EncodeArrayFunc is a custom func type implementing MarshaleArray.
// Use it to cast a func(*Encoder) to Marshal an object.
//
// enc := gojay.NewEncoder(io.Writer)
// enc.EncodeArray(gojay.EncodeArrayFunc(func(enc *gojay.Encoder) {
// enc.AddStringKey("hello", "world")
// }))
type EncodeArrayFunc func(*Encoder)
// MarshalJSONArray implements MarshalerJSONArray.
func (f EncodeArrayFunc) MarshalJSONArray(enc *Encoder) {
f(enc)
}
// IsNil implements MarshalerJSONArray.
func (f EncodeArrayFunc) IsNil() bool {
return f == nil
}
+164
View File
@@ -0,0 +1,164 @@
package gojay
import "strconv"
// EncodeBool encodes a bool to JSON
func (enc *Encoder) EncodeBool(v bool) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeBool(v)
_, err := enc.Write()
if err != nil {
enc.err = err
return err
}
return nil
}
// encodeBool encodes a bool to JSON
func (enc *Encoder) encodeBool(v bool) ([]byte, error) {
enc.grow(5)
if v {
enc.writeString("true")
} else {
enc.writeString("false")
}
return enc.buf, enc.err
}
// AddBool adds a bool to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddBool(v bool) {
enc.Bool(v)
}
// AddBoolOmitEmpty adds a bool to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddBoolOmitEmpty(v bool) {
enc.BoolOmitEmpty(v)
}
// AddBoolNullEmpty adds a bool to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddBoolNullEmpty(v bool) {
enc.BoolNullEmpty(v)
}
// AddBoolKey adds a bool to be encoded, must be used inside an object as it will encode a key.
func (enc *Encoder) AddBoolKey(key string, v bool) {
enc.BoolKey(key, v)
}
// AddBoolKeyOmitEmpty adds a bool to be encoded and skips if it is zero value.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddBoolKeyOmitEmpty(key string, v bool) {
enc.BoolKeyOmitEmpty(key, v)
}
// AddBoolKeyNullEmpty adds a bool to be encoded and encodes `null` if it is zero value.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddBoolKeyNullEmpty(key string, v bool) {
enc.BoolKeyNullEmpty(key, v)
}
// Bool adds a bool to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Bool(v bool) {
enc.grow(5)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
if v {
enc.writeString("true")
} else {
enc.writeString("false")
}
}
// BoolOmitEmpty adds a bool to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) BoolOmitEmpty(v bool) {
if v == false {
return
}
enc.grow(5)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeString("true")
}
// BoolNullEmpty adds a bool to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) BoolNullEmpty(v bool) {
enc.grow(5)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
if v == false {
enc.writeBytes(nullBytes)
return
}
enc.writeString("true")
}
// BoolKey adds a bool to be encoded, must be used inside an object as it will encode a key.
func (enc *Encoder) BoolKey(key string, value bool) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.buf = strconv.AppendBool(enc.buf, value)
}
// BoolKeyOmitEmpty adds a bool to be encoded and skips it if it is zero value.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) BoolKeyOmitEmpty(key string, v bool) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v == false {
return
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.buf = strconv.AppendBool(enc.buf, v)
}
// BoolKeyNullEmpty adds a bool to be encoded and skips it if it is zero value.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) BoolKeyNullEmpty(key string, v bool) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
if v == false {
enc.writeBytes(nullBytes)
return
}
enc.buf = strconv.AppendBool(enc.buf, v)
}
+65
View File
@@ -0,0 +1,65 @@
package gojay
const hex = "0123456789abcdef"
// grow grows b's capacity, if necessary, to guarantee space for
// another n bytes. After grow(n), at least n bytes can be written to b
// without another allocation. If n is negative, grow panics.
func (enc *Encoder) grow(n int) {
if cap(enc.buf)-len(enc.buf) < n {
Buf := make([]byte, len(enc.buf), 2*cap(enc.buf)+n)
copy(Buf, enc.buf)
enc.buf = Buf
}
}
// Write appends the contents of p to b's Buffer.
// Write always returns len(p), nil.
func (enc *Encoder) writeBytes(p []byte) {
enc.buf = append(enc.buf, p...)
}
func (enc *Encoder) writeTwoBytes(b1 byte, b2 byte) {
enc.buf = append(enc.buf, b1, b2)
}
// WriteByte appends the byte c to b's Buffer.
// The returned error is always nil.
func (enc *Encoder) writeByte(c byte) {
enc.buf = append(enc.buf, c)
}
// WriteString appends the contents of s to b's Buffer.
// It returns the length of s and a nil error.
func (enc *Encoder) writeString(s string) {
enc.buf = append(enc.buf, s...)
}
func (enc *Encoder) writeStringEscape(s string) {
l := len(s)
for i := 0; i < l; i++ {
c := s[i]
if c >= 0x20 && c != '\\' && c != '"' {
enc.writeByte(c)
continue
}
switch c {
case '\\', '"':
enc.writeTwoBytes('\\', c)
case '\n':
enc.writeTwoBytes('\\', 'n')
case '\f':
enc.writeTwoBytes('\\', 'f')
case '\b':
enc.writeTwoBytes('\\', 'b')
case '\r':
enc.writeTwoBytes('\\', 'r')
case '\t':
enc.writeTwoBytes('\\', 't')
default:
enc.writeString(`\u00`)
enc.writeTwoBytes(hex[c>>4], hex[c&0xF])
}
continue
}
}
+93
View File
@@ -0,0 +1,93 @@
package gojay
// EncodeEmbeddedJSON encodes an embedded JSON.
// is basically sets the internal buf as the value pointed by v and calls the io.Writer.Write()
func (enc *Encoder) EncodeEmbeddedJSON(v *EmbeddedJSON) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
enc.buf = *v
_, err := enc.Write()
if err != nil {
return err
}
return nil
}
func (enc *Encoder) encodeEmbeddedJSON(v *EmbeddedJSON) ([]byte, error) {
enc.writeBytes(*v)
return enc.buf, nil
}
// AddEmbeddedJSON adds an EmbeddedJSON to be encoded.
//
// It basically blindly writes the bytes to the final buffer. Therefore,
// it expects the JSON to be of proper format.
func (enc *Encoder) AddEmbeddedJSON(v *EmbeddedJSON) {
enc.grow(len(*v) + 4)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeBytes(*v)
}
// AddEmbeddedJSONOmitEmpty adds an EmbeddedJSON to be encoded or skips it if nil pointer or empty.
//
// It basically blindly writes the bytes to the final buffer. Therefore,
// it expects the JSON to be of proper format.
func (enc *Encoder) AddEmbeddedJSONOmitEmpty(v *EmbeddedJSON) {
if v == nil || len(*v) == 0 {
return
}
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeBytes(*v)
}
// AddEmbeddedJSONKey adds an EmbeddedJSON and a key to be encoded.
//
// It basically blindly writes the bytes to the final buffer. Therefore,
// it expects the JSON to be of proper format.
func (enc *Encoder) AddEmbeddedJSONKey(key string, v *EmbeddedJSON) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(len(key) + len(*v) + 5)
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.writeBytes(*v)
}
// AddEmbeddedJSONKeyOmitEmpty adds an EmbeddedJSON and a key to be encoded or skips it if nil pointer or empty.
//
// It basically blindly writes the bytes to the final buffer. Therefore,
// it expects the JSON to be of proper format.
func (enc *Encoder) AddEmbeddedJSONKeyOmitEmpty(key string, v *EmbeddedJSON) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v == nil || len(*v) == 0 {
return
}
enc.grow(len(key) + len(*v) + 5)
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.writeBytes(*v)
}
+173
View File
@@ -0,0 +1,173 @@
package gojay
import (
"fmt"
)
// Encode encodes a value to JSON.
//
// If Encode cannot find a way to encode the type to JSON
// it will return an InvalidMarshalError.
func (enc *Encoder) Encode(v interface{}) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
switch vt := v.(type) {
case string:
return enc.EncodeString(vt)
case bool:
return enc.EncodeBool(vt)
case MarshalerJSONArray:
return enc.EncodeArray(vt)
case MarshalerJSONObject:
return enc.EncodeObject(vt)
case int:
return enc.EncodeInt(vt)
case int64:
return enc.EncodeInt64(vt)
case int32:
return enc.EncodeInt(int(vt))
case int8:
return enc.EncodeInt(int(vt))
case uint64:
return enc.EncodeUint64(vt)
case uint32:
return enc.EncodeInt(int(vt))
case uint16:
return enc.EncodeInt(int(vt))
case uint8:
return enc.EncodeInt(int(vt))
case float64:
return enc.EncodeFloat(vt)
case float32:
return enc.EncodeFloat32(vt)
case *EmbeddedJSON:
return enc.EncodeEmbeddedJSON(vt)
default:
return InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt))
}
}
// AddInterface adds an interface{} to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddInterface(value interface{}) {
switch vt := value.(type) {
case string:
enc.AddString(vt)
case bool:
enc.AddBool(vt)
case MarshalerJSONArray:
enc.AddArray(vt)
case MarshalerJSONObject:
enc.AddObject(vt)
case int:
enc.AddInt(vt)
case int64:
enc.AddInt(int(vt))
case int32:
enc.AddInt(int(vt))
case int8:
enc.AddInt(int(vt))
case uint64:
enc.AddUint64(vt)
case uint32:
enc.AddInt(int(vt))
case uint16:
enc.AddInt(int(vt))
case uint8:
enc.AddInt(int(vt))
case float64:
enc.AddFloat(vt)
case float32:
enc.AddFloat32(vt)
default:
if vt != nil {
enc.err = InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt))
return
}
return
}
}
// AddInterfaceKey adds an interface{} to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddInterfaceKey(key string, value interface{}) {
switch vt := value.(type) {
case string:
enc.AddStringKey(key, vt)
case bool:
enc.AddBoolKey(key, vt)
case MarshalerJSONArray:
enc.AddArrayKey(key, vt)
case MarshalerJSONObject:
enc.AddObjectKey(key, vt)
case int:
enc.AddIntKey(key, vt)
case int64:
enc.AddIntKey(key, int(vt))
case int32:
enc.AddIntKey(key, int(vt))
case int16:
enc.AddIntKey(key, int(vt))
case int8:
enc.AddIntKey(key, int(vt))
case uint64:
enc.AddIntKey(key, int(vt))
case uint32:
enc.AddIntKey(key, int(vt))
case uint16:
enc.AddIntKey(key, int(vt))
case uint8:
enc.AddIntKey(key, int(vt))
case float64:
enc.AddFloatKey(key, vt)
case float32:
enc.AddFloat32Key(key, vt)
default:
if vt != nil {
enc.err = InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt))
return
}
return
}
}
// AddInterfaceKeyOmitEmpty adds an interface{} to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddInterfaceKeyOmitEmpty(key string, v interface{}) {
switch vt := v.(type) {
case string:
enc.AddStringKeyOmitEmpty(key, vt)
case bool:
enc.AddBoolKeyOmitEmpty(key, vt)
case MarshalerJSONArray:
enc.AddArrayKeyOmitEmpty(key, vt)
case MarshalerJSONObject:
enc.AddObjectKeyOmitEmpty(key, vt)
case int:
enc.AddIntKeyOmitEmpty(key, vt)
case int64:
enc.AddIntKeyOmitEmpty(key, int(vt))
case int32:
enc.AddIntKeyOmitEmpty(key, int(vt))
case int16:
enc.AddIntKeyOmitEmpty(key, int(vt))
case int8:
enc.AddIntKeyOmitEmpty(key, int(vt))
case uint64:
enc.AddIntKeyOmitEmpty(key, int(vt))
case uint32:
enc.AddIntKeyOmitEmpty(key, int(vt))
case uint16:
enc.AddIntKeyOmitEmpty(key, int(vt))
case uint8:
enc.AddIntKeyOmitEmpty(key, int(vt))
case float64:
enc.AddFloatKeyOmitEmpty(key, vt)
case float32:
enc.AddFloat32KeyOmitEmpty(key, vt)
default:
if vt != nil {
enc.err = InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt))
return
}
return
}
}
+39
View File
@@ -0,0 +1,39 @@
package gojay
// AddNull adds a `null` to be encoded. Must be used while encoding an array.`
func (enc *Encoder) AddNull() {
enc.Null()
}
// Null adds a `null` to be encoded. Must be used while encoding an array.`
func (enc *Encoder) Null() {
enc.grow(5)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeBytes(nullBytes)
}
// AddNullKey adds a `null` to be encoded. Must be used while encoding an array.`
func (enc *Encoder) AddNullKey(key string) {
enc.NullKey(key)
}
// NullKey adds a `null` to be encoded. Must be used while encoding an array.`
func (enc *Encoder) NullKey(key string) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.writeBytes(nullBytes)
}
+1
View File
@@ -0,0 +1 @@
package gojay
+368
View File
@@ -0,0 +1,368 @@
package gojay
import "strconv"
// EncodeFloat encodes a float64 to JSON
func (enc *Encoder) EncodeFloat(n float64) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeFloat(n)
_, err := enc.Write()
if err != nil {
return err
}
return nil
}
// encodeFloat encodes a float64 to JSON
func (enc *Encoder) encodeFloat(n float64) ([]byte, error) {
enc.buf = strconv.AppendFloat(enc.buf, n, 'f', -1, 64)
return enc.buf, nil
}
// EncodeFloat32 encodes a float32 to JSON
func (enc *Encoder) EncodeFloat32(n float32) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeFloat32(n)
_, err := enc.Write()
if err != nil {
return err
}
return nil
}
func (enc *Encoder) encodeFloat32(n float32) ([]byte, error) {
enc.buf = strconv.AppendFloat(enc.buf, float64(n), 'f', -1, 32)
return enc.buf, nil
}
// AddFloat adds a float64 to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddFloat(v float64) {
enc.Float64(v)
}
// AddFloatOmitEmpty adds a float64 to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddFloatOmitEmpty(v float64) {
enc.Float64OmitEmpty(v)
}
// AddFloatNullEmpty adds a float64 to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddFloatNullEmpty(v float64) {
enc.Float64NullEmpty(v)
}
// Float adds a float64 to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Float(v float64) {
enc.Float64(v)
}
// FloatOmitEmpty adds a float64 to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) FloatOmitEmpty(v float64) {
enc.Float64OmitEmpty(v)
}
// FloatNullEmpty adds a float64 to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) FloatNullEmpty(v float64) {
enc.Float64NullEmpty(v)
}
// AddFloatKey adds a float64 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddFloatKey(key string, v float64) {
enc.Float64Key(key, v)
}
// AddFloatKeyOmitEmpty adds a float64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddFloatKeyOmitEmpty(key string, v float64) {
enc.Float64KeyOmitEmpty(key, v)
}
// AddFloatKeyNullEmpty adds a float64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddFloatKeyNullEmpty(key string, v float64) {
enc.Float64KeyNullEmpty(key, v)
}
// FloatKey adds a float64 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) FloatKey(key string, v float64) {
enc.Float64Key(key, v)
}
// FloatKeyOmitEmpty adds a float64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key
func (enc *Encoder) FloatKeyOmitEmpty(key string, v float64) {
enc.Float64KeyOmitEmpty(key, v)
}
// FloatKeyNullEmpty adds a float64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key
func (enc *Encoder) FloatKeyNullEmpty(key string, v float64) {
enc.Float64KeyNullEmpty(key, v)
}
// AddFloat64 adds a float64 to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddFloat64(v float64) {
enc.Float(v)
}
// AddFloat64OmitEmpty adds a float64 to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddFloat64OmitEmpty(v float64) {
enc.FloatOmitEmpty(v)
}
// Float64 adds a float64 to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Float64(v float64) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendFloat(enc.buf, v, 'f', -1, 64)
}
// Float64OmitEmpty adds a float64 to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Float64OmitEmpty(v float64) {
if v == 0 {
return
}
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendFloat(enc.buf, v, 'f', -1, 64)
}
// Float64NullEmpty adds a float64 to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Float64NullEmpty(v float64) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
if v == 0 {
enc.writeBytes(nullBytes)
return
}
enc.buf = strconv.AppendFloat(enc.buf, v, 'f', -1, 64)
}
// AddFloat64Key adds a float64 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddFloat64Key(key string, v float64) {
enc.FloatKey(key, v)
}
// AddFloat64KeyOmitEmpty adds a float64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddFloat64KeyOmitEmpty(key string, v float64) {
enc.FloatKeyOmitEmpty(key, v)
}
// Float64Key adds a float64 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) Float64Key(key string, value float64) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.grow(10)
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.buf = strconv.AppendFloat(enc.buf, value, 'f', -1, 64)
}
// Float64KeyOmitEmpty adds a float64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key
func (enc *Encoder) Float64KeyOmitEmpty(key string, v float64) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v == 0 {
return
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.buf = strconv.AppendFloat(enc.buf, v, 'f', -1, 64)
}
// Float64KeyNullEmpty adds a float64 to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Float64KeyNullEmpty(key string, v float64) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
if v == 0 {
enc.writeBytes(nullBytes)
return
}
enc.buf = strconv.AppendFloat(enc.buf, v, 'f', -1, 64)
}
// AddFloat32 adds a float32 to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddFloat32(v float32) {
enc.Float32(v)
}
// AddFloat32OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddFloat32OmitEmpty(v float32) {
enc.Float32OmitEmpty(v)
}
// AddFloat32NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddFloat32NullEmpty(v float32) {
enc.Float32NullEmpty(v)
}
// Float32 adds a float32 to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Float32(v float32) {
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendFloat(enc.buf, float64(v), 'f', -1, 32)
}
// Float32OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Float32OmitEmpty(v float32) {
if v == 0 {
return
}
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendFloat(enc.buf, float64(v), 'f', -1, 32)
}
// Float32NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Float32NullEmpty(v float32) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
if v == 0 {
enc.writeBytes(nullBytes)
return
}
enc.buf = strconv.AppendFloat(enc.buf, float64(v), 'f', -1, 32)
}
// AddFloat32Key adds a float32 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddFloat32Key(key string, v float32) {
enc.Float32Key(key, v)
}
// AddFloat32KeyOmitEmpty adds a float64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddFloat32KeyOmitEmpty(key string, v float32) {
enc.Float32KeyOmitEmpty(key, v)
}
// AddFloat32KeyNullEmpty adds a float64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddFloat32KeyNullEmpty(key string, v float32) {
enc.Float32KeyNullEmpty(key, v)
}
// Float32Key adds a float32 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) Float32Key(key string, v float32) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeByte('"')
enc.writeByte(':')
enc.buf = strconv.AppendFloat(enc.buf, float64(v), 'f', -1, 32)
}
// Float32KeyOmitEmpty adds a float64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key
func (enc *Encoder) Float32KeyOmitEmpty(key string, v float32) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v == 0 {
return
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.buf = strconv.AppendFloat(enc.buf, float64(v), 'f', -1, 32)
}
// Float32KeyNullEmpty adds a float64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key
func (enc *Encoder) Float32KeyNullEmpty(key string, v float32) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
if v == 0 {
enc.writeBytes(nullBytes)
return
}
enc.buf = strconv.AppendFloat(enc.buf, float64(v), 'f', -1, 32)
}
+500
View File
@@ -0,0 +1,500 @@
package gojay
import "strconv"
// EncodeInt encodes an int to JSON
func (enc *Encoder) EncodeInt(n int) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeInt(n)
_, err := enc.Write()
if err != nil {
return err
}
return nil
}
// encodeInt encodes an int to JSON
func (enc *Encoder) encodeInt(n int) ([]byte, error) {
enc.buf = strconv.AppendInt(enc.buf, int64(n), 10)
return enc.buf, nil
}
// EncodeInt64 encodes an int64 to JSON
func (enc *Encoder) EncodeInt64(n int64) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeInt64(n)
_, err := enc.Write()
if err != nil {
return err
}
return nil
}
// encodeInt64 encodes an int to JSON
func (enc *Encoder) encodeInt64(n int64) ([]byte, error) {
enc.buf = strconv.AppendInt(enc.buf, n, 10)
return enc.buf, nil
}
// AddInt adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddInt(v int) {
enc.Int(v)
}
// AddIntOmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddIntOmitEmpty(v int) {
enc.IntOmitEmpty(v)
}
// AddIntNullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddIntNullEmpty(v int) {
enc.IntNullEmpty(v)
}
// Int adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Int(v int) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendInt(enc.buf, int64(v), 10)
}
// IntOmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) IntOmitEmpty(v int) {
if v == 0 {
return
}
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendInt(enc.buf, int64(v), 10)
}
// IntNullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) IntNullEmpty(v int) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
if v == 0 {
enc.writeBytes(nullBytes)
return
}
enc.buf = strconv.AppendInt(enc.buf, int64(v), 10)
}
// AddIntKey adds an int to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddIntKey(key string, v int) {
enc.IntKey(key, v)
}
// AddIntKeyOmitEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddIntKeyOmitEmpty(key string, v int) {
enc.IntKeyOmitEmpty(key, v)
}
// AddIntKeyNullEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddIntKeyNullEmpty(key string, v int) {
enc.IntKeyNullEmpty(key, v)
}
// IntKey adds an int to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) IntKey(key string, v int) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.buf = strconv.AppendInt(enc.buf, int64(v), 10)
}
// IntKeyOmitEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) IntKeyOmitEmpty(key string, v int) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v == 0 {
return
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' && r != '[' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.buf = strconv.AppendInt(enc.buf, int64(v), 10)
}
// IntKeyNullEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) IntKeyNullEmpty(key string, v int) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' && r != '[' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
if v == 0 {
enc.writeBytes(nullBytes)
return
}
enc.buf = strconv.AppendInt(enc.buf, int64(v), 10)
}
// AddInt64 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddInt64(v int64) {
enc.Int64(v)
}
// AddInt64OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddInt64OmitEmpty(v int64) {
enc.Int64OmitEmpty(v)
}
// AddInt64NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddInt64NullEmpty(v int64) {
enc.Int64NullEmpty(v)
}
// Int64 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Int64(v int64) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendInt(enc.buf, v, 10)
}
// Int64OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Int64OmitEmpty(v int64) {
if v == 0 {
return
}
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendInt(enc.buf, v, 10)
}
// Int64NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Int64NullEmpty(v int64) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
if v == 0 {
enc.writeBytes(nullBytes)
return
}
enc.buf = strconv.AppendInt(enc.buf, v, 10)
}
// AddInt64Key adds an int64 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddInt64Key(key string, v int64) {
enc.Int64Key(key, v)
}
// AddInt64KeyOmitEmpty adds an int64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddInt64KeyOmitEmpty(key string, v int64) {
enc.Int64KeyOmitEmpty(key, v)
}
// AddInt64KeyNullEmpty adds an int64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddInt64KeyNullEmpty(key string, v int64) {
enc.Int64KeyNullEmpty(key, v)
}
// Int64Key adds an int64 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) Int64Key(key string, v int64) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.buf = strconv.AppendInt(enc.buf, v, 10)
}
// Int64KeyOmitEmpty adds an int64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Int64KeyOmitEmpty(key string, v int64) {
if v == 0 {
return
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.buf = strconv.AppendInt(enc.buf, v, 10)
}
// Int64KeyNullEmpty adds an int64 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Int64KeyNullEmpty(key string, v int64) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
if v == 0 {
enc.writeBytes(nullBytes)
return
}
enc.buf = strconv.AppendInt(enc.buf, v, 10)
}
// AddInt32 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddInt32(v int32) {
enc.Int64(int64(v))
}
// AddInt32OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddInt32OmitEmpty(v int32) {
enc.Int64OmitEmpty(int64(v))
}
// AddInt32NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddInt32NullEmpty(v int32) {
enc.Int64NullEmpty(int64(v))
}
// Int32 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Int32(v int32) {
enc.Int64(int64(v))
}
// Int32OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Int32OmitEmpty(v int32) {
enc.Int64OmitEmpty(int64(v))
}
// Int32NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Int32NullEmpty(v int32) {
enc.Int64NullEmpty(int64(v))
}
// AddInt32Key adds an int32 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddInt32Key(key string, v int32) {
enc.Int64Key(key, int64(v))
}
// AddInt32KeyOmitEmpty adds an int32 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddInt32KeyOmitEmpty(key string, v int32) {
enc.Int64KeyOmitEmpty(key, int64(v))
}
// Int32Key adds an int32 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) Int32Key(key string, v int32) {
enc.Int64Key(key, int64(v))
}
// Int32KeyOmitEmpty adds an int32 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Int32KeyOmitEmpty(key string, v int32) {
enc.Int64KeyOmitEmpty(key, int64(v))
}
// Int32KeyNullEmpty adds an int32 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Int32KeyNullEmpty(key string, v int32) {
enc.Int64KeyNullEmpty(key, int64(v))
}
// AddInt16 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddInt16(v int16) {
enc.Int64(int64(v))
}
// AddInt16OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddInt16OmitEmpty(v int16) {
enc.Int64OmitEmpty(int64(v))
}
// Int16 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Int16(v int16) {
enc.Int64(int64(v))
}
// Int16OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Int16OmitEmpty(v int16) {
enc.Int64OmitEmpty(int64(v))
}
// Int16NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Int16NullEmpty(v int16) {
enc.Int64NullEmpty(int64(v))
}
// AddInt16Key adds an int16 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddInt16Key(key string, v int16) {
enc.Int64Key(key, int64(v))
}
// AddInt16KeyOmitEmpty adds an int16 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddInt16KeyOmitEmpty(key string, v int16) {
enc.Int64KeyOmitEmpty(key, int64(v))
}
// AddInt16KeyNullEmpty adds an int16 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddInt16KeyNullEmpty(key string, v int16) {
enc.Int64KeyNullEmpty(key, int64(v))
}
// Int16Key adds an int16 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) Int16Key(key string, v int16) {
enc.Int64Key(key, int64(v))
}
// Int16KeyOmitEmpty adds an int16 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Int16KeyOmitEmpty(key string, v int16) {
enc.Int64KeyOmitEmpty(key, int64(v))
}
// Int16KeyNullEmpty adds an int16 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Int16KeyNullEmpty(key string, v int16) {
enc.Int64KeyNullEmpty(key, int64(v))
}
// AddInt8 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddInt8(v int8) {
enc.Int64(int64(v))
}
// AddInt8OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddInt8OmitEmpty(v int8) {
enc.Int64OmitEmpty(int64(v))
}
// AddInt8NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddInt8NullEmpty(v int8) {
enc.Int64NullEmpty(int64(v))
}
// Int8 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Int8(v int8) {
enc.Int64(int64(v))
}
// Int8OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Int8OmitEmpty(v int8) {
enc.Int64OmitEmpty(int64(v))
}
// Int8NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Int8NullEmpty(v int8) {
enc.Int64NullEmpty(int64(v))
}
// AddInt8Key adds an int8 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddInt8Key(key string, v int8) {
enc.Int64Key(key, int64(v))
}
// AddInt8KeyOmitEmpty adds an int8 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddInt8KeyOmitEmpty(key string, v int8) {
enc.Int64KeyOmitEmpty(key, int64(v))
}
// AddInt8KeyNullEmpty adds an int8 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddInt8KeyNullEmpty(key string, v int8) {
enc.Int64KeyNullEmpty(key, int64(v))
}
// Int8Key adds an int8 to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) Int8Key(key string, v int8) {
enc.Int64Key(key, int64(v))
}
// Int8KeyOmitEmpty adds an int8 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Int8KeyOmitEmpty(key string, v int8) {
enc.Int64KeyOmitEmpty(key, int64(v))
}
// Int8KeyNullEmpty adds an int8 to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Int8KeyNullEmpty(key string, v int8) {
enc.Int64KeyNullEmpty(key, int64(v))
}
+362
View File
@@ -0,0 +1,362 @@
package gojay
import "strconv"
// EncodeUint64 encodes an int64 to JSON
func (enc *Encoder) EncodeUint64(n uint64) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeUint64(n)
_, err := enc.Write()
if err != nil {
return err
}
return nil
}
// encodeUint64 encodes an int to JSON
func (enc *Encoder) encodeUint64(n uint64) ([]byte, error) {
enc.buf = strconv.AppendUint(enc.buf, n, 10)
return enc.buf, nil
}
// AddUint64 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddUint64(v uint64) {
enc.Uint64(v)
}
// AddUint64OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddUint64OmitEmpty(v uint64) {
enc.Uint64OmitEmpty(v)
}
// AddUint64NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddUint64NullEmpty(v uint64) {
enc.Uint64NullEmpty(v)
}
// Uint64 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Uint64(v uint64) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendUint(enc.buf, v, 10)
}
// Uint64OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Uint64OmitEmpty(v uint64) {
if v == 0 {
return
}
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.buf = strconv.AppendUint(enc.buf, v, 10)
}
// Uint64NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Uint64NullEmpty(v uint64) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
if v == 0 {
enc.writeBytes(nullBytes)
return
}
enc.buf = strconv.AppendUint(enc.buf, v, 10)
}
// AddUint64Key adds an int to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddUint64Key(key string, v uint64) {
enc.Uint64Key(key, v)
}
// AddUint64KeyOmitEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddUint64KeyOmitEmpty(key string, v uint64) {
enc.Uint64KeyOmitEmpty(key, v)
}
// AddUint64KeyNullEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddUint64KeyNullEmpty(key string, v uint64) {
enc.Uint64KeyNullEmpty(key, v)
}
// Uint64Key adds an int to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) Uint64Key(key string, v uint64) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.buf = strconv.AppendUint(enc.buf, v, 10)
}
// Uint64KeyOmitEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Uint64KeyOmitEmpty(key string, v uint64) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v == 0 {
return
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' && r != '[' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
enc.buf = strconv.AppendUint(enc.buf, v, 10)
}
// Uint64KeyNullEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Uint64KeyNullEmpty(key string, v uint64) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' && r != '[' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
if v == 0 {
enc.writeBytes(nullBytes)
return
}
enc.buf = strconv.AppendUint(enc.buf, v, 10)
}
// AddUint32 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddUint32(v uint32) {
enc.Uint64(uint64(v))
}
// AddUint32OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddUint32OmitEmpty(v uint32) {
enc.Uint64OmitEmpty(uint64(v))
}
// AddUint32NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddUint32NullEmpty(v uint32) {
enc.Uint64NullEmpty(uint64(v))
}
// Uint32 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Uint32(v uint32) {
enc.Uint64(uint64(v))
}
// Uint32OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Uint32OmitEmpty(v uint32) {
enc.Uint64OmitEmpty(uint64(v))
}
// Uint32NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Uint32NullEmpty(v uint32) {
enc.Uint64NullEmpty(uint64(v))
}
// AddUint32Key adds an int to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddUint32Key(key string, v uint32) {
enc.Uint64Key(key, uint64(v))
}
// AddUint32KeyOmitEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddUint32KeyOmitEmpty(key string, v uint32) {
enc.Uint64KeyOmitEmpty(key, uint64(v))
}
// AddUint32KeyNullEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddUint32KeyNullEmpty(key string, v uint32) {
enc.Uint64KeyNullEmpty(key, uint64(v))
}
// Uint32Key adds an int to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) Uint32Key(key string, v uint32) {
enc.Uint64Key(key, uint64(v))
}
// Uint32KeyOmitEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Uint32KeyOmitEmpty(key string, v uint32) {
enc.Uint64KeyOmitEmpty(key, uint64(v))
}
// Uint32KeyNullEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Uint32KeyNullEmpty(key string, v uint32) {
enc.Uint64KeyNullEmpty(key, uint64(v))
}
// AddUint16 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddUint16(v uint16) {
enc.Uint64(uint64(v))
}
// AddUint16OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddUint16OmitEmpty(v uint16) {
enc.Uint64OmitEmpty(uint64(v))
}
// AddUint16NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddUint16NullEmpty(v uint16) {
enc.Uint64NullEmpty(uint64(v))
}
// Uint16 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Uint16(v uint16) {
enc.Uint64(uint64(v))
}
// Uint16OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Uint16OmitEmpty(v uint16) {
enc.Uint64OmitEmpty(uint64(v))
}
// Uint16NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Uint16NullEmpty(v uint16) {
enc.Uint64NullEmpty(uint64(v))
}
// AddUint16Key adds an int to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddUint16Key(key string, v uint16) {
enc.Uint64Key(key, uint64(v))
}
// AddUint16KeyOmitEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddUint16KeyOmitEmpty(key string, v uint16) {
enc.Uint64KeyOmitEmpty(key, uint64(v))
}
// AddUint16KeyNullEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddUint16KeyNullEmpty(key string, v uint16) {
enc.Uint64KeyNullEmpty(key, uint64(v))
}
// Uint16Key adds an int to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) Uint16Key(key string, v uint16) {
enc.Uint64Key(key, uint64(v))
}
// Uint16KeyOmitEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Uint16KeyOmitEmpty(key string, v uint16) {
enc.Uint64KeyOmitEmpty(key, uint64(v))
}
// Uint16KeyNullEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Uint16KeyNullEmpty(key string, v uint16) {
enc.Uint64KeyNullEmpty(key, uint64(v))
}
// AddUint8 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddUint8(v uint8) {
enc.Uint64(uint64(v))
}
// AddUint8OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddUint8OmitEmpty(v uint8) {
enc.Uint64OmitEmpty(uint64(v))
}
// AddUint8NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) AddUint8NullEmpty(v uint8) {
enc.Uint64NullEmpty(uint64(v))
}
// Uint8 adds an int to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Uint8(v uint8) {
enc.Uint64(uint64(v))
}
// Uint8OmitEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Uint8OmitEmpty(v uint8) {
enc.Uint64OmitEmpty(uint64(v))
}
// Uint8NullEmpty adds an int to be encoded and skips it if its value is 0,
// must be used inside a slice or array encoding (does not encode a key).
func (enc *Encoder) Uint8NullEmpty(v uint8) {
enc.Uint64NullEmpty(uint64(v))
}
// AddUint8Key adds an int to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddUint8Key(key string, v uint8) {
enc.Uint64Key(key, uint64(v))
}
// AddUint8KeyOmitEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddUint8KeyOmitEmpty(key string, v uint8) {
enc.Uint64KeyOmitEmpty(key, uint64(v))
}
// AddUint8KeyNullEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) AddUint8KeyNullEmpty(key string, v uint8) {
enc.Uint64KeyNullEmpty(key, uint64(v))
}
// Uint8Key adds an int to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) Uint8Key(key string, v uint8) {
enc.Uint64Key(key, uint64(v))
}
// Uint8KeyOmitEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Uint8KeyOmitEmpty(key string, v uint8) {
enc.Uint64KeyOmitEmpty(key, uint64(v))
}
// Uint8KeyNullEmpty adds an int to be encoded and skips it if its value is 0.
// Must be used inside an object as it will encode a key.
func (enc *Encoder) Uint8KeyNullEmpty(key string, v uint8) {
enc.Uint64KeyNullEmpty(key, uint64(v))
}
+400
View File
@@ -0,0 +1,400 @@
package gojay
var objKeyStr = []byte(`":"`)
var objKeyObj = []byte(`":{`)
var objKeyArr = []byte(`":[`)
var objKey = []byte(`":`)
// EncodeObject encodes an object to JSON
func (enc *Encoder) EncodeObject(v MarshalerJSONObject) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, err := enc.encodeObject(v)
if err != nil {
enc.err = err
return err
}
_, err = enc.Write()
if err != nil {
enc.err = err
return err
}
return nil
}
// EncodeObjectKeys encodes an object to JSON
func (enc *Encoder) EncodeObjectKeys(v MarshalerJSONObject, keys []string) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
enc.hasKeys = true
enc.keys = keys
_, err := enc.encodeObject(v)
if err != nil {
enc.err = err
return err
}
_, err = enc.Write()
if err != nil {
enc.err = err
return err
}
return nil
}
func (enc *Encoder) encodeObject(v MarshalerJSONObject) ([]byte, error) {
enc.grow(512)
enc.writeByte('{')
if !v.IsNil() {
v.MarshalJSONObject(enc)
}
if enc.hasKeys {
enc.hasKeys = false
enc.keys = nil
}
enc.writeByte('}')
return enc.buf, enc.err
}
// AddObject adds an object to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject
func (enc *Encoder) AddObject(v MarshalerJSONObject) {
enc.Object(v)
}
// AddObjectOmitEmpty adds an object to be encoded or skips it if IsNil returns true.
// Must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject
func (enc *Encoder) AddObjectOmitEmpty(v MarshalerJSONObject) {
enc.ObjectOmitEmpty(v)
}
// AddObjectNullEmpty adds an object to be encoded or skips it if IsNil returns true.
// Must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject
func (enc *Encoder) AddObjectNullEmpty(v MarshalerJSONObject) {
enc.ObjectNullEmpty(v)
}
// AddObjectKey adds a struct to be encoded, must be used inside an object as it will encode a key
// value must implement MarshalerJSONObject
func (enc *Encoder) AddObjectKey(key string, v MarshalerJSONObject) {
enc.ObjectKey(key, v)
}
// AddObjectKeyOmitEmpty adds an object to be encoded or skips it if IsNil returns true.
// Must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject
func (enc *Encoder) AddObjectKeyOmitEmpty(key string, v MarshalerJSONObject) {
enc.ObjectKeyOmitEmpty(key, v)
}
// AddObjectKeyNullEmpty adds an object to be encoded or skips it if IsNil returns true.
// Must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject
func (enc *Encoder) AddObjectKeyNullEmpty(key string, v MarshalerJSONObject) {
enc.ObjectKeyNullEmpty(key, v)
}
// Object adds an object to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject
func (enc *Encoder) Object(v MarshalerJSONObject) {
if v.IsNil() {
enc.grow(2)
r := enc.getPreviousRune()
if r != '{' && r != '[' {
enc.writeByte(',')
}
enc.writeByte('{')
enc.writeByte('}')
return
}
enc.grow(4)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeByte('{')
var origHasKeys = enc.hasKeys
var origKeys = enc.keys
enc.hasKeys = false
enc.keys = nil
v.MarshalJSONObject(enc)
enc.hasKeys = origHasKeys
enc.keys = origKeys
enc.writeByte('}')
}
// ObjectWithKeys adds an object to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject. It will only encode the keys in keys.
func (enc *Encoder) ObjectWithKeys(v MarshalerJSONObject, keys []string) {
if v.IsNil() {
enc.grow(2)
r := enc.getPreviousRune()
if r != '{' && r != '[' {
enc.writeByte(',')
}
enc.writeByte('{')
enc.writeByte('}')
return
}
enc.grow(4)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeByte('{')
var origKeys = enc.keys
var origHasKeys = enc.hasKeys
enc.hasKeys = true
enc.keys = keys
v.MarshalJSONObject(enc)
enc.hasKeys = origHasKeys
enc.keys = origKeys
enc.writeByte('}')
}
// ObjectOmitEmpty adds an object to be encoded or skips it if IsNil returns true.
// Must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject
func (enc *Encoder) ObjectOmitEmpty(v MarshalerJSONObject) {
if v.IsNil() {
return
}
enc.grow(2)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeByte('{')
var origHasKeys = enc.hasKeys
var origKeys = enc.keys
enc.hasKeys = false
enc.keys = nil
v.MarshalJSONObject(enc)
enc.hasKeys = origHasKeys
enc.keys = origKeys
enc.writeByte('}')
}
// ObjectNullEmpty adds an object to be encoded or skips it if IsNil returns true.
// Must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject
func (enc *Encoder) ObjectNullEmpty(v MarshalerJSONObject) {
enc.grow(2)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
if v.IsNil() {
enc.writeBytes(nullBytes)
return
}
enc.writeByte('{')
var origHasKeys = enc.hasKeys
var origKeys = enc.keys
enc.hasKeys = false
enc.keys = nil
v.MarshalJSONObject(enc)
enc.hasKeys = origHasKeys
enc.keys = origKeys
enc.writeByte('}')
}
// ObjectKey adds a struct to be encoded, must be used inside an object as it will encode a key
// value must implement MarshalerJSONObject
func (enc *Encoder) ObjectKey(key string, v MarshalerJSONObject) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v.IsNil() {
enc.grow(2 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyObj)
enc.writeByte('}')
return
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyObj)
var origHasKeys = enc.hasKeys
var origKeys = enc.keys
enc.hasKeys = false
enc.keys = nil
v.MarshalJSONObject(enc)
enc.hasKeys = origHasKeys
enc.keys = origKeys
enc.writeByte('}')
}
// ObjectKeyWithKeys adds a struct to be encoded, must be used inside an object as it will encode a key.
// Value must implement MarshalerJSONObject. It will only encode the keys in keys.
func (enc *Encoder) ObjectKeyWithKeys(key string, value MarshalerJSONObject, keys []string) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if value.IsNil() {
enc.grow(2 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyObj)
enc.writeByte('}')
return
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyObj)
var origKeys = enc.keys
var origHasKeys = enc.hasKeys
enc.hasKeys = true
enc.keys = keys
value.MarshalJSONObject(enc)
enc.hasKeys = origHasKeys
enc.keys = origKeys
enc.writeByte('}')
}
// ObjectKeyOmitEmpty adds an object to be encoded or skips it if IsNil returns true.
// Must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject
func (enc *Encoder) ObjectKeyOmitEmpty(key string, v MarshalerJSONObject) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v.IsNil() {
return
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyObj)
var origHasKeys = enc.hasKeys
var origKeys = enc.keys
enc.hasKeys = false
enc.keys = nil
v.MarshalJSONObject(enc)
enc.hasKeys = origHasKeys
enc.keys = origKeys
enc.writeByte('}')
}
// ObjectKeyNullEmpty adds an object to be encoded or skips it if IsNil returns true.
// Must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject
func (enc *Encoder) ObjectKeyNullEmpty(key string, v MarshalerJSONObject) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
if v.IsNil() {
enc.writeBytes(nullBytes)
return
}
enc.writeByte('{')
var origHasKeys = enc.hasKeys
var origKeys = enc.keys
enc.hasKeys = false
enc.keys = nil
v.MarshalJSONObject(enc)
enc.hasKeys = origHasKeys
enc.keys = origKeys
enc.writeByte('}')
}
// EncodeObjectFunc is a custom func type implementing MarshaleObject.
// Use it to cast a func(*Encoder) to Marshal an object.
//
// enc := gojay.NewEncoder(io.Writer)
// enc.EncodeObject(gojay.EncodeObjectFunc(func(enc *gojay.Encoder) {
// enc.AddStringKey("hello", "world")
// }))
type EncodeObjectFunc func(*Encoder)
// MarshalJSONObject implements MarshalerJSONObject.
func (f EncodeObjectFunc) MarshalJSONObject(enc *Encoder) {
f(enc)
}
// IsNil implements MarshalerJSONObject.
func (f EncodeObjectFunc) IsNil() bool {
return f == nil
}
func (enc *Encoder) keyExists(k string) bool {
if enc.keys == nil {
return false
}
for _, key := range enc.keys {
if key == k {
return true
}
}
return false
}
+50
View File
@@ -0,0 +1,50 @@
package gojay
import (
"io"
"sync"
)
var encPool = sync.Pool{
New: func() interface{} {
return NewEncoder(nil)
},
}
var streamEncPool = sync.Pool{
New: func() interface{} {
return Stream.NewEncoder(nil)
},
}
func init() {
for i := 0; i < 32; i++ {
encPool.Put(NewEncoder(nil))
}
for i := 0; i < 32; i++ {
streamEncPool.Put(Stream.NewEncoder(nil))
}
}
// NewEncoder returns a new encoder or borrows one from the pool
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w: w}
}
// BorrowEncoder borrows an Encoder from the pool.
func BorrowEncoder(w io.Writer) *Encoder {
enc := encPool.Get().(*Encoder)
enc.w = w
enc.buf = enc.buf[:0]
enc.isPooled = 0
enc.err = nil
enc.hasKeys = false
enc.keys = nil
return enc
}
// Release sends back a Encoder to the pool.
func (enc *Encoder) Release() {
enc.isPooled = 1
encPool.Put(enc)
}
+113
View File
@@ -0,0 +1,113 @@
package gojay
// AddSliceString marshals the given []string s
func (enc *Encoder) AddSliceString(s []string) {
enc.SliceString(s)
}
// SliceString marshals the given []string s
func (enc *Encoder) SliceString(s []string) {
enc.Array(EncodeArrayFunc(func(enc *Encoder) {
for _, str := range s {
enc.String(str)
}
}))
}
// AddSliceStringKey marshals the given []string s
func (enc *Encoder) AddSliceStringKey(k string, s []string) {
enc.SliceStringKey(k, s)
}
// SliceStringKey marshals the given []string s
func (enc *Encoder) SliceStringKey(k string, s []string) {
enc.ArrayKey(k, EncodeArrayFunc(func(enc *Encoder) {
for _, str := range s {
enc.String(str)
}
}))
}
// AddSliceInt marshals the given []int s
func (enc *Encoder) AddSliceInt(s []int) {
enc.SliceInt(s)
}
// SliceInt marshals the given []int s
func (enc *Encoder) SliceInt(s []int) {
enc.Array(EncodeArrayFunc(func(enc *Encoder) {
for _, i := range s {
enc.Int(i)
}
}))
}
// AddSliceIntKey marshals the given []int s
func (enc *Encoder) AddSliceIntKey(k string, s []int) {
enc.SliceIntKey(k, s)
}
// SliceIntKey marshals the given []int s
func (enc *Encoder) SliceIntKey(k string, s []int) {
enc.ArrayKey(k, EncodeArrayFunc(func(enc *Encoder) {
for _, i := range s {
enc.Int(i)
}
}))
}
// AddSliceFloat64 marshals the given []float64 s
func (enc *Encoder) AddSliceFloat64(s []float64) {
enc.SliceFloat64(s)
}
// SliceFloat64 marshals the given []float64 s
func (enc *Encoder) SliceFloat64(s []float64) {
enc.Array(EncodeArrayFunc(func(enc *Encoder) {
for _, i := range s {
enc.Float64(i)
}
}))
}
// AddSliceFloat64Key marshals the given []float64 s
func (enc *Encoder) AddSliceFloat64Key(k string, s []float64) {
enc.SliceFloat64Key(k, s)
}
// SliceFloat64Key marshals the given []float64 s
func (enc *Encoder) SliceFloat64Key(k string, s []float64) {
enc.ArrayKey(k, EncodeArrayFunc(func(enc *Encoder) {
for _, i := range s {
enc.Float64(i)
}
}))
}
// AddSliceBool marshals the given []bool s
func (enc *Encoder) AddSliceBool(s []bool) {
enc.SliceBool(s)
}
// SliceBool marshals the given []bool s
func (enc *Encoder) SliceBool(s []bool) {
enc.Array(EncodeArrayFunc(func(enc *Encoder) {
for _, i := range s {
enc.Bool(i)
}
}))
}
// AddSliceBoolKey marshals the given []bool s
func (enc *Encoder) AddSliceBoolKey(k string, s []bool) {
enc.SliceBoolKey(k, s)
}
// SliceBoolKey marshals the given []bool s
func (enc *Encoder) SliceBoolKey(k string, s []bool) {
enc.ArrayKey(k, EncodeArrayFunc(func(enc *Encoder) {
for _, i := range s {
enc.Bool(i)
}
}))
}
+377
View File
@@ -0,0 +1,377 @@
package gojay
import "database/sql"
// EncodeSQLNullString encodes a string to
func (enc *Encoder) EncodeSQLNullString(v *sql.NullString) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeString(v.String)
_, err := enc.Write()
if err != nil {
enc.err = err
return err
}
return nil
}
// AddSQLNullString adds a string to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddSQLNullString(v *sql.NullString) {
enc.String(v.String)
}
// AddSQLNullStringOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddSQLNullStringOmitEmpty(v *sql.NullString) {
if v != nil && v.Valid && v.String != "" {
enc.StringOmitEmpty(v.String)
}
}
// AddSQLNullStringNullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddSQLNullStringNullEmpty(v *sql.NullString) {
if v != nil && v.Valid {
enc.StringNullEmpty(v.String)
}
}
// AddSQLNullStringKey adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddSQLNullStringKey(key string, v *sql.NullString) {
enc.StringKey(key, v.String)
}
// AddSQLNullStringKeyOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddSQLNullStringKeyOmitEmpty(key string, v *sql.NullString) {
if v != nil && v.Valid && v.String != "" {
enc.StringKeyOmitEmpty(key, v.String)
}
}
// SQLNullString adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullString(v *sql.NullString) {
enc.String(v.String)
}
// SQLNullStringOmitEmpty adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullStringOmitEmpty(v *sql.NullString) {
if v != nil && v.Valid && v.String != "" {
enc.String(v.String)
}
}
// SQLNullStringNullEmpty adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullStringNullEmpty(v *sql.NullString) {
if v != nil && v.Valid {
enc.StringNullEmpty(v.String)
}
}
// SQLNullStringKey adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullStringKey(key string, v *sql.NullString) {
enc.StringKey(key, v.String)
}
// SQLNullStringKeyOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullStringKeyOmitEmpty(key string, v *sql.NullString) {
if v != nil && v.Valid && v.String != "" {
enc.StringKeyOmitEmpty(key, v.String)
}
}
// SQLNullStringKeyNullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullStringKeyNullEmpty(key string, v *sql.NullString) {
if v != nil && v.Valid {
enc.StringKeyNullEmpty(key, v.String)
}
}
// NullInt64
// EncodeSQLNullInt64 encodes a string to
func (enc *Encoder) EncodeSQLNullInt64(v *sql.NullInt64) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeInt64(v.Int64)
_, err := enc.Write()
if err != nil {
enc.err = err
return err
}
return nil
}
// AddSQLNullInt64 adds a string to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddSQLNullInt64(v *sql.NullInt64) {
enc.Int64(v.Int64)
}
// AddSQLNullInt64OmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddSQLNullInt64OmitEmpty(v *sql.NullInt64) {
if v != nil && v.Valid && v.Int64 != 0 {
enc.Int64OmitEmpty(v.Int64)
}
}
// AddSQLNullInt64NullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddSQLNullInt64NullEmpty(v *sql.NullInt64) {
if v != nil && v.Valid {
enc.Int64NullEmpty(v.Int64)
}
}
// AddSQLNullInt64Key adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddSQLNullInt64Key(key string, v *sql.NullInt64) {
enc.Int64Key(key, v.Int64)
}
// AddSQLNullInt64KeyOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddSQLNullInt64KeyOmitEmpty(key string, v *sql.NullInt64) {
if v != nil && v.Valid && v.Int64 != 0 {
enc.Int64KeyOmitEmpty(key, v.Int64)
}
}
// AddSQLNullInt64KeyNullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddSQLNullInt64KeyNullEmpty(key string, v *sql.NullInt64) {
if v != nil && v.Valid {
enc.Int64KeyNullEmpty(key, v.Int64)
}
}
// SQLNullInt64 adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullInt64(v *sql.NullInt64) {
enc.Int64(v.Int64)
}
// SQLNullInt64OmitEmpty adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullInt64OmitEmpty(v *sql.NullInt64) {
if v != nil && v.Valid && v.Int64 != 0 {
enc.Int64(v.Int64)
}
}
// SQLNullInt64NullEmpty adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullInt64NullEmpty(v *sql.NullInt64) {
if v != nil && v.Valid {
enc.Int64NullEmpty(v.Int64)
}
}
// SQLNullInt64Key adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullInt64Key(key string, v *sql.NullInt64) {
enc.Int64Key(key, v.Int64)
}
// SQLNullInt64KeyOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullInt64KeyOmitEmpty(key string, v *sql.NullInt64) {
if v != nil && v.Valid && v.Int64 != 0 {
enc.Int64KeyOmitEmpty(key, v.Int64)
}
}
// SQLNullInt64KeyNullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullInt64KeyNullEmpty(key string, v *sql.NullInt64) {
if v != nil && v.Valid {
enc.Int64KeyNullEmpty(key, v.Int64)
}
}
// NullFloat64
// EncodeSQLNullFloat64 encodes a string to
func (enc *Encoder) EncodeSQLNullFloat64(v *sql.NullFloat64) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeFloat(v.Float64)
_, err := enc.Write()
if err != nil {
enc.err = err
return err
}
return nil
}
// AddSQLNullFloat64 adds a string to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddSQLNullFloat64(v *sql.NullFloat64) {
enc.Float64(v.Float64)
}
// AddSQLNullFloat64OmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddSQLNullFloat64OmitEmpty(v *sql.NullFloat64) {
if v != nil && v.Valid && v.Float64 != 0 {
enc.Float64OmitEmpty(v.Float64)
}
}
// AddSQLNullFloat64NullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddSQLNullFloat64NullEmpty(v *sql.NullFloat64) {
if v != nil && v.Valid {
enc.Float64NullEmpty(v.Float64)
}
}
// AddSQLNullFloat64Key adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddSQLNullFloat64Key(key string, v *sql.NullFloat64) {
enc.Float64Key(key, v.Float64)
}
// AddSQLNullFloat64KeyOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddSQLNullFloat64KeyOmitEmpty(key string, v *sql.NullFloat64) {
if v != nil && v.Valid && v.Float64 != 0 {
enc.Float64KeyOmitEmpty(key, v.Float64)
}
}
// AddSQLNullFloat64KeyNullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddSQLNullFloat64KeyNullEmpty(key string, v *sql.NullFloat64) {
if v != nil && v.Valid {
enc.Float64KeyNullEmpty(key, v.Float64)
}
}
// SQLNullFloat64 adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullFloat64(v *sql.NullFloat64) {
enc.Float64(v.Float64)
}
// SQLNullFloat64OmitEmpty adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullFloat64OmitEmpty(v *sql.NullFloat64) {
if v != nil && v.Valid && v.Float64 != 0 {
enc.Float64(v.Float64)
}
}
// SQLNullFloat64NullEmpty adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullFloat64NullEmpty(v *sql.NullFloat64) {
if v != nil && v.Valid {
enc.Float64NullEmpty(v.Float64)
}
}
// SQLNullFloat64Key adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullFloat64Key(key string, v *sql.NullFloat64) {
enc.Float64Key(key, v.Float64)
}
// SQLNullFloat64KeyOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullFloat64KeyOmitEmpty(key string, v *sql.NullFloat64) {
if v != nil && v.Valid && v.Float64 != 0 {
enc.Float64KeyOmitEmpty(key, v.Float64)
}
}
// SQLNullFloat64KeyNullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullFloat64KeyNullEmpty(key string, v *sql.NullFloat64) {
if v != nil && v.Valid {
enc.Float64KeyNullEmpty(key, v.Float64)
}
}
// NullBool
// EncodeSQLNullBool encodes a string to
func (enc *Encoder) EncodeSQLNullBool(v *sql.NullBool) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeBool(v.Bool)
_, err := enc.Write()
if err != nil {
enc.err = err
return err
}
return nil
}
// AddSQLNullBool adds a string to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddSQLNullBool(v *sql.NullBool) {
enc.Bool(v.Bool)
}
// AddSQLNullBoolOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddSQLNullBoolOmitEmpty(v *sql.NullBool) {
if v != nil && v.Valid && v.Bool != false {
enc.BoolOmitEmpty(v.Bool)
}
}
// AddSQLNullBoolKey adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddSQLNullBoolKey(key string, v *sql.NullBool) {
enc.BoolKey(key, v.Bool)
}
// AddSQLNullBoolKeyOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddSQLNullBoolKeyOmitEmpty(key string, v *sql.NullBool) {
if v != nil && v.Valid && v.Bool != false {
enc.BoolKeyOmitEmpty(key, v.Bool)
}
}
// AddSQLNullBoolKeyNullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddSQLNullBoolKeyNullEmpty(key string, v *sql.NullBool) {
if v != nil && v.Valid {
enc.BoolKeyNullEmpty(key, v.Bool)
}
}
// SQLNullBool adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullBool(v *sql.NullBool) {
enc.Bool(v.Bool)
}
// SQLNullBoolOmitEmpty adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullBoolOmitEmpty(v *sql.NullBool) {
if v != nil && v.Valid && v.Bool != false {
enc.Bool(v.Bool)
}
}
// SQLNullBoolNullEmpty adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullBoolNullEmpty(v *sql.NullBool) {
if v != nil && v.Valid {
enc.BoolNullEmpty(v.Bool)
}
}
// SQLNullBoolKey adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullBoolKey(key string, v *sql.NullBool) {
enc.BoolKey(key, v.Bool)
}
// SQLNullBoolKeyOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullBoolKeyOmitEmpty(key string, v *sql.NullBool) {
if v != nil && v.Valid && v.Bool != false {
enc.BoolKeyOmitEmpty(key, v.Bool)
}
}
// SQLNullBoolKeyNullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) SQLNullBoolKeyNullEmpty(key string, v *sql.NullBool) {
if v != nil && v.Valid {
enc.BoolKeyNullEmpty(key, v.Bool)
}
}
+205
View File
@@ -0,0 +1,205 @@
package gojay
import (
"strconv"
"sync"
"time"
)
// MarshalerStream is the interface to implement
// to continuously encode of stream of data.
type MarshalerStream interface {
MarshalStream(enc *StreamEncoder)
}
// A StreamEncoder reads and encodes values to JSON from an input stream.
//
// It implements conext.Context and provide a channel to notify interruption.
type StreamEncoder struct {
mux *sync.RWMutex
*Encoder
nConsumer int
delimiter byte
deadline *time.Time
done chan struct{}
}
// EncodeStream spins up a defined number of non blocking consumers of the MarshalerStream m.
//
// m must implement MarshalerStream. Ideally m is a channel. See example for implementation.
//
// See the documentation for Marshal for details about the conversion of Go value to JSON.
func (s *StreamEncoder) EncodeStream(m MarshalerStream) {
// if a single consumer, just use this encoder
if s.nConsumer == 1 {
go consume(s, s, m)
return
}
// else use this Encoder only for first consumer
// and use new encoders for other consumers
// this is to avoid concurrent writing to same buffer
// resulting in a weird JSON
go consume(s, s, m)
for i := 1; i < s.nConsumer; i++ {
s.mux.RLock()
select {
case <-s.done:
default:
ss := Stream.borrowEncoder(s.w)
ss.mux.Lock()
ss.done = s.done
ss.buf = make([]byte, 0, 512)
ss.delimiter = s.delimiter
go consume(s, ss, m)
ss.mux.Unlock()
}
s.mux.RUnlock()
}
return
}
// LineDelimited sets the delimiter to a new line character.
//
// It will add a new line after each JSON marshaled by the MarshalerStream
func (s *StreamEncoder) LineDelimited() *StreamEncoder {
s.delimiter = '\n'
return s
}
// CommaDelimited sets the delimiter to a comma.
//
// It will add a new line after each JSON marshaled by the MarshalerStream
func (s *StreamEncoder) CommaDelimited() *StreamEncoder {
s.delimiter = ','
return s
}
// NConsumer sets the number of non blocking go routine to consume the stream.
func (s *StreamEncoder) NConsumer(n int) *StreamEncoder {
s.nConsumer = n
return s
}
// Release sends back a Decoder to the pool.
// If a decoder is used after calling Release
// a panic will be raised with an InvalidUsagePooledDecoderError error.
func (s *StreamEncoder) Release() {
s.isPooled = 1
streamEncPool.Put(s)
}
// Done returns a channel that's closed when work is done.
// It implements context.Context
func (s *StreamEncoder) Done() <-chan struct{} {
return s.done
}
// Err returns nil if Done is not yet closed.
// If Done is closed, Err returns a non-nil error explaining why.
// It implements context.Context
func (s *StreamEncoder) Err() error {
return s.err
}
// Deadline returns the time when work done on behalf of this context
// should be canceled. Deadline returns ok==false when no deadline is
// set. Successive calls to Deadline return the same results.
func (s *StreamEncoder) Deadline() (time.Time, bool) {
if s.deadline != nil {
return *s.deadline, true
}
return time.Time{}, false
}
// SetDeadline sets the deadline
func (s *StreamEncoder) SetDeadline(t time.Time) {
s.deadline = &t
}
// Value implements context.Context
func (s *StreamEncoder) Value(key interface{}) interface{} {
return nil
}
// Cancel cancels the consumers of the stream, interrupting the stream encoding.
//
// After calling cancel, Done() will return a closed channel.
func (s *StreamEncoder) Cancel(err error) {
s.mux.Lock()
defer s.mux.Unlock()
select {
case <-s.done:
default:
s.err = err
close(s.done)
}
}
// AddObject adds an object to be encoded.
// value must implement MarshalerJSONObject.
func (s *StreamEncoder) AddObject(v MarshalerJSONObject) {
if v.IsNil() {
return
}
s.Encoder.writeByte('{')
v.MarshalJSONObject(s.Encoder)
s.Encoder.writeByte('}')
s.Encoder.writeByte(s.delimiter)
}
// AddString adds a string to be encoded.
func (s *StreamEncoder) AddString(v string) {
s.Encoder.writeByte('"')
s.Encoder.writeString(v)
s.Encoder.writeByte('"')
s.Encoder.writeByte(s.delimiter)
}
// AddArray adds an implementation of MarshalerJSONArray to be encoded.
func (s *StreamEncoder) AddArray(v MarshalerJSONArray) {
s.Encoder.writeByte('[')
v.MarshalJSONArray(s.Encoder)
s.Encoder.writeByte(']')
s.Encoder.writeByte(s.delimiter)
}
// AddInt adds an int to be encoded.
func (s *StreamEncoder) AddInt(value int) {
s.buf = strconv.AppendInt(s.buf, int64(value), 10)
s.Encoder.writeByte(s.delimiter)
}
// AddFloat64 adds a float64 to be encoded.
func (s *StreamEncoder) AddFloat64(value float64) {
s.buf = strconv.AppendFloat(s.buf, value, 'f', -1, 64)
s.Encoder.writeByte(s.delimiter)
}
// AddFloat adds a float64 to be encoded.
func (s *StreamEncoder) AddFloat(value float64) {
s.AddFloat64(value)
}
// Non exposed
func consume(init *StreamEncoder, s *StreamEncoder, m MarshalerStream) {
defer s.Release()
for {
select {
case <-init.Done():
return
default:
m.MarshalStream(s)
if s.Encoder.err != nil {
init.Cancel(s.Encoder.err)
return
}
i, err := s.Encoder.Write()
if err != nil || i == 0 {
init.Cancel(err)
return
}
}
}
}
+38
View File
@@ -0,0 +1,38 @@
package gojay
import (
"io"
"sync"
)
// NewEncoder returns a new StreamEncoder.
// It takes an io.Writer implementation to output data.
// It initiates the done channel returned by Done().
func (s stream) NewEncoder(w io.Writer) *StreamEncoder {
enc := BorrowEncoder(w)
return &StreamEncoder{Encoder: enc, nConsumer: 1, done: make(chan struct{}, 1), mux: &sync.RWMutex{}}
}
// BorrowEncoder borrows a StreamEncoder from the pool.
// It takes an io.Writer implementation to output data.
// It initiates the done channel returned by Done().
//
// If no StreamEncoder is available in the pool, it returns a fresh one
func (s stream) BorrowEncoder(w io.Writer) *StreamEncoder {
streamEnc := streamEncPool.Get().(*StreamEncoder)
streamEnc.w = w
streamEnc.Encoder.err = nil
streamEnc.done = make(chan struct{}, 1)
streamEnc.Encoder.buf = streamEnc.buf[:0]
streamEnc.nConsumer = 1
streamEnc.isPooled = 0
return streamEnc
}
func (s stream) borrowEncoder(w io.Writer) *StreamEncoder {
streamEnc := streamEncPool.Get().(*StreamEncoder)
streamEnc.isPooled = 0
streamEnc.w = w
streamEnc.Encoder.err = nil
return streamEnc
}
+186
View File
@@ -0,0 +1,186 @@
package gojay
// EncodeString encodes a string to
func (enc *Encoder) EncodeString(s string) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeString(s)
_, err := enc.Write()
if err != nil {
enc.err = err
return err
}
return nil
}
// encodeString encodes a string to
func (enc *Encoder) encodeString(v string) ([]byte, error) {
enc.writeByte('"')
enc.writeStringEscape(v)
enc.writeByte('"')
return enc.buf, nil
}
// AppendString appends a string to the buffer
func (enc *Encoder) AppendString(v string) {
enc.grow(len(v) + 2)
enc.writeByte('"')
enc.writeStringEscape(v)
enc.writeByte('"')
}
// AddString adds a string to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddString(v string) {
enc.String(v)
}
// AddStringOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddStringOmitEmpty(v string) {
enc.StringOmitEmpty(v)
}
// AddStringNullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddStringNullEmpty(v string) {
enc.StringNullEmpty(v)
}
// AddStringKey adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) AddStringKey(key, v string) {
enc.StringKey(key, v)
}
// AddStringKeyOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddStringKeyOmitEmpty(key, v string) {
enc.StringKeyOmitEmpty(key, v)
}
// AddStringKeyNullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) AddStringKeyNullEmpty(key, v string) {
enc.StringKeyNullEmpty(key, v)
}
// String adds a string to be encoded, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) String(v string) {
enc.grow(len(v) + 4)
r := enc.getPreviousRune()
if r != '[' {
enc.writeTwoBytes(',', '"')
} else {
enc.writeByte('"')
}
enc.writeStringEscape(v)
enc.writeByte('"')
}
// StringOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) StringOmitEmpty(v string) {
if v == "" {
return
}
r := enc.getPreviousRune()
if r != '[' {
enc.writeTwoBytes(',', '"')
} else {
enc.writeByte('"')
}
enc.writeStringEscape(v)
enc.writeByte('"')
}
// StringNullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) StringNullEmpty(v string) {
r := enc.getPreviousRune()
if v == "" {
if r != '[' {
enc.writeByte(',')
enc.writeBytes(nullBytes)
} else {
enc.writeBytes(nullBytes)
}
return
}
if r != '[' {
enc.writeTwoBytes(',', '"')
} else {
enc.writeByte('"')
}
enc.writeStringEscape(v)
enc.writeByte('"')
}
// StringKey adds a string to be encoded, must be used inside an object as it will encode a key
func (enc *Encoder) StringKey(key, v string) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(len(key) + len(v) + 5)
r := enc.getPreviousRune()
if r != '{' {
enc.writeTwoBytes(',', '"')
} else {
enc.writeByte('"')
}
enc.writeStringEscape(key)
enc.writeBytes(objKeyStr)
enc.writeStringEscape(v)
enc.writeByte('"')
}
// StringKeyOmitEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) StringKeyOmitEmpty(key, v string) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v == "" {
return
}
enc.grow(len(key) + len(v) + 5)
r := enc.getPreviousRune()
if r != '{' {
enc.writeTwoBytes(',', '"')
} else {
enc.writeByte('"')
}
enc.writeStringEscape(key)
enc.writeBytes(objKeyStr)
enc.writeStringEscape(v)
enc.writeByte('"')
}
// StringKeyNullEmpty adds a string to be encoded or skips it if it is zero value.
// Must be used inside an object as it will encode a key
func (enc *Encoder) StringKeyNullEmpty(key, v string) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(len(key) + len(v) + 5)
r := enc.getPreviousRune()
if r != '{' {
enc.writeTwoBytes(',', '"')
} else {
enc.writeByte('"')
}
enc.writeStringEscape(key)
enc.writeBytes(objKey)
if v == "" {
enc.writeBytes(nullBytes)
return
}
enc.writeByte('"')
enc.writeStringEscape(v)
enc.writeByte('"')
}
+68
View File
@@ -0,0 +1,68 @@
package gojay
import (
"time"
)
// EncodeTime encodes a *time.Time to JSON with the given format
func (enc *Encoder) EncodeTime(t *time.Time, format string) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeTime(t, format)
_, err := enc.Write()
if err != nil {
return err
}
return nil
}
// encodeInt encodes an int to JSON
func (enc *Encoder) encodeTime(t *time.Time, format string) ([]byte, error) {
enc.writeByte('"')
enc.buf = t.AppendFormat(enc.buf, format)
enc.writeByte('"')
return enc.buf, nil
}
// AddTimeKey adds an *time.Time to be encoded with the given format, must be used inside an object as it will encode a key
func (enc *Encoder) AddTimeKey(key string, t *time.Time, format string) {
enc.TimeKey(key, t, format)
}
// TimeKey adds an *time.Time to be encoded with the given format, must be used inside an object as it will encode a key
func (enc *Encoder) TimeKey(key string, t *time.Time, format string) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(10 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeTwoBytes(',', '"')
} else {
enc.writeByte('"')
}
enc.writeStringEscape(key)
enc.writeBytes(objKeyStr)
enc.buf = t.AppendFormat(enc.buf, format)
enc.writeByte('"')
}
// AddTime adds an *time.Time to be encoded with the given format, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) AddTime(t *time.Time, format string) {
enc.Time(t, format)
}
// Time adds an *time.Time to be encoded with the given format, must be used inside a slice or array encoding (does not encode a key)
func (enc *Encoder) Time(t *time.Time, format string) {
enc.grow(10)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.buf = t.AppendFormat(enc.buf, format)
enc.writeByte('"')
}
+88
View File
@@ -0,0 +1,88 @@
package gojay
import (
"errors"
"fmt"
)
const invalidJSONCharErrorMsg = "Invalid JSON, wrong char '%c' found at position %d"
// InvalidJSONError is a type representing an error returned when
// Decoding encounters invalid JSON.
type InvalidJSONError string
func (err InvalidJSONError) Error() string {
return string(err)
}
func (dec *Decoder) raiseInvalidJSONErr(pos int) error {
var c byte
if len(dec.data) > pos {
c = dec.data[pos]
}
dec.err = InvalidJSONError(
fmt.Sprintf(
invalidJSONCharErrorMsg,
c,
pos,
),
)
return dec.err
}
const invalidUnmarshalErrorMsg = "Cannot unmarshal JSON to type '%T'"
// InvalidUnmarshalError is a type representing an error returned when
// Decoding cannot unmarshal JSON to the receiver type for various reasons.
type InvalidUnmarshalError string
func (err InvalidUnmarshalError) Error() string {
return string(err)
}
func (dec *Decoder) makeInvalidUnmarshalErr(v interface{}) error {
return InvalidUnmarshalError(
fmt.Sprintf(
invalidUnmarshalErrorMsg,
v,
),
)
}
const invalidMarshalErrorMsg = "Invalid type %T provided to Marshal"
// InvalidMarshalError is a type representing an error returned when
// Encoding did not find the proper way to encode
type InvalidMarshalError string
func (err InvalidMarshalError) Error() string {
return string(err)
}
// NoReaderError is a type representing an error returned when
// decoding requires a reader and none was given
type NoReaderError string
func (err NoReaderError) Error() string {
return string(err)
}
// InvalidUsagePooledDecoderError is a type representing an error returned
// when decoding is called on a still pooled Decoder
type InvalidUsagePooledDecoderError string
func (err InvalidUsagePooledDecoderError) Error() string {
return string(err)
}
// InvalidUsagePooledEncoderError is a type representing an error returned
// when decoding is called on a still pooled Encoder
type InvalidUsagePooledEncoderError string
func (err InvalidUsagePooledEncoderError) Error() string {
return string(err)
}
// ErrUnmarshalPtrExpected is the error returned when unmarshal expects a pointer value,
// When using `dec.ObjectNull` or `dec.ArrayNull` for example.
var ErrUnmarshalPtrExpected = errors.New("Cannot unmarshal to given value, a pointer is expected")
+10
View File
@@ -0,0 +1,10 @@
// Package gojay implements encoding and decoding of JSON as defined in RFC 7159.
// The mapping between JSON and Go values is described
// in the documentation for the Marshal and Unmarshal functions.
//
// It aims at performance and usability by relying on simple interfaces
// to decode and encode structures, slices, arrays and even channels.
//
// On top of the simple interfaces to implement, gojay provides lots of helpers to decode and encode
// multiple of different types natively such as bit.Int, sql.NullString or time.Time
package gojay
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

+529
View File
@@ -0,0 +1,529 @@
package qlog
import (
"errors"
"fmt"
"net"
"time"
"github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
"github.com/lucas-clemente/quic-go/logging"
"github.com/francoispqt/gojay"
)
func milliseconds(dur time.Duration) float64 { return float64(dur.Nanoseconds()) / 1e6 }
type eventDetails interface {
Category() category
Name() string
gojay.MarshalerJSONObject
}
type event struct {
RelativeTime time.Duration
eventDetails
}
var _ gojay.MarshalerJSONObject = event{}
func (e event) IsNil() bool { return false }
func (e event) MarshalJSONObject(enc *gojay.Encoder) {
enc.Float64Key("time", milliseconds(e.RelativeTime))
enc.StringKey("name", e.Category().String()+":"+e.Name())
enc.ObjectKey("data", e.eventDetails)
}
type versions []versionNumber
func (v versions) IsNil() bool { return false }
func (v versions) MarshalJSONArray(enc *gojay.Encoder) {
for _, e := range v {
enc.AddString(e.String())
}
}
type rawInfo struct {
Length logging.ByteCount // full packet length, including header and AEAD authentication tag
PayloadLength logging.ByteCount // length of the packet payload, excluding AEAD tag
}
func (i rawInfo) IsNil() bool { return false }
func (i rawInfo) MarshalJSONObject(enc *gojay.Encoder) {
enc.Uint64Key("length", uint64(i.Length))
enc.Uint64KeyOmitEmpty("payload_length", uint64(i.PayloadLength))
}
type eventConnectionStarted struct {
SrcAddr *net.UDPAddr
DestAddr *net.UDPAddr
SrcConnectionID protocol.ConnectionID
DestConnectionID protocol.ConnectionID
}
var _ eventDetails = &eventConnectionStarted{}
func (e eventConnectionStarted) Category() category { return categoryTransport }
func (e eventConnectionStarted) Name() string { return "connection_started" }
func (e eventConnectionStarted) IsNil() bool { return false }
func (e eventConnectionStarted) MarshalJSONObject(enc *gojay.Encoder) {
if utils.IsIPv4(e.SrcAddr.IP) {
enc.StringKey("ip_version", "ipv4")
} else {
enc.StringKey("ip_version", "ipv6")
}
enc.StringKey("src_ip", e.SrcAddr.IP.String())
enc.IntKey("src_port", e.SrcAddr.Port)
enc.StringKey("dst_ip", e.DestAddr.IP.String())
enc.IntKey("dst_port", e.DestAddr.Port)
enc.StringKey("src_cid", connectionID(e.SrcConnectionID).String())
enc.StringKey("dst_cid", connectionID(e.DestConnectionID).String())
}
type eventVersionNegotiated struct {
clientVersions, serverVersions []versionNumber
chosenVersion versionNumber
}
func (e eventVersionNegotiated) Category() category { return categoryTransport }
func (e eventVersionNegotiated) Name() string { return "version_information" }
func (e eventVersionNegotiated) IsNil() bool { return false }
func (e eventVersionNegotiated) MarshalJSONObject(enc *gojay.Encoder) {
if len(e.clientVersions) > 0 {
enc.ArrayKey("client_versions", versions(e.clientVersions))
}
if len(e.serverVersions) > 0 {
enc.ArrayKey("server_versions", versions(e.serverVersions))
}
enc.StringKey("chosen_version", e.chosenVersion.String())
}
type eventConnectionClosed struct {
e error
}
func (e eventConnectionClosed) Category() category { return categoryTransport }
func (e eventConnectionClosed) Name() string { return "connection_closed" }
func (e eventConnectionClosed) IsNil() bool { return false }
func (e eventConnectionClosed) MarshalJSONObject(enc *gojay.Encoder) {
var (
statelessResetErr *quic.StatelessResetError
handshakeTimeoutErr *quic.HandshakeTimeoutError
idleTimeoutErr *quic.IdleTimeoutError
applicationErr *quic.ApplicationError
transportErr *quic.TransportError
versionNegotiationErr *quic.VersionNegotiationError
)
switch {
case errors.As(e.e, &statelessResetErr):
enc.StringKey("owner", ownerRemote.String())
enc.StringKey("trigger", "stateless_reset")
enc.StringKey("stateless_reset_token", fmt.Sprintf("%x", statelessResetErr.Token))
case errors.As(e.e, &handshakeTimeoutErr):
enc.StringKey("owner", ownerLocal.String())
enc.StringKey("trigger", "handshake_timeout")
case errors.As(e.e, &idleTimeoutErr):
enc.StringKey("owner", ownerLocal.String())
enc.StringKey("trigger", "idle_timeout")
case errors.As(e.e, &applicationErr):
owner := ownerLocal
if applicationErr.Remote {
owner = ownerRemote
}
enc.StringKey("owner", owner.String())
enc.Uint64Key("application_code", uint64(applicationErr.ErrorCode))
enc.StringKey("reason", applicationErr.ErrorMessage)
case errors.As(e.e, &transportErr):
owner := ownerLocal
if transportErr.Remote {
owner = ownerRemote
}
enc.StringKey("owner", owner.String())
enc.StringKey("connection_code", transportError(transportErr.ErrorCode).String())
enc.StringKey("reason", transportErr.ErrorMessage)
case errors.As(e.e, &versionNegotiationErr):
enc.StringKey("owner", ownerRemote.String())
enc.StringKey("trigger", "version_negotiation")
}
}
type eventPacketSent struct {
Header packetHeader
Length logging.ByteCount
PayloadLength logging.ByteCount
Frames frames
IsCoalesced bool
Trigger string
}
var _ eventDetails = eventPacketSent{}
func (e eventPacketSent) Category() category { return categoryTransport }
func (e eventPacketSent) Name() string { return "packet_sent" }
func (e eventPacketSent) IsNil() bool { return false }
func (e eventPacketSent) MarshalJSONObject(enc *gojay.Encoder) {
enc.ObjectKey("header", e.Header)
enc.ObjectKey("raw", rawInfo{Length: e.Length, PayloadLength: e.PayloadLength})
enc.ArrayKeyOmitEmpty("frames", e.Frames)
enc.BoolKeyOmitEmpty("is_coalesced", e.IsCoalesced)
enc.StringKeyOmitEmpty("trigger", e.Trigger)
}
type eventPacketReceived struct {
Header packetHeader
Length logging.ByteCount
PayloadLength logging.ByteCount
Frames frames
IsCoalesced bool
Trigger string
}
var _ eventDetails = eventPacketReceived{}
func (e eventPacketReceived) Category() category { return categoryTransport }
func (e eventPacketReceived) Name() string { return "packet_received" }
func (e eventPacketReceived) IsNil() bool { return false }
func (e eventPacketReceived) MarshalJSONObject(enc *gojay.Encoder) {
enc.ObjectKey("header", e.Header)
enc.ObjectKey("raw", rawInfo{Length: e.Length, PayloadLength: e.PayloadLength})
enc.ArrayKeyOmitEmpty("frames", e.Frames)
enc.BoolKeyOmitEmpty("is_coalesced", e.IsCoalesced)
enc.StringKeyOmitEmpty("trigger", e.Trigger)
}
type eventRetryReceived struct {
Header packetHeader
}
func (e eventRetryReceived) Category() category { return categoryTransport }
func (e eventRetryReceived) Name() string { return "packet_received" }
func (e eventRetryReceived) IsNil() bool { return false }
func (e eventRetryReceived) MarshalJSONObject(enc *gojay.Encoder) {
enc.ObjectKey("header", e.Header)
}
type eventVersionNegotiationReceived struct {
Header packetHeader
SupportedVersions []versionNumber
}
func (e eventVersionNegotiationReceived) Category() category { return categoryTransport }
func (e eventVersionNegotiationReceived) Name() string { return "packet_received" }
func (e eventVersionNegotiationReceived) IsNil() bool { return false }
func (e eventVersionNegotiationReceived) MarshalJSONObject(enc *gojay.Encoder) {
enc.ObjectKey("header", e.Header)
enc.ArrayKey("supported_versions", versions(e.SupportedVersions))
}
type eventPacketBuffered struct {
PacketType logging.PacketType
}
func (e eventPacketBuffered) Category() category { return categoryTransport }
func (e eventPacketBuffered) Name() string { return "packet_buffered" }
func (e eventPacketBuffered) IsNil() bool { return false }
func (e eventPacketBuffered) MarshalJSONObject(enc *gojay.Encoder) {
//nolint:gosimple
enc.ObjectKey("header", packetHeaderWithType{PacketType: e.PacketType})
enc.StringKey("trigger", "keys_unavailable")
}
type eventPacketDropped struct {
PacketType logging.PacketType
PacketSize protocol.ByteCount
Trigger packetDropReason
}
func (e eventPacketDropped) Category() category { return categoryTransport }
func (e eventPacketDropped) Name() string { return "packet_dropped" }
func (e eventPacketDropped) IsNil() bool { return false }
func (e eventPacketDropped) MarshalJSONObject(enc *gojay.Encoder) {
enc.ObjectKey("header", packetHeaderWithType{PacketType: e.PacketType})
enc.ObjectKey("raw", rawInfo{Length: e.PacketSize})
enc.StringKey("trigger", e.Trigger.String())
}
type metrics struct {
MinRTT time.Duration
SmoothedRTT time.Duration
LatestRTT time.Duration
RTTVariance time.Duration
CongestionWindow protocol.ByteCount
BytesInFlight protocol.ByteCount
PacketsInFlight int
}
type eventMetricsUpdated struct {
Last *metrics
Current *metrics
}
func (e eventMetricsUpdated) Category() category { return categoryRecovery }
func (e eventMetricsUpdated) Name() string { return "metrics_updated" }
func (e eventMetricsUpdated) IsNil() bool { return false }
func (e eventMetricsUpdated) MarshalJSONObject(enc *gojay.Encoder) {
if e.Last == nil || e.Last.MinRTT != e.Current.MinRTT {
enc.FloatKey("min_rtt", milliseconds(e.Current.MinRTT))
}
if e.Last == nil || e.Last.SmoothedRTT != e.Current.SmoothedRTT {
enc.FloatKey("smoothed_rtt", milliseconds(e.Current.SmoothedRTT))
}
if e.Last == nil || e.Last.LatestRTT != e.Current.LatestRTT {
enc.FloatKey("latest_rtt", milliseconds(e.Current.LatestRTT))
}
if e.Last == nil || e.Last.RTTVariance != e.Current.RTTVariance {
enc.FloatKey("rtt_variance", milliseconds(e.Current.RTTVariance))
}
if e.Last == nil || e.Last.CongestionWindow != e.Current.CongestionWindow {
enc.Uint64Key("congestion_window", uint64(e.Current.CongestionWindow))
}
if e.Last == nil || e.Last.BytesInFlight != e.Current.BytesInFlight {
enc.Uint64Key("bytes_in_flight", uint64(e.Current.BytesInFlight))
}
if e.Last == nil || e.Last.PacketsInFlight != e.Current.PacketsInFlight {
enc.Uint64KeyOmitEmpty("packets_in_flight", uint64(e.Current.PacketsInFlight))
}
}
type eventUpdatedPTO struct {
Value uint32
}
func (e eventUpdatedPTO) Category() category { return categoryRecovery }
func (e eventUpdatedPTO) Name() string { return "metrics_updated" }
func (e eventUpdatedPTO) IsNil() bool { return false }
func (e eventUpdatedPTO) MarshalJSONObject(enc *gojay.Encoder) {
enc.Uint32Key("pto_count", e.Value)
}
type eventPacketLost struct {
PacketType logging.PacketType
PacketNumber protocol.PacketNumber
Trigger packetLossReason
}
func (e eventPacketLost) Category() category { return categoryRecovery }
func (e eventPacketLost) Name() string { return "packet_lost" }
func (e eventPacketLost) IsNil() bool { return false }
func (e eventPacketLost) MarshalJSONObject(enc *gojay.Encoder) {
enc.ObjectKey("header", packetHeaderWithTypeAndPacketNumber{
PacketType: e.PacketType,
PacketNumber: e.PacketNumber,
})
enc.StringKey("trigger", e.Trigger.String())
}
type eventKeyUpdated struct {
Trigger keyUpdateTrigger
KeyType keyType
Generation protocol.KeyPhase
// we don't log the keys here, so we don't need `old` and `new`.
}
func (e eventKeyUpdated) Category() category { return categorySecurity }
func (e eventKeyUpdated) Name() string { return "key_updated" }
func (e eventKeyUpdated) IsNil() bool { return false }
func (e eventKeyUpdated) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("trigger", e.Trigger.String())
enc.StringKey("key_type", e.KeyType.String())
if e.KeyType == keyTypeClient1RTT || e.KeyType == keyTypeServer1RTT {
enc.Uint64Key("generation", uint64(e.Generation))
}
}
type eventKeyRetired struct {
KeyType keyType
Generation protocol.KeyPhase
}
func (e eventKeyRetired) Category() category { return categorySecurity }
func (e eventKeyRetired) Name() string { return "key_retired" }
func (e eventKeyRetired) IsNil() bool { return false }
func (e eventKeyRetired) MarshalJSONObject(enc *gojay.Encoder) {
if e.KeyType != keyTypeClient1RTT && e.KeyType != keyTypeServer1RTT {
enc.StringKey("trigger", "tls")
}
enc.StringKey("key_type", e.KeyType.String())
if e.KeyType == keyTypeClient1RTT || e.KeyType == keyTypeServer1RTT {
enc.Uint64Key("generation", uint64(e.Generation))
}
}
type eventTransportParameters struct {
Restore bool
Owner owner
SentBy protocol.Perspective
OriginalDestinationConnectionID protocol.ConnectionID
InitialSourceConnectionID protocol.ConnectionID
RetrySourceConnectionID *protocol.ConnectionID
StatelessResetToken *protocol.StatelessResetToken
DisableActiveMigration bool
MaxIdleTimeout time.Duration
MaxUDPPayloadSize protocol.ByteCount
AckDelayExponent uint8
MaxAckDelay time.Duration
ActiveConnectionIDLimit uint64
InitialMaxData protocol.ByteCount
InitialMaxStreamDataBidiLocal protocol.ByteCount
InitialMaxStreamDataBidiRemote protocol.ByteCount
InitialMaxStreamDataUni protocol.ByteCount
InitialMaxStreamsBidi int64
InitialMaxStreamsUni int64
PreferredAddress *preferredAddress
MaxDatagramFrameSize protocol.ByteCount
}
func (e eventTransportParameters) Category() category { return categoryTransport }
func (e eventTransportParameters) Name() string {
if e.Restore {
return "parameters_restored"
}
return "parameters_set"
}
func (e eventTransportParameters) IsNil() bool { return false }
func (e eventTransportParameters) MarshalJSONObject(enc *gojay.Encoder) {
if !e.Restore {
enc.StringKey("owner", e.Owner.String())
if e.SentBy == protocol.PerspectiveServer {
enc.StringKey("original_destination_connection_id", connectionID(e.OriginalDestinationConnectionID).String())
if e.StatelessResetToken != nil {
enc.StringKey("stateless_reset_token", fmt.Sprintf("%x", e.StatelessResetToken[:]))
}
if e.RetrySourceConnectionID != nil {
enc.StringKey("retry_source_connection_id", connectionID(*e.RetrySourceConnectionID).String())
}
}
enc.StringKey("initial_source_connection_id", connectionID(e.InitialSourceConnectionID).String())
}
enc.BoolKey("disable_active_migration", e.DisableActiveMigration)
enc.FloatKeyOmitEmpty("max_idle_timeout", milliseconds(e.MaxIdleTimeout))
enc.Int64KeyNullEmpty("max_udp_payload_size", int64(e.MaxUDPPayloadSize))
enc.Uint8KeyOmitEmpty("ack_delay_exponent", e.AckDelayExponent)
enc.FloatKeyOmitEmpty("max_ack_delay", milliseconds(e.MaxAckDelay))
enc.Uint64KeyOmitEmpty("active_connection_id_limit", e.ActiveConnectionIDLimit)
enc.Int64KeyOmitEmpty("initial_max_data", int64(e.InitialMaxData))
enc.Int64KeyOmitEmpty("initial_max_stream_data_bidi_local", int64(e.InitialMaxStreamDataBidiLocal))
enc.Int64KeyOmitEmpty("initial_max_stream_data_bidi_remote", int64(e.InitialMaxStreamDataBidiRemote))
enc.Int64KeyOmitEmpty("initial_max_stream_data_uni", int64(e.InitialMaxStreamDataUni))
enc.Int64KeyOmitEmpty("initial_max_streams_bidi", e.InitialMaxStreamsBidi)
enc.Int64KeyOmitEmpty("initial_max_streams_uni", e.InitialMaxStreamsUni)
if e.PreferredAddress != nil {
enc.ObjectKey("preferred_address", e.PreferredAddress)
}
if e.MaxDatagramFrameSize != protocol.InvalidByteCount {
enc.Int64Key("max_datagram_frame_size", int64(e.MaxDatagramFrameSize))
}
}
type preferredAddress struct {
IPv4, IPv6 net.IP
PortV4, PortV6 uint16
ConnectionID protocol.ConnectionID
StatelessResetToken protocol.StatelessResetToken
}
var _ gojay.MarshalerJSONObject = &preferredAddress{}
func (a preferredAddress) IsNil() bool { return false }
func (a preferredAddress) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("ip_v4", a.IPv4.String())
enc.Uint16Key("port_v4", a.PortV4)
enc.StringKey("ip_v6", a.IPv6.String())
enc.Uint16Key("port_v6", a.PortV6)
enc.StringKey("connection_id", connectionID(a.ConnectionID).String())
enc.StringKey("stateless_reset_token", fmt.Sprintf("%x", a.StatelessResetToken))
}
type eventLossTimerSet struct {
TimerType timerType
EncLevel protocol.EncryptionLevel
Delta time.Duration
}
func (e eventLossTimerSet) Category() category { return categoryRecovery }
func (e eventLossTimerSet) Name() string { return "loss_timer_updated" }
func (e eventLossTimerSet) IsNil() bool { return false }
func (e eventLossTimerSet) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("event_type", "set")
enc.StringKey("timer_type", e.TimerType.String())
enc.StringKey("packet_number_space", encLevelToPacketNumberSpace(e.EncLevel))
enc.Float64Key("delta", milliseconds(e.Delta))
}
type eventLossTimerExpired struct {
TimerType timerType
EncLevel protocol.EncryptionLevel
}
func (e eventLossTimerExpired) Category() category { return categoryRecovery }
func (e eventLossTimerExpired) Name() string { return "loss_timer_updated" }
func (e eventLossTimerExpired) IsNil() bool { return false }
func (e eventLossTimerExpired) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("event_type", "expired")
enc.StringKey("timer_type", e.TimerType.String())
enc.StringKey("packet_number_space", encLevelToPacketNumberSpace(e.EncLevel))
}
type eventLossTimerCanceled struct{}
func (e eventLossTimerCanceled) Category() category { return categoryRecovery }
func (e eventLossTimerCanceled) Name() string { return "loss_timer_updated" }
func (e eventLossTimerCanceled) IsNil() bool { return false }
func (e eventLossTimerCanceled) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("event_type", "cancelled")
}
type eventCongestionStateUpdated struct {
state congestionState
}
func (e eventCongestionStateUpdated) Category() category { return categoryRecovery }
func (e eventCongestionStateUpdated) Name() string { return "congestion_state_updated" }
func (e eventCongestionStateUpdated) IsNil() bool { return false }
func (e eventCongestionStateUpdated) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("new", e.state.String())
}
type eventGeneric struct {
name string
msg string
}
func (e eventGeneric) Category() category { return categoryTransport }
func (e eventGeneric) Name() string { return e.name }
func (e eventGeneric) IsNil() bool { return false }
func (e eventGeneric) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("details", e.msg)
}
+227
View File
@@ -0,0 +1,227 @@
package qlog
import (
"fmt"
"github.com/lucas-clemente/quic-go/internal/wire"
"github.com/lucas-clemente/quic-go/logging"
"github.com/francoispqt/gojay"
)
type frame struct {
Frame logging.Frame
}
var _ gojay.MarshalerJSONObject = frame{}
var _ gojay.MarshalerJSONArray = frames{}
func (f frame) MarshalJSONObject(enc *gojay.Encoder) {
switch frame := f.Frame.(type) {
case *logging.PingFrame:
marshalPingFrame(enc, frame)
case *logging.AckFrame:
marshalAckFrame(enc, frame)
case *logging.ResetStreamFrame:
marshalResetStreamFrame(enc, frame)
case *logging.StopSendingFrame:
marshalStopSendingFrame(enc, frame)
case *logging.CryptoFrame:
marshalCryptoFrame(enc, frame)
case *logging.NewTokenFrame:
marshalNewTokenFrame(enc, frame)
case *logging.StreamFrame:
marshalStreamFrame(enc, frame)
case *logging.MaxDataFrame:
marshalMaxDataFrame(enc, frame)
case *logging.MaxStreamDataFrame:
marshalMaxStreamDataFrame(enc, frame)
case *logging.MaxStreamsFrame:
marshalMaxStreamsFrame(enc, frame)
case *logging.DataBlockedFrame:
marshalDataBlockedFrame(enc, frame)
case *logging.StreamDataBlockedFrame:
marshalStreamDataBlockedFrame(enc, frame)
case *logging.StreamsBlockedFrame:
marshalStreamsBlockedFrame(enc, frame)
case *logging.NewConnectionIDFrame:
marshalNewConnectionIDFrame(enc, frame)
case *logging.RetireConnectionIDFrame:
marshalRetireConnectionIDFrame(enc, frame)
case *logging.PathChallengeFrame:
marshalPathChallengeFrame(enc, frame)
case *logging.PathResponseFrame:
marshalPathResponseFrame(enc, frame)
case *logging.ConnectionCloseFrame:
marshalConnectionCloseFrame(enc, frame)
case *logging.HandshakeDoneFrame:
marshalHandshakeDoneFrame(enc, frame)
case *logging.DatagramFrame:
marshalDatagramFrame(enc, frame)
default:
panic("unknown frame type")
}
}
func (f frame) IsNil() bool { return false }
type frames []frame
func (fs frames) IsNil() bool { return fs == nil }
func (fs frames) MarshalJSONArray(enc *gojay.Encoder) {
for _, f := range fs {
enc.Object(f)
}
}
func marshalPingFrame(enc *gojay.Encoder, _ *wire.PingFrame) {
enc.StringKey("frame_type", "ping")
}
type ackRanges []wire.AckRange
func (ars ackRanges) MarshalJSONArray(enc *gojay.Encoder) {
for _, r := range ars {
enc.Array(ackRange(r))
}
}
func (ars ackRanges) IsNil() bool { return false }
type ackRange wire.AckRange
func (ar ackRange) MarshalJSONArray(enc *gojay.Encoder) {
enc.AddInt64(int64(ar.Smallest))
if ar.Smallest != ar.Largest {
enc.AddInt64(int64(ar.Largest))
}
}
func (ar ackRange) IsNil() bool { return false }
func marshalAckFrame(enc *gojay.Encoder, f *logging.AckFrame) {
enc.StringKey("frame_type", "ack")
enc.FloatKeyOmitEmpty("ack_delay", milliseconds(f.DelayTime))
enc.ArrayKey("acked_ranges", ackRanges(f.AckRanges))
if hasECN := f.ECT0 > 0 || f.ECT1 > 0 || f.ECNCE > 0; hasECN {
enc.Uint64Key("ect0", f.ECT0)
enc.Uint64Key("ect1", f.ECT1)
enc.Uint64Key("ce", f.ECNCE)
}
}
func marshalResetStreamFrame(enc *gojay.Encoder, f *logging.ResetStreamFrame) {
enc.StringKey("frame_type", "reset_stream")
enc.Int64Key("stream_id", int64(f.StreamID))
enc.Int64Key("error_code", int64(f.ErrorCode))
enc.Int64Key("final_size", int64(f.FinalSize))
}
func marshalStopSendingFrame(enc *gojay.Encoder, f *logging.StopSendingFrame) {
enc.StringKey("frame_type", "stop_sending")
enc.Int64Key("stream_id", int64(f.StreamID))
enc.Int64Key("error_code", int64(f.ErrorCode))
}
func marshalCryptoFrame(enc *gojay.Encoder, f *logging.CryptoFrame) {
enc.StringKey("frame_type", "crypto")
enc.Int64Key("offset", int64(f.Offset))
enc.Int64Key("length", int64(f.Length))
}
func marshalNewTokenFrame(enc *gojay.Encoder, f *logging.NewTokenFrame) {
enc.StringKey("frame_type", "new_token")
enc.ObjectKey("token", &token{Raw: f.Token})
}
func marshalStreamFrame(enc *gojay.Encoder, f *logging.StreamFrame) {
enc.StringKey("frame_type", "stream")
enc.Int64Key("stream_id", int64(f.StreamID))
enc.Int64Key("offset", int64(f.Offset))
enc.IntKey("length", int(f.Length))
enc.BoolKeyOmitEmpty("fin", f.Fin)
}
func marshalMaxDataFrame(enc *gojay.Encoder, f *logging.MaxDataFrame) {
enc.StringKey("frame_type", "max_data")
enc.Int64Key("maximum", int64(f.MaximumData))
}
func marshalMaxStreamDataFrame(enc *gojay.Encoder, f *logging.MaxStreamDataFrame) {
enc.StringKey("frame_type", "max_stream_data")
enc.Int64Key("stream_id", int64(f.StreamID))
enc.Int64Key("maximum", int64(f.MaximumStreamData))
}
func marshalMaxStreamsFrame(enc *gojay.Encoder, f *logging.MaxStreamsFrame) {
enc.StringKey("frame_type", "max_streams")
enc.StringKey("stream_type", streamType(f.Type).String())
enc.Int64Key("maximum", int64(f.MaxStreamNum))
}
func marshalDataBlockedFrame(enc *gojay.Encoder, f *logging.DataBlockedFrame) {
enc.StringKey("frame_type", "data_blocked")
enc.Int64Key("limit", int64(f.MaximumData))
}
func marshalStreamDataBlockedFrame(enc *gojay.Encoder, f *logging.StreamDataBlockedFrame) {
enc.StringKey("frame_type", "stream_data_blocked")
enc.Int64Key("stream_id", int64(f.StreamID))
enc.Int64Key("limit", int64(f.MaximumStreamData))
}
func marshalStreamsBlockedFrame(enc *gojay.Encoder, f *logging.StreamsBlockedFrame) {
enc.StringKey("frame_type", "streams_blocked")
enc.StringKey("stream_type", streamType(f.Type).String())
enc.Int64Key("limit", int64(f.StreamLimit))
}
func marshalNewConnectionIDFrame(enc *gojay.Encoder, f *logging.NewConnectionIDFrame) {
enc.StringKey("frame_type", "new_connection_id")
enc.Int64Key("sequence_number", int64(f.SequenceNumber))
enc.Int64Key("retire_prior_to", int64(f.RetirePriorTo))
enc.IntKey("length", f.ConnectionID.Len())
enc.StringKey("connection_id", connectionID(f.ConnectionID).String())
enc.StringKey("stateless_reset_token", fmt.Sprintf("%x", f.StatelessResetToken))
}
func marshalRetireConnectionIDFrame(enc *gojay.Encoder, f *logging.RetireConnectionIDFrame) {
enc.StringKey("frame_type", "retire_connection_id")
enc.Int64Key("sequence_number", int64(f.SequenceNumber))
}
func marshalPathChallengeFrame(enc *gojay.Encoder, f *logging.PathChallengeFrame) {
enc.StringKey("frame_type", "path_challenge")
enc.StringKey("data", fmt.Sprintf("%x", f.Data[:]))
}
func marshalPathResponseFrame(enc *gojay.Encoder, f *logging.PathResponseFrame) {
enc.StringKey("frame_type", "path_response")
enc.StringKey("data", fmt.Sprintf("%x", f.Data[:]))
}
func marshalConnectionCloseFrame(enc *gojay.Encoder, f *logging.ConnectionCloseFrame) {
errorSpace := "transport"
if f.IsApplicationError {
errorSpace = "application"
}
enc.StringKey("frame_type", "connection_close")
enc.StringKey("error_space", errorSpace)
if errName := transportError(f.ErrorCode).String(); len(errName) > 0 {
enc.StringKey("error_code", errName)
} else {
enc.Uint64Key("error_code", f.ErrorCode)
}
enc.Uint64Key("raw_error_code", f.ErrorCode)
enc.StringKey("reason", f.ReasonPhrase)
}
func marshalHandshakeDoneFrame(enc *gojay.Encoder, _ *logging.HandshakeDoneFrame) {
enc.StringKey("frame_type", "handshake_done")
}
func marshalDatagramFrame(enc *gojay.Encoder, f *logging.DatagramFrame) {
enc.StringKey("frame_type", "datagram")
enc.Int64Key("length", int64(f.Length))
}
+119
View File
@@ -0,0 +1,119 @@
package qlog
import (
"fmt"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/wire"
"github.com/lucas-clemente/quic-go/logging"
"github.com/francoispqt/gojay"
)
func getPacketTypeFromEncryptionLevel(encLevel protocol.EncryptionLevel) logging.PacketType {
switch encLevel {
case protocol.EncryptionInitial:
return logging.PacketTypeInitial
case protocol.EncryptionHandshake:
return logging.PacketTypeHandshake
case protocol.Encryption0RTT:
return logging.PacketType0RTT
case protocol.Encryption1RTT:
return logging.PacketType1RTT
default:
panic("unknown encryption level")
}
}
type token struct {
Raw []byte
}
var _ gojay.MarshalerJSONObject = &token{}
func (t token) IsNil() bool { return false }
func (t token) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("data", fmt.Sprintf("%x", t.Raw))
}
// PacketHeader is a QUIC packet header.
type packetHeader struct {
PacketType logging.PacketType
KeyPhaseBit logging.KeyPhaseBit
PacketNumber logging.PacketNumber
Version logging.VersionNumber
SrcConnectionID logging.ConnectionID
DestConnectionID logging.ConnectionID
Token *token
}
func transformHeader(hdr *wire.Header) *packetHeader {
h := &packetHeader{
PacketType: logging.PacketTypeFromHeader(hdr),
SrcConnectionID: hdr.SrcConnectionID,
DestConnectionID: hdr.DestConnectionID,
Version: hdr.Version,
}
if len(hdr.Token) > 0 {
h.Token = &token{Raw: hdr.Token}
}
return h
}
func transformExtendedHeader(hdr *wire.ExtendedHeader) *packetHeader {
h := transformHeader(&hdr.Header)
h.PacketNumber = hdr.PacketNumber
h.KeyPhaseBit = hdr.KeyPhase
return h
}
func (h packetHeader) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("packet_type", packetType(h.PacketType).String())
if h.PacketType != logging.PacketTypeRetry && h.PacketType != logging.PacketTypeVersionNegotiation {
enc.Int64Key("packet_number", int64(h.PacketNumber))
}
if h.Version != 0 {
enc.StringKey("version", versionNumber(h.Version).String())
}
if h.PacketType != logging.PacketType1RTT {
enc.IntKey("scil", h.SrcConnectionID.Len())
if h.SrcConnectionID.Len() > 0 {
enc.StringKey("scid", connectionID(h.SrcConnectionID).String())
}
}
enc.IntKey("dcil", h.DestConnectionID.Len())
if h.DestConnectionID.Len() > 0 {
enc.StringKey("dcid", connectionID(h.DestConnectionID).String())
}
if h.KeyPhaseBit == logging.KeyPhaseZero || h.KeyPhaseBit == logging.KeyPhaseOne {
enc.StringKey("key_phase_bit", h.KeyPhaseBit.String())
}
if h.Token != nil {
enc.ObjectKey("token", h.Token)
}
}
// a minimal header that only outputs the packet type
type packetHeaderWithType struct {
PacketType logging.PacketType
}
func (h packetHeaderWithType) IsNil() bool { return false }
func (h packetHeaderWithType) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("packet_type", packetType(h.PacketType).String())
}
// a minimal header that only outputs the packet type
type packetHeaderWithTypeAndPacketNumber struct {
PacketType logging.PacketType
PacketNumber logging.PacketNumber
}
func (h packetHeaderWithTypeAndPacketNumber) IsNil() bool { return false }
func (h packetHeaderWithTypeAndPacketNumber) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("packet_type", packetType(h.PacketType).String())
enc.Int64Key("packet_number", int64(h.PacketNumber))
}
+486
View File
@@ -0,0 +1,486 @@
package qlog
import (
"bytes"
"context"
"fmt"
"io"
"log"
"net"
"runtime/debug"
"sync"
"time"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
"github.com/lucas-clemente/quic-go/internal/wire"
"github.com/lucas-clemente/quic-go/logging"
"github.com/francoispqt/gojay"
)
// Setting of this only works when quic-go is used as a library.
// When building a binary from this repository, the version can be set using the following go build flag:
// -ldflags="-X github.com/lucas-clemente/quic-go/qlog.quicGoVersion=foobar"
var quicGoVersion = "(devel)"
func init() {
if quicGoVersion != "(devel)" { // variable set by ldflags
return
}
info, ok := debug.ReadBuildInfo()
if !ok { // no build info available. This happens when quic-go is not used as a library.
return
}
for _, d := range info.Deps {
if d.Path == "github.com/lucas-clemente/quic-go" {
quicGoVersion = d.Version
if d.Replace != nil {
if len(d.Replace.Version) > 0 {
quicGoVersion = d.Version
} else {
quicGoVersion += " (replaced)"
}
}
break
}
}
}
const eventChanSize = 50
type tracer struct {
getLogWriter func(p logging.Perspective, connectionID []byte) io.WriteCloser
}
var _ logging.Tracer = &tracer{}
// NewTracer creates a new qlog tracer.
func NewTracer(getLogWriter func(p logging.Perspective, connectionID []byte) io.WriteCloser) logging.Tracer {
return &tracer{getLogWriter: getLogWriter}
}
func (t *tracer) TracerForConnection(_ context.Context, p logging.Perspective, odcid protocol.ConnectionID) logging.ConnectionTracer {
if w := t.getLogWriter(p, odcid.Bytes()); w != nil {
return NewConnectionTracer(w, p, odcid)
}
return nil
}
func (t *tracer) SentPacket(net.Addr, *logging.Header, protocol.ByteCount, []logging.Frame) {}
func (t *tracer) DroppedPacket(net.Addr, logging.PacketType, protocol.ByteCount, logging.PacketDropReason) {
}
type connectionTracer struct {
mutex sync.Mutex
w io.WriteCloser
odcid protocol.ConnectionID
perspective protocol.Perspective
referenceTime time.Time
events chan event
encodeErr error
runStopped chan struct{}
lastMetrics *metrics
}
var _ logging.ConnectionTracer = &connectionTracer{}
// NewConnectionTracer creates a new tracer to record a qlog for a connection.
func NewConnectionTracer(w io.WriteCloser, p protocol.Perspective, odcid protocol.ConnectionID) logging.ConnectionTracer {
t := &connectionTracer{
w: w,
perspective: p,
odcid: odcid,
runStopped: make(chan struct{}),
events: make(chan event, eventChanSize),
referenceTime: time.Now(),
}
go t.run()
return t
}
func (t *connectionTracer) run() {
defer close(t.runStopped)
buf := &bytes.Buffer{}
enc := gojay.NewEncoder(buf)
tl := &topLevel{
trace: trace{
VantagePoint: vantagePoint{Type: t.perspective},
CommonFields: commonFields{
ODCID: connectionID(t.odcid),
GroupID: connectionID(t.odcid),
ReferenceTime: t.referenceTime,
},
},
}
if err := enc.Encode(tl); err != nil {
panic(fmt.Sprintf("qlog encoding into a bytes.Buffer failed: %s", err))
}
if err := buf.WriteByte('\n'); err != nil {
panic(fmt.Sprintf("qlog encoding into a bytes.Buffer failed: %s", err))
}
if _, err := t.w.Write(buf.Bytes()); err != nil {
t.encodeErr = err
}
enc = gojay.NewEncoder(t.w)
for ev := range t.events {
if t.encodeErr != nil { // if encoding failed, just continue draining the event channel
continue
}
if err := enc.Encode(ev); err != nil {
t.encodeErr = err
continue
}
if _, err := t.w.Write([]byte{'\n'}); err != nil {
t.encodeErr = err
}
}
}
func (t *connectionTracer) Close() {
if err := t.export(); err != nil {
log.Printf("exporting qlog failed: %s\n", err)
}
}
// export writes a qlog.
func (t *connectionTracer) export() error {
close(t.events)
<-t.runStopped
if t.encodeErr != nil {
return t.encodeErr
}
return t.w.Close()
}
func (t *connectionTracer) recordEvent(eventTime time.Time, details eventDetails) {
t.events <- event{
RelativeTime: eventTime.Sub(t.referenceTime),
eventDetails: details,
}
}
func (t *connectionTracer) StartedConnection(local, remote net.Addr, srcConnID, destConnID protocol.ConnectionID) {
// ignore this event if we're not dealing with UDP addresses here
localAddr, ok := local.(*net.UDPAddr)
if !ok {
return
}
remoteAddr, ok := remote.(*net.UDPAddr)
if !ok {
return
}
t.mutex.Lock()
t.recordEvent(time.Now(), &eventConnectionStarted{
SrcAddr: localAddr,
DestAddr: remoteAddr,
SrcConnectionID: srcConnID,
DestConnectionID: destConnID,
})
t.mutex.Unlock()
}
func (t *connectionTracer) NegotiatedVersion(chosen logging.VersionNumber, client, server []logging.VersionNumber) {
var clientVersions, serverVersions []versionNumber
if len(client) > 0 {
clientVersions = make([]versionNumber, len(client))
for i, v := range client {
clientVersions[i] = versionNumber(v)
}
}
if len(server) > 0 {
serverVersions = make([]versionNumber, len(server))
for i, v := range server {
serverVersions[i] = versionNumber(v)
}
}
t.mutex.Lock()
t.recordEvent(time.Now(), &eventVersionNegotiated{
clientVersions: clientVersions,
serverVersions: serverVersions,
chosenVersion: versionNumber(chosen),
})
t.mutex.Unlock()
}
func (t *connectionTracer) ClosedConnection(e error) {
t.mutex.Lock()
t.recordEvent(time.Now(), &eventConnectionClosed{e: e})
t.mutex.Unlock()
}
func (t *connectionTracer) SentTransportParameters(tp *wire.TransportParameters) {
t.recordTransportParameters(t.perspective, tp)
}
func (t *connectionTracer) ReceivedTransportParameters(tp *wire.TransportParameters) {
t.recordTransportParameters(t.perspective.Opposite(), tp)
}
func (t *connectionTracer) RestoredTransportParameters(tp *wire.TransportParameters) {
ev := t.toTransportParameters(tp)
ev.Restore = true
t.mutex.Lock()
t.recordEvent(time.Now(), ev)
t.mutex.Unlock()
}
func (t *connectionTracer) recordTransportParameters(sentBy protocol.Perspective, tp *wire.TransportParameters) {
ev := t.toTransportParameters(tp)
ev.Owner = ownerLocal
if sentBy != t.perspective {
ev.Owner = ownerRemote
}
ev.SentBy = sentBy
t.mutex.Lock()
t.recordEvent(time.Now(), ev)
t.mutex.Unlock()
}
func (t *connectionTracer) toTransportParameters(tp *wire.TransportParameters) *eventTransportParameters {
var pa *preferredAddress
if tp.PreferredAddress != nil {
pa = &preferredAddress{
IPv4: tp.PreferredAddress.IPv4,
PortV4: tp.PreferredAddress.IPv4Port,
IPv6: tp.PreferredAddress.IPv6,
PortV6: tp.PreferredAddress.IPv6Port,
ConnectionID: tp.PreferredAddress.ConnectionID,
StatelessResetToken: tp.PreferredAddress.StatelessResetToken,
}
}
return &eventTransportParameters{
OriginalDestinationConnectionID: tp.OriginalDestinationConnectionID,
InitialSourceConnectionID: tp.InitialSourceConnectionID,
RetrySourceConnectionID: tp.RetrySourceConnectionID,
StatelessResetToken: tp.StatelessResetToken,
DisableActiveMigration: tp.DisableActiveMigration,
MaxIdleTimeout: tp.MaxIdleTimeout,
MaxUDPPayloadSize: tp.MaxUDPPayloadSize,
AckDelayExponent: tp.AckDelayExponent,
MaxAckDelay: tp.MaxAckDelay,
ActiveConnectionIDLimit: tp.ActiveConnectionIDLimit,
InitialMaxData: tp.InitialMaxData,
InitialMaxStreamDataBidiLocal: tp.InitialMaxStreamDataBidiLocal,
InitialMaxStreamDataBidiRemote: tp.InitialMaxStreamDataBidiRemote,
InitialMaxStreamDataUni: tp.InitialMaxStreamDataUni,
InitialMaxStreamsBidi: int64(tp.MaxBidiStreamNum),
InitialMaxStreamsUni: int64(tp.MaxUniStreamNum),
PreferredAddress: pa,
MaxDatagramFrameSize: tp.MaxDatagramFrameSize,
}
}
func (t *connectionTracer) SentPacket(hdr *wire.ExtendedHeader, packetSize logging.ByteCount, ack *logging.AckFrame, frames []logging.Frame) {
numFrames := len(frames)
if ack != nil {
numFrames++
}
fs := make([]frame, 0, numFrames)
if ack != nil {
fs = append(fs, frame{Frame: ack})
}
for _, f := range frames {
fs = append(fs, frame{Frame: f})
}
header := *transformExtendedHeader(hdr)
t.mutex.Lock()
t.recordEvent(time.Now(), &eventPacketSent{
Header: header,
Length: packetSize,
PayloadLength: hdr.Length,
Frames: fs,
})
t.mutex.Unlock()
}
func (t *connectionTracer) ReceivedPacket(hdr *wire.ExtendedHeader, packetSize logging.ByteCount, frames []logging.Frame) {
fs := make([]frame, len(frames))
for i, f := range frames {
fs[i] = frame{Frame: f}
}
header := *transformExtendedHeader(hdr)
t.mutex.Lock()
t.recordEvent(time.Now(), &eventPacketReceived{
Header: header,
Length: packetSize,
PayloadLength: hdr.Length,
Frames: fs,
})
t.mutex.Unlock()
}
func (t *connectionTracer) ReceivedRetry(hdr *wire.Header) {
t.mutex.Lock()
t.recordEvent(time.Now(), &eventRetryReceived{
Header: *transformHeader(hdr),
})
t.mutex.Unlock()
}
func (t *connectionTracer) ReceivedVersionNegotiationPacket(hdr *wire.Header, versions []logging.VersionNumber) {
ver := make([]versionNumber, len(versions))
for i, v := range versions {
ver[i] = versionNumber(v)
}
t.mutex.Lock()
t.recordEvent(time.Now(), &eventVersionNegotiationReceived{
Header: *transformHeader(hdr),
SupportedVersions: ver,
})
t.mutex.Unlock()
}
func (t *connectionTracer) BufferedPacket(pt logging.PacketType) {
t.mutex.Lock()
t.recordEvent(time.Now(), &eventPacketBuffered{PacketType: pt})
t.mutex.Unlock()
}
func (t *connectionTracer) DroppedPacket(pt logging.PacketType, size protocol.ByteCount, reason logging.PacketDropReason) {
t.mutex.Lock()
t.recordEvent(time.Now(), &eventPacketDropped{
PacketType: pt,
PacketSize: size,
Trigger: packetDropReason(reason),
})
t.mutex.Unlock()
}
func (t *connectionTracer) UpdatedMetrics(rttStats *utils.RTTStats, cwnd, bytesInFlight protocol.ByteCount, packetsInFlight int) {
m := &metrics{
MinRTT: rttStats.MinRTT(),
SmoothedRTT: rttStats.SmoothedRTT(),
LatestRTT: rttStats.LatestRTT(),
RTTVariance: rttStats.MeanDeviation(),
CongestionWindow: cwnd,
BytesInFlight: bytesInFlight,
PacketsInFlight: packetsInFlight,
}
t.mutex.Lock()
t.recordEvent(time.Now(), &eventMetricsUpdated{
Last: t.lastMetrics,
Current: m,
})
t.lastMetrics = m
t.mutex.Unlock()
}
func (t *connectionTracer) AcknowledgedPacket(protocol.EncryptionLevel, protocol.PacketNumber) {}
func (t *connectionTracer) LostPacket(encLevel protocol.EncryptionLevel, pn protocol.PacketNumber, lossReason logging.PacketLossReason) {
t.mutex.Lock()
t.recordEvent(time.Now(), &eventPacketLost{
PacketType: getPacketTypeFromEncryptionLevel(encLevel),
PacketNumber: pn,
Trigger: packetLossReason(lossReason),
})
t.mutex.Unlock()
}
func (t *connectionTracer) UpdatedCongestionState(state logging.CongestionState) {
t.mutex.Lock()
t.recordEvent(time.Now(), &eventCongestionStateUpdated{state: congestionState(state)})
t.mutex.Unlock()
}
func (t *connectionTracer) UpdatedPTOCount(value uint32) {
t.mutex.Lock()
t.recordEvent(time.Now(), &eventUpdatedPTO{Value: value})
t.mutex.Unlock()
}
func (t *connectionTracer) UpdatedKeyFromTLS(encLevel protocol.EncryptionLevel, pers protocol.Perspective) {
t.mutex.Lock()
t.recordEvent(time.Now(), &eventKeyUpdated{
Trigger: keyUpdateTLS,
KeyType: encLevelToKeyType(encLevel, pers),
})
t.mutex.Unlock()
}
func (t *connectionTracer) UpdatedKey(generation protocol.KeyPhase, remote bool) {
trigger := keyUpdateLocal
if remote {
trigger = keyUpdateRemote
}
t.mutex.Lock()
now := time.Now()
t.recordEvent(now, &eventKeyUpdated{
Trigger: trigger,
KeyType: keyTypeClient1RTT,
Generation: generation,
})
t.recordEvent(now, &eventKeyUpdated{
Trigger: trigger,
KeyType: keyTypeServer1RTT,
Generation: generation,
})
t.mutex.Unlock()
}
func (t *connectionTracer) DroppedEncryptionLevel(encLevel protocol.EncryptionLevel) {
t.mutex.Lock()
now := time.Now()
if encLevel == protocol.Encryption0RTT {
t.recordEvent(now, &eventKeyRetired{KeyType: encLevelToKeyType(encLevel, t.perspective)})
} else {
t.recordEvent(now, &eventKeyRetired{KeyType: encLevelToKeyType(encLevel, protocol.PerspectiveServer)})
t.recordEvent(now, &eventKeyRetired{KeyType: encLevelToKeyType(encLevel, protocol.PerspectiveClient)})
}
t.mutex.Unlock()
}
func (t *connectionTracer) DroppedKey(generation protocol.KeyPhase) {
t.mutex.Lock()
now := time.Now()
t.recordEvent(now, &eventKeyRetired{
KeyType: encLevelToKeyType(protocol.Encryption1RTT, protocol.PerspectiveServer),
Generation: generation,
})
t.recordEvent(now, &eventKeyRetired{
KeyType: encLevelToKeyType(protocol.Encryption1RTT, protocol.PerspectiveClient),
Generation: generation,
})
t.mutex.Unlock()
}
func (t *connectionTracer) SetLossTimer(tt logging.TimerType, encLevel protocol.EncryptionLevel, timeout time.Time) {
t.mutex.Lock()
now := time.Now()
t.recordEvent(now, &eventLossTimerSet{
TimerType: timerType(tt),
EncLevel: encLevel,
Delta: timeout.Sub(now),
})
t.mutex.Unlock()
}
func (t *connectionTracer) LossTimerExpired(tt logging.TimerType, encLevel protocol.EncryptionLevel) {
t.mutex.Lock()
t.recordEvent(time.Now(), &eventLossTimerExpired{
TimerType: timerType(tt),
EncLevel: encLevel,
})
t.mutex.Unlock()
}
func (t *connectionTracer) LossTimerCanceled() {
t.mutex.Lock()
t.recordEvent(time.Now(), &eventLossTimerCanceled{})
t.mutex.Unlock()
}
func (t *connectionTracer) Debug(name, msg string) {
t.mutex.Lock()
t.recordEvent(time.Now(), &eventGeneric{
name: name,
msg: msg,
})
t.mutex.Unlock()
}
+66
View File
@@ -0,0 +1,66 @@
package qlog
import (
"time"
"github.com/francoispqt/gojay"
"github.com/lucas-clemente/quic-go/internal/protocol"
)
type topLevel struct {
trace trace
}
func (topLevel) IsNil() bool { return false }
func (l topLevel) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("qlog_format", "NDJSON")
enc.StringKey("qlog_version", "draft-02")
enc.StringKeyOmitEmpty("title", "quic-go qlog")
enc.StringKey("code_version", quicGoVersion)
enc.ObjectKey("trace", l.trace)
}
type vantagePoint struct {
Name string
Type protocol.Perspective
}
func (p vantagePoint) IsNil() bool { return false }
func (p vantagePoint) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKeyOmitEmpty("name", p.Name)
switch p.Type {
case protocol.PerspectiveClient:
enc.StringKey("type", "client")
case protocol.PerspectiveServer:
enc.StringKey("type", "server")
}
}
type commonFields struct {
ODCID connectionID
GroupID connectionID
ProtocolType string
ReferenceTime time.Time
}
func (f commonFields) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("ODCID", f.ODCID.String())
enc.StringKey("group_id", f.ODCID.String())
enc.StringKeyOmitEmpty("protocol_type", f.ProtocolType)
enc.Float64Key("reference_time", float64(f.ReferenceTime.UnixNano())/1e6)
enc.StringKey("time_format", "relative")
}
func (f commonFields) IsNil() bool { return false }
type trace struct {
VantagePoint vantagePoint
CommonFields commonFields
}
func (trace) IsNil() bool { return false }
func (t trace) MarshalJSONObject(enc *gojay.Encoder) {
enc.ObjectKey("vantage_point", t.VantagePoint)
enc.ObjectKey("common_fields", t.CommonFields)
}
+320
View File
@@ -0,0 +1,320 @@
package qlog
import (
"fmt"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/qerr"
"github.com/lucas-clemente/quic-go/logging"
)
type owner uint8
const (
ownerLocal owner = iota
ownerRemote
)
func (o owner) String() string {
switch o {
case ownerLocal:
return "local"
case ownerRemote:
return "remote"
default:
return "unknown owner"
}
}
type streamType protocol.StreamType
func (s streamType) String() string {
switch protocol.StreamType(s) {
case protocol.StreamTypeUni:
return "unidirectional"
case protocol.StreamTypeBidi:
return "bidirectional"
default:
return "unknown stream type"
}
}
type connectionID protocol.ConnectionID
func (c connectionID) String() string {
return fmt.Sprintf("%x", []byte(c))
}
// category is the qlog event category.
type category uint8
const (
categoryConnectivity category = iota
categoryTransport
categorySecurity
categoryRecovery
)
func (c category) String() string {
switch c {
case categoryConnectivity:
return "connectivity"
case categoryTransport:
return "transport"
case categorySecurity:
return "security"
case categoryRecovery:
return "recovery"
default:
return "unknown category"
}
}
type versionNumber protocol.VersionNumber
func (v versionNumber) String() string {
return fmt.Sprintf("%x", uint32(v))
}
func (packetHeader) IsNil() bool { return false }
func encLevelToPacketNumberSpace(encLevel protocol.EncryptionLevel) string {
switch encLevel {
case protocol.EncryptionInitial:
return "initial"
case protocol.EncryptionHandshake:
return "handshake"
case protocol.Encryption0RTT, protocol.Encryption1RTT:
return "application_data"
default:
return "unknown encryption level"
}
}
type keyType uint8
const (
keyTypeServerInitial keyType = 1 + iota
keyTypeClientInitial
keyTypeServerHandshake
keyTypeClientHandshake
keyTypeServer0RTT
keyTypeClient0RTT
keyTypeServer1RTT
keyTypeClient1RTT
)
func encLevelToKeyType(encLevel protocol.EncryptionLevel, pers protocol.Perspective) keyType {
if pers == protocol.PerspectiveServer {
switch encLevel {
case protocol.EncryptionInitial:
return keyTypeServerInitial
case protocol.EncryptionHandshake:
return keyTypeServerHandshake
case protocol.Encryption0RTT:
return keyTypeServer0RTT
case protocol.Encryption1RTT:
return keyTypeServer1RTT
default:
return 0
}
}
switch encLevel {
case protocol.EncryptionInitial:
return keyTypeClientInitial
case protocol.EncryptionHandshake:
return keyTypeClientHandshake
case protocol.Encryption0RTT:
return keyTypeClient0RTT
case protocol.Encryption1RTT:
return keyTypeClient1RTT
default:
return 0
}
}
func (t keyType) String() string {
switch t {
case keyTypeServerInitial:
return "server_initial_secret"
case keyTypeClientInitial:
return "client_initial_secret"
case keyTypeServerHandshake:
return "server_handshake_secret"
case keyTypeClientHandshake:
return "client_handshake_secret"
case keyTypeServer0RTT:
return "server_0rtt_secret"
case keyTypeClient0RTT:
return "client_0rtt_secret"
case keyTypeServer1RTT:
return "server_1rtt_secret"
case keyTypeClient1RTT:
return "client_1rtt_secret"
default:
return "unknown key type"
}
}
type keyUpdateTrigger uint8
const (
keyUpdateTLS keyUpdateTrigger = iota
keyUpdateRemote
keyUpdateLocal
)
func (t keyUpdateTrigger) String() string {
switch t {
case keyUpdateTLS:
return "tls"
case keyUpdateRemote:
return "remote_update"
case keyUpdateLocal:
return "local_update"
default:
return "unknown key update trigger"
}
}
type transportError uint64
func (e transportError) String() string {
switch qerr.TransportErrorCode(e) {
case qerr.NoError:
return "no_error"
case qerr.InternalError:
return "internal_error"
case qerr.ConnectionRefused:
return "connection_refused"
case qerr.FlowControlError:
return "flow_control_error"
case qerr.StreamLimitError:
return "stream_limit_error"
case qerr.StreamStateError:
return "stream_state_error"
case qerr.FinalSizeError:
return "final_size_error"
case qerr.FrameEncodingError:
return "frame_encoding_error"
case qerr.TransportParameterError:
return "transport_parameter_error"
case qerr.ConnectionIDLimitError:
return "connection_id_limit_error"
case qerr.ProtocolViolation:
return "protocol_violation"
case qerr.InvalidToken:
return "invalid_token"
case qerr.ApplicationErrorErrorCode:
return "application_error"
case qerr.CryptoBufferExceeded:
return "crypto_buffer_exceeded"
case qerr.KeyUpdateError:
return "key_update_error"
case qerr.AEADLimitReached:
return "aead_limit_reached"
case qerr.NoViablePathError:
return "no_viable_path"
default:
return ""
}
}
type packetType logging.PacketType
func (t packetType) String() string {
switch logging.PacketType(t) {
case logging.PacketTypeInitial:
return "initial"
case logging.PacketTypeHandshake:
return "handshake"
case logging.PacketTypeRetry:
return "retry"
case logging.PacketType0RTT:
return "0RTT"
case logging.PacketTypeVersionNegotiation:
return "version_negotiation"
case logging.PacketTypeStatelessReset:
return "stateless_reset"
case logging.PacketType1RTT:
return "1RTT"
case logging.PacketTypeNotDetermined:
return ""
default:
return "unknown packet type"
}
}
type packetLossReason logging.PacketLossReason
func (r packetLossReason) String() string {
switch logging.PacketLossReason(r) {
case logging.PacketLossReorderingThreshold:
return "reordering_threshold"
case logging.PacketLossTimeThreshold:
return "time_threshold"
default:
return "unknown loss reason"
}
}
type packetDropReason logging.PacketDropReason
func (r packetDropReason) String() string {
switch logging.PacketDropReason(r) {
case logging.PacketDropKeyUnavailable:
return "key_unavailable"
case logging.PacketDropUnknownConnectionID:
return "unknown_connection_id"
case logging.PacketDropHeaderParseError:
return "header_parse_error"
case logging.PacketDropPayloadDecryptError:
return "payload_decrypt_error"
case logging.PacketDropProtocolViolation:
return "protocol_violation"
case logging.PacketDropDOSPrevention:
return "dos_prevention"
case logging.PacketDropUnsupportedVersion:
return "unsupported_version"
case logging.PacketDropUnexpectedPacket:
return "unexpected_packet"
case logging.PacketDropUnexpectedSourceConnectionID:
return "unexpected_source_connection_id"
case logging.PacketDropUnexpectedVersion:
return "unexpected_version"
case logging.PacketDropDuplicate:
return "duplicate"
default:
return "unknown packet drop reason"
}
}
type timerType logging.TimerType
func (t timerType) String() string {
switch logging.TimerType(t) {
case logging.TimerTypeACK:
return "ack"
case logging.TimerTypePTO:
return "pto"
default:
return "unknown timer type"
}
}
type congestionState logging.CongestionState
func (s congestionState) String() string {
switch logging.CongestionState(s) {
case logging.CongestionStateSlowStart:
return "slow_start"
case logging.CongestionStateCongestionAvoidance:
return "congestion_avoidance"
case logging.CongestionStateRecovery:
return "recovery"
case logging.CongestionStateApplicationLimited:
return "application_limited"
default:
return "unknown congestion state"
}
}
+2 -1
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.7,amd64,!gccgo,!appengine
//go:build go1.7 && amd64 && gc && !purego
// +build go1.7,amd64,gc,!purego
package blake2b
+46 -51
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.7,amd64,!gccgo,!appengine
//go:build go1.7 && amd64 && gc && !purego
// +build go1.7,amd64,gc,!purego
#include "textflag.h"
@@ -282,14 +283,12 @@ TEXT ·hashBlocksAVX2(SB), 4, $320-48 // frame size = 288 + 32 byte alignment
MOVQ blocks_len+32(FP), DI
MOVQ SP, DX
MOVQ SP, R9
ADDQ $31, R9
ANDQ $~31, R9
MOVQ R9, SP
ADDQ $31, DX
ANDQ $~31, DX
MOVQ CX, 16(SP)
MOVQ CX, 16(DX)
XORQ CX, CX
MOVQ CX, 24(SP)
MOVQ CX, 24(DX)
VMOVDQU ·AVX2_c40<>(SB), Y4
VMOVDQU ·AVX2_c48<>(SB), Y5
@@ -301,33 +300,33 @@ TEXT ·hashBlocksAVX2(SB), 4, $320-48 // frame size = 288 + 32 byte alignment
MOVQ 0(BX), R8
MOVQ 8(BX), R9
MOVQ R9, 8(SP)
MOVQ R9, 8(DX)
loop:
ADDQ $128, R8
MOVQ R8, 0(SP)
MOVQ R8, 0(DX)
CMPQ R8, $128
JGE noinc
INCQ R9
MOVQ R9, 8(SP)
MOVQ R9, 8(DX)
noinc:
VMOVDQA Y8, Y0
VMOVDQA Y9, Y1
VMOVDQA Y6, Y2
VPXOR 0(SP), Y7, Y3
VPXOR 0(DX), Y7, Y3
LOAD_MSG_AVX2_0_2_4_6_1_3_5_7_8_10_12_14_9_11_13_15()
VMOVDQA Y12, 32(SP)
VMOVDQA Y13, 64(SP)
VMOVDQA Y14, 96(SP)
VMOVDQA Y15, 128(SP)
VMOVDQA Y12, 32(DX)
VMOVDQA Y13, 64(DX)
VMOVDQA Y14, 96(DX)
VMOVDQA Y15, 128(DX)
ROUND_AVX2(Y12, Y13, Y14, Y15, Y10, Y4, Y5)
LOAD_MSG_AVX2_14_4_9_13_10_8_15_6_1_0_11_5_12_2_7_3()
VMOVDQA Y12, 160(SP)
VMOVDQA Y13, 192(SP)
VMOVDQA Y14, 224(SP)
VMOVDQA Y15, 256(SP)
VMOVDQA Y12, 160(DX)
VMOVDQA Y13, 192(DX)
VMOVDQA Y14, 224(DX)
VMOVDQA Y15, 256(DX)
ROUND_AVX2(Y12, Y13, Y14, Y15, Y10, Y4, Y5)
LOAD_MSG_AVX2_11_12_5_15_8_0_2_13_10_3_7_9_14_6_1_4()
@@ -347,8 +346,8 @@ noinc:
LOAD_MSG_AVX2_10_8_7_1_2_4_6_5_15_9_3_13_11_14_12_0()
ROUND_AVX2(Y12, Y13, Y14, Y15, Y10, Y4, Y5)
ROUND_AVX2(32(SP), 64(SP), 96(SP), 128(SP), Y10, Y4, Y5)
ROUND_AVX2(160(SP), 192(SP), 224(SP), 256(SP), Y10, Y4, Y5)
ROUND_AVX2(32(DX), 64(DX), 96(DX), 128(DX), Y10, Y4, Y5)
ROUND_AVX2(160(DX), 192(DX), 224(DX), 256(DX), Y10, Y4, Y5)
VPXOR Y0, Y8, Y8
VPXOR Y1, Y9, Y9
@@ -366,7 +365,6 @@ noinc:
VMOVDQU Y9, 32(AX)
VZEROUPPER
MOVQ DX, SP
RET
#define VPUNPCKLQDQ_X2_X2_X15 BYTE $0xC5; BYTE $0x69; BYTE $0x6C; BYTE $0xFA
@@ -584,11 +582,9 @@ TEXT ·hashBlocksAVX(SB), 4, $288-48 // frame size = 272 + 16 byte alignment
MOVQ blocks_base+24(FP), SI
MOVQ blocks_len+32(FP), DI
MOVQ SP, BP
MOVQ SP, R9
ADDQ $15, R9
ANDQ $~15, R9
MOVQ R9, SP
MOVQ SP, R10
ADDQ $15, R10
ANDQ $~15, R10
VMOVDQU ·AVX_c40<>(SB), X0
VMOVDQU ·AVX_c48<>(SB), X1
@@ -596,8 +592,8 @@ TEXT ·hashBlocksAVX(SB), 4, $288-48 // frame size = 272 + 16 byte alignment
VMOVDQA X1, X9
VMOVDQU ·AVX_iv3<>(SB), X0
VMOVDQA X0, 0(SP)
XORQ CX, 0(SP) // 0(SP) = ·AVX_iv3 ^ (CX || 0)
VMOVDQA X0, 0(R10)
XORQ CX, 0(R10) // 0(R10) = ·AVX_iv3 ^ (CX || 0)
VMOVDQU 0(AX), X10
VMOVDQU 16(AX), X11
@@ -624,35 +620,35 @@ noinc:
VMOVDQU ·AVX_iv2<>(SB), X6
VPXOR X15, X6, X6
VMOVDQA 0(SP), X7
VMOVDQA 0(R10), X7
LOAD_MSG_AVX_0_2_4_6_1_3_5_7()
VMOVDQA X12, 16(SP)
VMOVDQA X13, 32(SP)
VMOVDQA X14, 48(SP)
VMOVDQA X15, 64(SP)
VMOVDQA X12, 16(R10)
VMOVDQA X13, 32(R10)
VMOVDQA X14, 48(R10)
VMOVDQA X15, 64(R10)
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, X12, X13, X14, X15, X15, X8, X9)
SHUFFLE_AVX()
LOAD_MSG_AVX(8, 10, 12, 14, 9, 11, 13, 15)
VMOVDQA X12, 80(SP)
VMOVDQA X13, 96(SP)
VMOVDQA X14, 112(SP)
VMOVDQA X15, 128(SP)
VMOVDQA X12, 80(R10)
VMOVDQA X13, 96(R10)
VMOVDQA X14, 112(R10)
VMOVDQA X15, 128(R10)
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, X12, X13, X14, X15, X15, X8, X9)
SHUFFLE_AVX_INV()
LOAD_MSG_AVX(14, 4, 9, 13, 10, 8, 15, 6)
VMOVDQA X12, 144(SP)
VMOVDQA X13, 160(SP)
VMOVDQA X14, 176(SP)
VMOVDQA X15, 192(SP)
VMOVDQA X12, 144(R10)
VMOVDQA X13, 160(R10)
VMOVDQA X14, 176(R10)
VMOVDQA X15, 192(R10)
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, X12, X13, X14, X15, X15, X8, X9)
SHUFFLE_AVX()
LOAD_MSG_AVX_1_0_11_5_12_2_7_3()
VMOVDQA X12, 208(SP)
VMOVDQA X13, 224(SP)
VMOVDQA X14, 240(SP)
VMOVDQA X15, 256(SP)
VMOVDQA X12, 208(R10)
VMOVDQA X13, 224(R10)
VMOVDQA X14, 240(R10)
VMOVDQA X15, 256(R10)
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, X12, X13, X14, X15, X15, X8, X9)
SHUFFLE_AVX_INV()
@@ -712,14 +708,14 @@ noinc:
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, X12, X13, X14, X15, X15, X8, X9)
SHUFFLE_AVX_INV()
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, 16(SP), 32(SP), 48(SP), 64(SP), X15, X8, X9)
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, 16(R10), 32(R10), 48(R10), 64(R10), X15, X8, X9)
SHUFFLE_AVX()
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, 80(SP), 96(SP), 112(SP), 128(SP), X15, X8, X9)
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, 80(R10), 96(R10), 112(R10), 128(R10), X15, X8, X9)
SHUFFLE_AVX_INV()
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, 144(SP), 160(SP), 176(SP), 192(SP), X15, X8, X9)
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, 144(R10), 160(R10), 176(R10), 192(R10), X15, X8, X9)
SHUFFLE_AVX()
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, 208(SP), 224(SP), 240(SP), 256(SP), X15, X8, X9)
HALF_ROUND_AVX(X0, X1, X2, X3, X4, X5, X6, X7, 208(R10), 224(R10), 240(R10), 256(R10), X15, X8, X9)
SHUFFLE_AVX_INV()
VMOVDQU 32(AX), X14
@@ -746,5 +742,4 @@ noinc:
MOVQ R9, 8(BX)
VZEROUPPER
MOVQ BP, SP
RET
+2 -1
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !go1.7,amd64,!gccgo,!appengine
//go:build !go1.7 && amd64 && gc && !purego
// +build !go1.7,amd64,gc,!purego
package blake2b
+28 -30
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build amd64,!gccgo,!appengine
//go:build amd64 && gc && !purego
// +build amd64,gc,!purego
#include "textflag.h"
@@ -118,15 +119,13 @@ TEXT ·hashBlocksSSE4(SB), 4, $288-48 // frame size = 272 + 16 byte alignment
MOVQ blocks_base+24(FP), SI
MOVQ blocks_len+32(FP), DI
MOVQ SP, BP
MOVQ SP, R9
ADDQ $15, R9
ANDQ $~15, R9
MOVQ R9, SP
MOVQ SP, R10
ADDQ $15, R10
ANDQ $~15, R10
MOVOU ·iv3<>(SB), X0
MOVO X0, 0(SP)
XORQ CX, 0(SP) // 0(SP) = ·iv3 ^ (CX || 0)
MOVO X0, 0(R10)
XORQ CX, 0(R10) // 0(R10) = ·iv3 ^ (CX || 0)
MOVOU ·c40<>(SB), X13
MOVOU ·c48<>(SB), X14
@@ -156,35 +155,35 @@ noinc:
MOVOU ·iv2<>(SB), X6
PXOR X8, X6
MOVO 0(SP), X7
MOVO 0(R10), X7
LOAD_MSG(X8, X9, X10, X11, SI, 0, 2, 4, 6, 1, 3, 5, 7)
MOVO X8, 16(SP)
MOVO X9, 32(SP)
MOVO X10, 48(SP)
MOVO X11, 64(SP)
MOVO X8, 16(R10)
MOVO X9, 32(R10)
MOVO X10, 48(R10)
MOVO X11, 64(R10)
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
LOAD_MSG(X8, X9, X10, X11, SI, 8, 10, 12, 14, 9, 11, 13, 15)
MOVO X8, 80(SP)
MOVO X9, 96(SP)
MOVO X10, 112(SP)
MOVO X11, 128(SP)
MOVO X8, 80(R10)
MOVO X9, 96(R10)
MOVO X10, 112(R10)
MOVO X11, 128(R10)
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
LOAD_MSG(X8, X9, X10, X11, SI, 14, 4, 9, 13, 10, 8, 15, 6)
MOVO X8, 144(SP)
MOVO X9, 160(SP)
MOVO X10, 176(SP)
MOVO X11, 192(SP)
MOVO X8, 144(R10)
MOVO X9, 160(R10)
MOVO X10, 176(R10)
MOVO X11, 192(R10)
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
LOAD_MSG(X8, X9, X10, X11, SI, 1, 0, 11, 5, 12, 2, 7, 3)
MOVO X8, 208(SP)
MOVO X9, 224(SP)
MOVO X10, 240(SP)
MOVO X11, 256(SP)
MOVO X8, 208(R10)
MOVO X9, 224(R10)
MOVO X10, 240(R10)
MOVO X11, 256(R10)
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
@@ -244,14 +243,14 @@ noinc:
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X11, X13, X14)
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, 16(SP), 32(SP), 48(SP), 64(SP), X11, X13, X14)
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, 16(R10), 32(R10), 48(R10), 64(R10), X11, X13, X14)
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, 80(SP), 96(SP), 112(SP), 128(SP), X11, X13, X14)
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, 80(R10), 96(R10), 112(R10), 128(R10), X11, X13, X14)
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, 144(SP), 160(SP), 176(SP), 192(SP), X11, X13, X14)
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, 144(R10), 160(R10), 176(R10), 192(R10), X11, X13, X14)
SHUFFLE(X2, X3, X4, X5, X6, X7, X8, X9)
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, 208(SP), 224(SP), 240(SP), 256(SP), X11, X13, X14)
HALF_ROUND(X0, X1, X2, X3, X4, X5, X6, X7, 208(R10), 224(R10), 240(R10), 256(R10), X11, X13, X14)
SHUFFLE_INV(X2, X3, X4, X5, X6, X7, X8, X9)
MOVOU 32(AX), X10
@@ -277,5 +276,4 @@ noinc:
MOVQ R8, 0(BX)
MOVQ R9, 8(BX)
MOVQ BP, SP
RET
+2 -1
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !amd64 appengine gccgo
//go:build !amd64 || purego || !gc
// +build !amd64 purego !gc
package blake2b
+1
View File
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.9
// +build go1.9
package blake2b
+2 -1
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.11,!gccgo,!purego
//go:build go1.11 && gc && !purego
// +build go1.11,gc,!purego
package chacha20
+2 -1
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.11,!gccgo,!purego
//go:build go1.11 && gc && !purego
// +build go1.11,gc,!purego
#include "textflag.h"
+2 -1
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !arm64,!s390x,!ppc64le arm64,!go1.11 gccgo purego
//go:build (!arm64 && !s390x && !ppc64le) || (arm64 && !go1.11) || !gc || purego
// +build !arm64,!s390x,!ppc64le arm64,!go1.11 !gc purego
package chacha20
+2 -1
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !gccgo,!purego
//go:build gc && !purego
// +build gc,!purego
package chacha20
+2 -1
View File
@@ -19,7 +19,8 @@
// The differences in this and the original implementation are
// due to the calling conventions and initialization of constants.
// +build !gccgo,!purego
//go:build gc && !purego
// +build gc,!purego
#include "textflag.h"
+2 -1
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !gccgo,!purego
//go:build gc && !purego
// +build gc,!purego
package chacha20
+2 -1
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !gccgo,!purego
//go:build gc && !purego
// +build gc,!purego
#include "go_asm.h"
#include "textflag.h"
+5 -1
View File
@@ -26,6 +26,10 @@ const (
// NonceSizeX is the size of the nonce used with the XChaCha20-Poly1305
// variant of this AEAD, in bytes.
NonceSizeX = 24
// Overhead is the size of the Poly1305 authentication tag, and the
// difference between a ciphertext length and its plaintext.
Overhead = 16
)
type chacha20poly1305 struct {
@@ -47,7 +51,7 @@ func (c *chacha20poly1305) NonceSize() int {
}
func (c *chacha20poly1305) Overhead() int {
return 16
return Overhead
}
func (c *chacha20poly1305) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
+2 -1
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !gccgo,!purego
//go:build gc && !purego
// +build gc,!purego
package chacha20poly1305
+2 -1
View File
@@ -4,7 +4,8 @@
// This file was originally from https://golang.org/cl/24717 by Vlad Krasnov of CloudFlare.
// +build !gccgo,!purego
//go:build gc && !purego
// +build gc,!purego
#include "textflag.h"
// General register allocation
+1 -1
View File
@@ -8,8 +8,8 @@ import (
"encoding/binary"
"golang.org/x/crypto/chacha20"
"golang.org/x/crypto/internal/poly1305"
"golang.org/x/crypto/internal/subtle"
"golang.org/x/crypto/poly1305"
)
func writeWithPadding(p *poly1305.MAC, b []byte) {
+2 -1
View File
@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !amd64 gccgo purego
//go:build !amd64 || !gc || purego
// +build !amd64 !gc purego
package chacha20poly1305
+1 -1
View File
@@ -35,7 +35,7 @@ func (*xchacha20poly1305) NonceSize() int {
}
func (*xchacha20poly1305) Overhead() int {
return 16
return Overhead
}
func (x *xchacha20poly1305) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
+52
View File
@@ -117,6 +117,19 @@ func (b *Builder) AddASN1GeneralizedTime(t time.Time) {
})
}
// AddASN1UTCTime appends a DER-encoded ASN.1 UTCTime.
func (b *Builder) AddASN1UTCTime(t time.Time) {
b.AddASN1(asn1.UTCTime, func(c *Builder) {
// As utilized by the X.509 profile, UTCTime can only
// represent the years 1950 through 2049.
if t.Year() < 1950 || t.Year() >= 2050 {
b.err = fmt.Errorf("cryptobyte: cannot represent %v as a UTCTime", t)
return
}
c.AddBytes([]byte(t.Format(defaultUTCTimeFormatStr)))
})
}
// AddASN1BitString appends a DER-encoded ASN.1 BIT STRING. This does not
// support BIT STRINGs that are not a whole number of bytes.
func (b *Builder) AddASN1BitString(data []byte) {
@@ -466,6 +479,45 @@ func (s *String) ReadASN1GeneralizedTime(out *time.Time) bool {
return true
}
const defaultUTCTimeFormatStr = "060102150405Z0700"
// ReadASN1UTCTime decodes an ASN.1 UTCTime into out and advances.
// It reports whether the read was successful.
func (s *String) ReadASN1UTCTime(out *time.Time) bool {
var bytes String
if !s.ReadASN1(&bytes, asn1.UTCTime) {
return false
}
t := string(bytes)
formatStr := defaultUTCTimeFormatStr
var err error
res, err := time.Parse(formatStr, t)
if err != nil {
// Fallback to minute precision if we can't parse second
// precision. If we are following X.509 or X.690 we shouldn't
// support this, but we do.
formatStr = "0601021504Z0700"
res, err = time.Parse(formatStr, t)
}
if err != nil {
return false
}
if serialized := res.Format(formatStr); serialized != t {
return false
}
if res.Year() >= 2050 {
// UTCTime interprets the low order digits 50-99 as 1950-99.
// This only applies to its use in the X.509 profile.
// See https://tools.ietf.org/html/rfc5280#section-4.1.2.5.1
res = res.AddDate(-100, 0, 0)
}
*out = res
return true
}
// ReadASN1BitString decodes an ASN.1 BIT STRING into out and advances.
// It reports whether the read was successful.
func (s *String) ReadASN1BitString(out *encoding_asn1.BitString) bool {
+51 -1
View File
@@ -10,6 +10,8 @@ package curve25519 // import "golang.org/x/crypto/curve25519"
import (
"crypto/subtle"
"fmt"
"golang.org/x/crypto/curve25519/internal/field"
)
// ScalarMult sets dst to the product scalar * point.
@@ -18,7 +20,55 @@ import (
// zeroes, irrespective of the scalar. Instead, use the X25519 function, which
// will return an error.
func ScalarMult(dst, scalar, point *[32]byte) {
scalarMult(dst, scalar, point)
var e [32]byte
copy(e[:], scalar[:])
e[0] &= 248
e[31] &= 127
e[31] |= 64
var x1, x2, z2, x3, z3, tmp0, tmp1 field.Element
x1.SetBytes(point[:])
x2.One()
x3.Set(&x1)
z3.One()
swap := 0
for pos := 254; pos >= 0; pos-- {
b := e[pos/8] >> uint(pos&7)
b &= 1
swap ^= int(b)
x2.Swap(&x3, swap)
z2.Swap(&z3, swap)
swap = int(b)
tmp0.Subtract(&x3, &z3)
tmp1.Subtract(&x2, &z2)
x2.Add(&x2, &z2)
z2.Add(&x3, &z3)
z3.Multiply(&tmp0, &x2)
z2.Multiply(&z2, &tmp1)
tmp0.Square(&tmp1)
tmp1.Square(&x2)
x3.Add(&z3, &z2)
z2.Subtract(&z3, &z2)
x2.Multiply(&tmp1, &tmp0)
tmp1.Subtract(&tmp1, &tmp0)
z2.Square(&z2)
z3.Mult32(&tmp1, 121666)
x3.Square(&x3)
tmp0.Add(&tmp0, &z3)
z3.Multiply(&x1, &z2)
z2.Multiply(&tmp1, &tmp0)
}
x2.Swap(&x3, swap)
z2.Swap(&z3, swap)
z2.Invert(&z2)
x2.Multiply(&x2, &z2)
copy(dst[:], x2.Bytes())
}
// ScalarBaseMult sets dst to the product scalar * base where base is the
-240
View File
@@ -1,240 +0,0 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build amd64,!gccgo,!appengine,!purego
package curve25519
// These functions are implemented in the .s files. The names of the functions
// in the rest of the file are also taken from the SUPERCOP sources to help
// people following along.
//go:noescape
func cswap(inout *[5]uint64, v uint64)
//go:noescape
func ladderstep(inout *[5][5]uint64)
//go:noescape
func freeze(inout *[5]uint64)
//go:noescape
func mul(dest, a, b *[5]uint64)
//go:noescape
func square(out, in *[5]uint64)
// mladder uses a Montgomery ladder to calculate (xr/zr) *= s.
func mladder(xr, zr *[5]uint64, s *[32]byte) {
var work [5][5]uint64
work[0] = *xr
setint(&work[1], 1)
setint(&work[2], 0)
work[3] = *xr
setint(&work[4], 1)
j := uint(6)
var prevbit byte
for i := 31; i >= 0; i-- {
for j < 8 {
bit := ((*s)[i] >> j) & 1
swap := bit ^ prevbit
prevbit = bit
cswap(&work[1], uint64(swap))
ladderstep(&work)
j--
}
j = 7
}
*xr = work[1]
*zr = work[2]
}
func scalarMult(out, in, base *[32]byte) {
var e [32]byte
copy(e[:], (*in)[:])
e[0] &= 248
e[31] &= 127
e[31] |= 64
var t, z [5]uint64
unpack(&t, base)
mladder(&t, &z, &e)
invert(&z, &z)
mul(&t, &t, &z)
pack(out, &t)
}
func setint(r *[5]uint64, v uint64) {
r[0] = v
r[1] = 0
r[2] = 0
r[3] = 0
r[4] = 0
}
// unpack sets r = x where r consists of 5, 51-bit limbs in little-endian
// order.
func unpack(r *[5]uint64, x *[32]byte) {
r[0] = uint64(x[0]) |
uint64(x[1])<<8 |
uint64(x[2])<<16 |
uint64(x[3])<<24 |
uint64(x[4])<<32 |
uint64(x[5])<<40 |
uint64(x[6]&7)<<48
r[1] = uint64(x[6])>>3 |
uint64(x[7])<<5 |
uint64(x[8])<<13 |
uint64(x[9])<<21 |
uint64(x[10])<<29 |
uint64(x[11])<<37 |
uint64(x[12]&63)<<45
r[2] = uint64(x[12])>>6 |
uint64(x[13])<<2 |
uint64(x[14])<<10 |
uint64(x[15])<<18 |
uint64(x[16])<<26 |
uint64(x[17])<<34 |
uint64(x[18])<<42 |
uint64(x[19]&1)<<50
r[3] = uint64(x[19])>>1 |
uint64(x[20])<<7 |
uint64(x[21])<<15 |
uint64(x[22])<<23 |
uint64(x[23])<<31 |
uint64(x[24])<<39 |
uint64(x[25]&15)<<47
r[4] = uint64(x[25])>>4 |
uint64(x[26])<<4 |
uint64(x[27])<<12 |
uint64(x[28])<<20 |
uint64(x[29])<<28 |
uint64(x[30])<<36 |
uint64(x[31]&127)<<44
}
// pack sets out = x where out is the usual, little-endian form of the 5,
// 51-bit limbs in x.
func pack(out *[32]byte, x *[5]uint64) {
t := *x
freeze(&t)
out[0] = byte(t[0])
out[1] = byte(t[0] >> 8)
out[2] = byte(t[0] >> 16)
out[3] = byte(t[0] >> 24)
out[4] = byte(t[0] >> 32)
out[5] = byte(t[0] >> 40)
out[6] = byte(t[0] >> 48)
out[6] ^= byte(t[1]<<3) & 0xf8
out[7] = byte(t[1] >> 5)
out[8] = byte(t[1] >> 13)
out[9] = byte(t[1] >> 21)
out[10] = byte(t[1] >> 29)
out[11] = byte(t[1] >> 37)
out[12] = byte(t[1] >> 45)
out[12] ^= byte(t[2]<<6) & 0xc0
out[13] = byte(t[2] >> 2)
out[14] = byte(t[2] >> 10)
out[15] = byte(t[2] >> 18)
out[16] = byte(t[2] >> 26)
out[17] = byte(t[2] >> 34)
out[18] = byte(t[2] >> 42)
out[19] = byte(t[2] >> 50)
out[19] ^= byte(t[3]<<1) & 0xfe
out[20] = byte(t[3] >> 7)
out[21] = byte(t[3] >> 15)
out[22] = byte(t[3] >> 23)
out[23] = byte(t[3] >> 31)
out[24] = byte(t[3] >> 39)
out[25] = byte(t[3] >> 47)
out[25] ^= byte(t[4]<<4) & 0xf0
out[26] = byte(t[4] >> 4)
out[27] = byte(t[4] >> 12)
out[28] = byte(t[4] >> 20)
out[29] = byte(t[4] >> 28)
out[30] = byte(t[4] >> 36)
out[31] = byte(t[4] >> 44)
}
// invert calculates r = x^-1 mod p using Fermat's little theorem.
func invert(r *[5]uint64, x *[5]uint64) {
var z2, z9, z11, z2_5_0, z2_10_0, z2_20_0, z2_50_0, z2_100_0, t [5]uint64
square(&z2, x) /* 2 */
square(&t, &z2) /* 4 */
square(&t, &t) /* 8 */
mul(&z9, &t, x) /* 9 */
mul(&z11, &z9, &z2) /* 11 */
square(&t, &z11) /* 22 */
mul(&z2_5_0, &t, &z9) /* 2^5 - 2^0 = 31 */
square(&t, &z2_5_0) /* 2^6 - 2^1 */
for i := 1; i < 5; i++ { /* 2^20 - 2^10 */
square(&t, &t)
}
mul(&z2_10_0, &t, &z2_5_0) /* 2^10 - 2^0 */
square(&t, &z2_10_0) /* 2^11 - 2^1 */
for i := 1; i < 10; i++ { /* 2^20 - 2^10 */
square(&t, &t)
}
mul(&z2_20_0, &t, &z2_10_0) /* 2^20 - 2^0 */
square(&t, &z2_20_0) /* 2^21 - 2^1 */
for i := 1; i < 20; i++ { /* 2^40 - 2^20 */
square(&t, &t)
}
mul(&t, &t, &z2_20_0) /* 2^40 - 2^0 */
square(&t, &t) /* 2^41 - 2^1 */
for i := 1; i < 10; i++ { /* 2^50 - 2^10 */
square(&t, &t)
}
mul(&z2_50_0, &t, &z2_10_0) /* 2^50 - 2^0 */
square(&t, &z2_50_0) /* 2^51 - 2^1 */
for i := 1; i < 50; i++ { /* 2^100 - 2^50 */
square(&t, &t)
}
mul(&z2_100_0, &t, &z2_50_0) /* 2^100 - 2^0 */
square(&t, &z2_100_0) /* 2^101 - 2^1 */
for i := 1; i < 100; i++ { /* 2^200 - 2^100 */
square(&t, &t)
}
mul(&t, &t, &z2_100_0) /* 2^200 - 2^0 */
square(&t, &t) /* 2^201 - 2^1 */
for i := 1; i < 50; i++ { /* 2^250 - 2^50 */
square(&t, &t)
}
mul(&t, &t, &z2_50_0) /* 2^250 - 2^0 */
square(&t, &t) /* 2^251 - 2^1 */
square(&t, &t) /* 2^252 - 2^2 */
square(&t, &t) /* 2^253 - 2^3 */
square(&t, &t) /* 2^254 - 2^4 */
square(&t, &t) /* 2^255 - 2^5 */
mul(r, &t, &z11) /* 2^255 - 21 */
}

Some files were not shown because too many files have changed in this diff Show More