Skip to content

Commit

Permalink
Avoid using deprecated hujson API (#17)
Browse files Browse the repository at this point in the history
The hujson.Unmarshal function is deprecated.
Use hujson.Standardize instead.

The hujson package is evolving to be just a syntactic parser for HuJSON
and is moving away from semantically understanding HuJSON as Go data.
  • Loading branch information
dsnet authored May 10, 2022
1 parent eeb1d64 commit f0d7764
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/stretchr/testify v1.7.1
github.com/tailscale/hujson v0.0.0-20211215203138-ffd971c5f362
github.com/tailscale/hujson v0.0.0-20220506213045-af5ed07155e5
)

require (
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tailscale/hujson v0.0.0-20211215203138-ffd971c5f362 h1:xx7EMpWIKUrMMg+QanclF7bj8QTH/XYdQb/eplkmkgw=
github.com/tailscale/hujson v0.0.0-20211215203138-ffd971c5f362/go.mod h1:iTDXJsA6A2wNNjurgic2rk+is6uzU4U2NLm4T+edr6M=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
github.com/tailscale/hujson v0.0.0-20220506213045-af5ed07155e5 h1:erxeiTyq+nw4Cz5+hLDkOwNF5/9IQWCQPv0gpb3+QHU=
github.com/tailscale/hujson v0.0.0-20220506213045-af5ed07155e5/go.mod h1:DFSS3NAGHthKo1gTlmEcSBiZrRJXi28rLNd/1udP1c8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
54 changes: 33 additions & 21 deletions tailscale/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (c *Client) buildRequest(ctx context.Context, method, uri string, body inte

var bodyBytes []byte
if body != nil {
bodyBytes, err = hujson.MarshalIndent(body, "", " ")
bodyBytes, err = json.MarshalIndent(body, "", " ")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -118,9 +119,20 @@ func (c *Client) performRequest(req *http.Request, out interface{}) error {
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if !json.Valid(body) {
body, err = hujson.Standardize(body)
if err != nil {
return err
}
}

if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
var apiErr APIError
if err = hujson.NewDecoder(res.Body).Decode(&apiErr); err != nil {
if err = json.Unmarshal(body, &apiErr); err != nil {
return err
}

Expand All @@ -129,7 +141,7 @@ func (c *Client) performRequest(req *http.Request, out interface{}) error {
}

if out != nil {
return hujson.NewDecoder(res.Body).Decode(out)
return json.Unmarshal(body, out)
}

return nil
Expand Down Expand Up @@ -391,24 +403,24 @@ func (t *Time) UnmarshalJSON(data []byte) error {
}

type Device struct {
Addresses []string `json:"addresses"`
Name string `json:"name"`
ID string `json:"id"`
Authorized bool `json:"authorized"`
User string `json:"user"`
Tags []string `json:"tags"`
KeyExpiryDisabled bool `json:"keyExpiryDisabled"`
BlocksIncomingConnections bool `json:"blocksIncomingConnections"`
ClientVersion string `json:"clientVersion"`
Created Time `json:"created"`
Expires Time `json:"expires"`
Hostname string `json:"hostname"`
IsExternal bool `json:"isExternal"`
LastSeen Time `json:"lastSeen"`
MachineKey string `json:"machineKey"`
NodeKey string `json:"nodeKey"`
OS string `json:"os"`
UpdateAvailable bool `json:"updateAvailable"`
Addresses []string `json:"addresses"`
Name string `json:"name"`
ID string `json:"id"`
Authorized bool `json:"authorized"`
User string `json:"user"`
Tags []string `json:"tags"`
KeyExpiryDisabled bool `json:"keyExpiryDisabled"`
BlocksIncomingConnections bool `json:"blocksIncomingConnections"`
ClientVersion string `json:"clientVersion"`
Created Time `json:"created"`
Expires Time `json:"expires"`
Hostname string `json:"hostname"`
IsExternal bool `json:"isExternal"`
LastSeen Time `json:"lastSeen"`
MachineKey string `json:"machineKey"`
NodeKey string `json:"nodeKey"`
OS string `json:"os"`
UpdateAvailable bool `json:"updateAvailable"`
}

// Devices lists the devices in a tailnet.
Expand Down
12 changes: 9 additions & 3 deletions tailscale/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,15 @@ func TestACL_Unmarshal(t *testing.T) {
},
},
{
Name: "It should handle HuJSON ACLs",
ACLContent: huJSONACL,
UnmarshalFunc: hujson.Unmarshal,
Name: "It should handle HuJSON ACLs",
ACLContent: huJSONACL,
UnmarshalFunc: func(b []byte, v interface{}) error {
b, err := hujson.Standardize(b)
if err != nil {
return err
}
return json.Unmarshal(b, v)
},
Expected: tailscale.ACL{
ACLs: []tailscale.ACLEntry{
{
Expand Down

0 comments on commit f0d7764

Please sign in to comment.