📌 Upgraded and vendored go modules
This commit is contained in:
parent
2e6989e9a3
commit
8fd01ab5f5
42
go.mod
42
go.mod
|
@ -1,24 +1,24 @@
|
|||
module source.toby3d.me/toby3d/auth
|
||||
|
||||
go 1.20
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0
|
||||
github.com/brianvoe/gofakeit/v6 v6.23.1
|
||||
github.com/brianvoe/gofakeit/v6 v6.24.0
|
||||
github.com/caarlos0/env/v9 v9.0.0
|
||||
github.com/goccy/go-json v0.10.2
|
||||
github.com/google/go-cmp v0.5.9
|
||||
github.com/google/go-cmp v0.6.0
|
||||
github.com/jmoiron/sqlx v1.3.5
|
||||
github.com/lestrrat-go/jwx/v2 v2.0.11
|
||||
github.com/lestrrat-go/jwx/v2 v2.0.16
|
||||
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80
|
||||
github.com/valyala/fasttemplate v1.2.2
|
||||
github.com/valyala/quicktemplate v1.7.0
|
||||
go.etcd.io/bbolt v1.3.7
|
||||
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b
|
||||
golang.org/x/text v0.12.0
|
||||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
|
||||
go.etcd.io/bbolt v1.3.8
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
|
||||
golang.org/x/text v0.13.0
|
||||
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028
|
||||
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a
|
||||
modernc.org/sqlite v1.25.0
|
||||
modernc.org/sqlite v1.27.0
|
||||
source.toby3d.me/toby3d/form v0.4.0
|
||||
willnorris.com/go/microformats v1.2.0
|
||||
)
|
||||
|
@ -26,32 +26,32 @@ require (
|
|||
require (
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/google/uuid v1.4.0 // indirect
|
||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
|
||||
github.com/lestrrat-go/blackmagic v1.0.1 // indirect
|
||||
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
|
||||
github.com/lestrrat-go/httpcc v1.0.1 // indirect
|
||||
github.com/lestrrat-go/httprc v1.0.4 // indirect
|
||||
github.com/lestrrat-go/iter v1.0.2 // indirect
|
||||
github.com/lestrrat-go/option v1.0.1 // indirect
|
||||
github.com/lib/pq v1.10.6 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/segmentio/asm v1.2.0 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
go4.org/intern v0.0.0-20230525184215-6c62f75575cb // indirect
|
||||
go4.org/unsafe/assume-no-moving-gc v0.0.0-20230525183740-e7c30c78aeb2 // indirect
|
||||
golang.org/x/crypto v0.12.0 // indirect
|
||||
golang.org/x/mod v0.12.0 // indirect
|
||||
golang.org/x/net v0.14.0 // indirect
|
||||
golang.org/x/sys v0.11.0 // indirect
|
||||
golang.org/x/tools v0.11.1 // indirect
|
||||
golang.org/x/crypto v0.14.0 // indirect
|
||||
golang.org/x/mod v0.13.0 // indirect
|
||||
golang.org/x/net v0.17.0 // indirect
|
||||
golang.org/x/sys v0.13.0 // indirect
|
||||
golang.org/x/tools v0.14.0 // indirect
|
||||
lukechampine.com/uint128 v1.3.0 // indirect
|
||||
modernc.org/cc/v3 v3.41.0 // indirect
|
||||
modernc.org/ccgo/v3 v3.16.14 // indirect
|
||||
modernc.org/libc v1.24.1 // indirect
|
||||
modernc.org/ccgo/v3 v3.16.15 // indirect
|
||||
modernc.org/libc v1.29.0 // indirect
|
||||
modernc.org/mathutil v1.6.0 // indirect
|
||||
modernc.org/memory v1.6.0 // indirect
|
||||
modernc.org/memory v1.7.2 // indirect
|
||||
modernc.org/opt v0.1.3 // indirect
|
||||
modernc.org/strutil v1.1.3 // indirect
|
||||
modernc.org/strutil v1.2.0 // indirect
|
||||
modernc.org/token v1.1.0 // indirect
|
||||
)
|
||||
|
|
92
go.sum
92
go.sum
|
@ -2,8 +2,8 @@ github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20O
|
|||
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
|
||||
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
||||
github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||
github.com/brianvoe/gofakeit/v6 v6.23.1 h1:k2gX0hQpJStvixDbbw8oJOvPBg0XmHJWbSOF5JkiUHw=
|
||||
github.com/brianvoe/gofakeit/v6 v6.23.1/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8=
|
||||
github.com/brianvoe/gofakeit/v6 v6.24.0 h1:74yq7RRz/noddscZHRS2T84oHZisW9muwbb8sRnU52A=
|
||||
github.com/brianvoe/gofakeit/v6 v6.24.0/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8=
|
||||
github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc=
|
||||
github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
|
@ -20,11 +20,12 @@ github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB
|
|||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
|
||||
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
|
||||
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
|
||||
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
|
||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
|
||||
|
@ -32,26 +33,28 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:C
|
|||
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
|
||||
github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||
github.com/lestrrat-go/blackmagic v1.0.1 h1:lS5Zts+5HIC/8og6cGHb0uCcNCa3OUt1ygh3Qz2Fe80=
|
||||
github.com/lestrrat-go/blackmagic v1.0.1/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
|
||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=
|
||||
github.com/lestrrat-go/blackmagic v1.0.2/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
|
||||
github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
|
||||
github.com/lestrrat-go/httpcc v1.0.1/go.mod h1:qiltp3Mt56+55GPVCbTdM9MlqhvzyuL6W/NMDA8vA5E=
|
||||
github.com/lestrrat-go/httprc v1.0.4 h1:bAZymwoZQb+Oq8MEbyipag7iSq6YIga8Wj6GOiJGdI8=
|
||||
github.com/lestrrat-go/httprc v1.0.4/go.mod h1:mwwz3JMTPBjHUkkDv/IGJ39aALInZLrhBp0X7KGUZlo=
|
||||
github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI=
|
||||
github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4=
|
||||
github.com/lestrrat-go/jwx/v2 v2.0.11 h1:ViHMnaMeaO0qV16RZWBHM7GTrAnX2aFLVKofc7FuKLQ=
|
||||
github.com/lestrrat-go/jwx/v2 v2.0.11/go.mod h1:ZtPtMFlrfDrH2Y0iwfa3dRFn8VzwBrB+cyrm3IBWdDg=
|
||||
github.com/lestrrat-go/jwx/v2 v2.0.16 h1:TuH3dBkYTy2giQg/9D8f20znS3JtMRuQJ372boS3lWk=
|
||||
github.com/lestrrat-go/jwx/v2 v2.0.16/go.mod h1:jBHyESp4e7QxfERM0UKkQ80/94paqNIEcdEfiUYz5zE=
|
||||
github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
|
||||
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
|
||||
github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
|
||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
|
||||
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
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/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
|
@ -78,8 +81,8 @@ github.com/valyala/quicktemplate v1.7.0/go.mod h1:sqKJnoaOF88V07vkO+9FL8fb9uZg/V
|
|||
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ=
|
||||
go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
|
||||
go.etcd.io/bbolt v1.3.8 h1:xs88BrvEv273UsB79e0hcVrlUWmS0a8upikMFhSyAtA=
|
||||
go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
|
||||
go4.org/intern v0.0.0-20211027215823-ae77deb06f29/go.mod h1:cS2ma+47FKrLPdXFpr7CuxiTW3eyJbWew4qx0qtQWDA=
|
||||
go4.org/intern v0.0.0-20230525184215-6c62f75575cb h1:ae7kzL5Cfdmcecbh22ll7lYP3iuUdnfnhiPcSaDgH/8=
|
||||
go4.org/intern v0.0.0-20230525184215-6c62f75575cb/go.mod h1:Ycrt6raEcnF5FTsLiLKkhBTO6DPX3RCUCUVnks3gFJU=
|
||||
|
@ -91,16 +94,15 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
|
|||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
|
||||
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
|
||||
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
|
||||
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI=
|
||||
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
|
||||
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
||||
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
|
||||
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
|
||||
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
|
@ -109,13 +111,14 @@ golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qx
|
|||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
|
||||
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
|
||||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
|
||||
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
|
||||
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
@ -130,32 +133,33 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
||||
golang.org/x/sys v0.13.0/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-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
|
||||
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/tools v0.11.1 h1:ojD5zOW8+7dOGzdnNgersm8aPfcDjhMp12UfG93NIMc=
|
||||
golang.org/x/tools v0.11.1/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
|
||||
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
|
||||
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
|
||||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
|
||||
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
|
||||
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
@ -166,26 +170,30 @@ lukechampine.com/uint128 v1.3.0 h1:cDdUVfRwDUDovz610ABgFD17nXD4/uDgVHl2sC3+sbo=
|
|||
lukechampine.com/uint128 v1.3.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
|
||||
modernc.org/cc/v3 v3.41.0 h1:QoR1Sn3YWlmA1T4vLaKZfawdVtSiGx8H+cEojbC7v1Q=
|
||||
modernc.org/cc/v3 v3.41.0/go.mod h1:Ni4zjJYJ04CDOhG7dn640WGfwBzfE0ecX8TyMB0Fv0Y=
|
||||
modernc.org/ccgo/v3 v3.16.14 h1:af6KNtFgsVmnDYrWk3PQCS9XT6BXe7o3ZFJKkIKvXNQ=
|
||||
modernc.org/ccgo/v3 v3.16.14/go.mod h1:mPDSujUIaTNWQSG4eqKw+atqLOEbma6Ncsa94WbC9zo=
|
||||
modernc.org/ccgo/v3 v3.16.15 h1:KbDR3ZAVU+wiLyMESPtbtE/Add4elztFyfsWoNTgxS0=
|
||||
modernc.org/ccgo/v3 v3.16.15/go.mod h1:yT7B+/E2m43tmMOT51GMoM98/MtHIcQQSleGnddkUNI=
|
||||
modernc.org/ccorpus v1.11.6 h1:J16RXiiqiCgua6+ZvQot4yUuUy8zxgqbqEEUuGPlISk=
|
||||
modernc.org/ccorpus v1.11.6/go.mod h1:2gEUTrWqdpH2pXsmTM1ZkjeSrUWDpjMu2T6m29L/ErQ=
|
||||
modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM=
|
||||
modernc.org/libc v1.24.1 h1:uvJSeCKL/AgzBo2yYIPPTy82v21KgGnizcGYfBHaNuM=
|
||||
modernc.org/libc v1.24.1/go.mod h1:FmfO1RLrU3MHJfyi9eYYmZBfi/R+tqZ6+hQ3yQQUkak=
|
||||
modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM=
|
||||
modernc.org/libc v1.29.0 h1:tTFRFq69YKCF2QyGNuRUQxKBm1uZZLubf6Cjh/pVHXs=
|
||||
modernc.org/libc v1.29.0/go.mod h1:DaG/4Q3LRRdqpiLyP0C2m1B8ZMGkQ+cCgOIjEtQlYhQ=
|
||||
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
|
||||
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
|
||||
modernc.org/memory v1.6.0 h1:i6mzavxrE9a30whzMfwf7XWVODx2r5OYXvU46cirX7o=
|
||||
modernc.org/memory v1.6.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
|
||||
modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=
|
||||
modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E=
|
||||
modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
|
||||
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
|
||||
modernc.org/sqlite v1.25.0 h1:AFweiwPNd/b3BoKnBOfFm+Y260guGMF+0UFk0savqeA=
|
||||
modernc.org/sqlite v1.25.0/go.mod h1:FL3pVXie73rg3Rii6V/u5BoHlSoyeZeIgKZEgHARyCU=
|
||||
modernc.org/strutil v1.1.3 h1:fNMm+oJklMGYfU9Ylcywl0CO5O6nTfaowNsh2wpPjzY=
|
||||
modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw=
|
||||
modernc.org/sqlite v1.27.0 h1:MpKAHoyYB7xqcwnUwkuD+npwEa0fojF0B5QRbN+auJ8=
|
||||
modernc.org/sqlite v1.27.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0=
|
||||
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
|
||||
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
|
||||
modernc.org/tcl v1.15.2 h1:C4ybAYCGJw968e+Me18oW55kD/FexcHbqH2xak1ROSY=
|
||||
modernc.org/tcl v1.15.2/go.mod h1:3+k/ZaEbKrC8ePv8zJWPtBSW0V7Gg9g8rkmhI1Kfs3c=
|
||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||
modernc.org/z v1.7.3 h1:zDJf6iHjrnB+WRD88stbXokugjyc0/pB91ri1gO6LZY=
|
||||
modernc.org/z v1.7.3/go.mod h1:Ipv4tsdxZRbQyLq9Q1M6gdbkxYzdlrciF2Hi/lS7nWE=
|
||||
source.toby3d.me/toby3d/form v0.4.0 h1:p4erlFQZpWi64oHQVYsNe8FKT75ZwnExELk69ZDoQO8=
|
||||
source.toby3d.me/toby3d/form v0.4.0/go.mod h1:drlHMC+j/gb5zsttCSwx8qcYsbaRW+wFfE8bK6y+oeY=
|
||||
willnorris.com/go/microformats v1.2.0 h1:73pzJCLJM69kYE5qsLI9OOC/7sImNVOzya9EQ0+1wmM=
|
||||
|
|
|
@ -131,6 +131,7 @@ type Foo struct {
|
|||
ArrayRange []string `fakesize:"2,6"`
|
||||
Bar Bar
|
||||
Skip *string `fake:"skip"` // Set to "skip" to not generate data for
|
||||
SkipAlt *string `fake:"-"` // Set to "-" to not generate data for
|
||||
Created time.Time // Can take in a fake tag as well as a format tag
|
||||
CreatedFormat time.Time `fake:"{year}-{month}-{day}" format:"2006-01-02"`
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ func csvFunc(f *Faker, co *CSVOptions) ([]byte, error) {
|
|||
if strings.ToLower(co.Delimiter) == "tab" {
|
||||
co.Delimiter = "\t"
|
||||
}
|
||||
if co.Delimiter != "," && co.Delimiter != "\t" {
|
||||
if co.Delimiter != "," && co.Delimiter != "\t" && co.Delimiter != ";" {
|
||||
return nil, errors.New("invalid delimiter type")
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@ package gofakeit
|
|||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
|
||||
|
@ -22,13 +21,20 @@ func boolFunc(r *rand.Rand) bool { return randIntRange(r, 0, 1) == 1 }
|
|||
func UUID() string { return uuid(globalFaker.Rand) }
|
||||
|
||||
// UUID (version 4) will generate a random unique identifier based upon random numbers
|
||||
// Format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
||||
// Format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 8-4-4-4-12
|
||||
func (f *Faker) UUID() string { return uuid(f.Rand) }
|
||||
|
||||
func uuid(r *rand.Rand) string {
|
||||
version := byte(4)
|
||||
uuid := make([]byte, 16)
|
||||
io.ReadFull(r, uuid[:])
|
||||
|
||||
// Commented out due to io.ReadFull not being race condition safe
|
||||
// io.ReadFull(r, uuid[:])
|
||||
|
||||
// Read 16 random bytes
|
||||
for i := 0; i < 16; i++ {
|
||||
uuid[i] = byte(r.Intn(256))
|
||||
}
|
||||
|
||||
// Set version
|
||||
uuid[6] = (uuid[6] & 0x0f) | (version << 4)
|
||||
|
|
|
@ -79,119 +79,142 @@ func rCustom(f *Faker, t reflect.Type, v reflect.Value, tag string) error {
|
|||
}
|
||||
|
||||
fName, fParams := parseNameAndParamsFromTag(tag)
|
||||
info := GetFuncLookup(fName)
|
||||
|
||||
// Check to see if it's a replaceable lookup function
|
||||
if info := GetFuncLookup(fName); info != nil {
|
||||
// Parse map params
|
||||
mapParams := parseMapParams(info, fParams)
|
||||
|
||||
// Call function
|
||||
fValue, err := info.Generate(f.Rand, mapParams, info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create new element of expected type
|
||||
field := reflect.New(reflect.TypeOf(fValue))
|
||||
field.Elem().Set(reflect.ValueOf(fValue))
|
||||
|
||||
// Check if element is pointer if so
|
||||
// grab the underlying value
|
||||
fieldElem := field.Elem()
|
||||
if fieldElem.Kind() == reflect.Ptr {
|
||||
fieldElem = fieldElem.Elem()
|
||||
}
|
||||
|
||||
// Check if field kind is the same as the expected type
|
||||
if fieldElem.Kind() != v.Kind() {
|
||||
// return error saying the field and kinds that do not match
|
||||
return errors.New("field kind " + fieldElem.Kind().String() + " does not match expected kind " + v.Kind().String())
|
||||
}
|
||||
|
||||
// Set the value
|
||||
v.Set(fieldElem.Convert(v.Type()))
|
||||
|
||||
// If a function is called to set the struct
|
||||
// stop from going through sub fields
|
||||
return nil
|
||||
if info == nil {
|
||||
return fmt.Errorf("function %q not found", tag)
|
||||
}
|
||||
|
||||
return fmt.Errorf("function %q not found", tag)
|
||||
// Parse map params
|
||||
mapParams := parseMapParams(info, fParams)
|
||||
|
||||
// Call function
|
||||
fValue, err := info.Generate(f.Rand, mapParams, info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create new element of expected type
|
||||
field := reflect.New(reflect.TypeOf(fValue))
|
||||
field.Elem().Set(reflect.ValueOf(fValue))
|
||||
|
||||
// Check if element is pointer if so
|
||||
// grab the underlying value
|
||||
fieldElem := field.Elem()
|
||||
if fieldElem.Kind() == reflect.Ptr {
|
||||
fieldElem = fieldElem.Elem()
|
||||
}
|
||||
|
||||
// Check if field kind is the same as the expected type
|
||||
if fieldElem.Kind() != v.Kind() {
|
||||
// return error saying the field and kinds that do not match
|
||||
return errors.New("field kind " + fieldElem.Kind().String() + " does not match expected kind " + v.Kind().String())
|
||||
}
|
||||
|
||||
// Set the value
|
||||
v.Set(fieldElem.Convert(v.Type()))
|
||||
|
||||
// If a function is called to set the struct
|
||||
// stop from going through sub fields
|
||||
return nil
|
||||
}
|
||||
|
||||
func rStruct(f *Faker, t reflect.Type, v reflect.Value, tag string) error {
|
||||
// Check if tag exists, if so run custom function
|
||||
if t.Name() != "" && tag != "" {
|
||||
return rCustom(f, t, v, tag)
|
||||
} else if isFakeable(t) {
|
||||
}
|
||||
|
||||
// Check if struct is fakeable
|
||||
if isFakeable(t) {
|
||||
value, err := callFake(f, v, reflect.Struct)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
v.Set(reflect.ValueOf(value))
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
n := t.NumField()
|
||||
for i := 0; i < n; i++ {
|
||||
elementT := t.Field(i)
|
||||
elementV := v.Field(i)
|
||||
fakeTag, ok := elementT.Tag.Lookup("fake")
|
||||
// Loop through all the fields of the struct
|
||||
n := t.NumField()
|
||||
for i := 0; i < n; i++ {
|
||||
elementT := t.Field(i)
|
||||
elementV := v.Field(i)
|
||||
fakeTag, ok := elementT.Tag.Lookup("fake")
|
||||
|
||||
// Check whether or not to skip this field
|
||||
if ok && fakeTag == "skip" {
|
||||
// Do nothing, skip it
|
||||
continue
|
||||
// Check whether or not to skip this field
|
||||
if ok && fakeTag == "skip" || fakeTag == "-" {
|
||||
// Do nothing, skip it
|
||||
continue
|
||||
}
|
||||
|
||||
// Check to make sure you can set it or that it's an embedded(anonymous) field
|
||||
if !elementV.CanSet() && !elementT.Anonymous {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if reflect type is of values we can specifically set
|
||||
elemStr := elementT.Type.String()
|
||||
switch elemStr {
|
||||
case "time.Time", "*time.Time":
|
||||
// Check if element is a pointer
|
||||
elemV := elementV
|
||||
if elemStr == "*time.Time" {
|
||||
elemV = reflect.New(elementT.Type.Elem()).Elem()
|
||||
}
|
||||
|
||||
// Check to make sure you can set it or that it's an embedded(anonymous) field
|
||||
if elementV.CanSet() || elementT.Anonymous {
|
||||
// Check if reflect type is of values we can specifically set
|
||||
switch elementT.Type.String() {
|
||||
case "time.Time":
|
||||
err := rTime(f, elementT, elementV, fakeTag)
|
||||
// Run rTime on the element
|
||||
err := rTime(f, elementT, elemV, fakeTag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if elemStr == "*time.Time" {
|
||||
elementV.Set(elemV.Addr())
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if fakesize is set
|
||||
size := -1 // Set to -1 to indicate fakesize was not set
|
||||
fs, ok := elementT.Tag.Lookup("fakesize")
|
||||
if ok {
|
||||
var err error
|
||||
|
||||
// Check if size has params separated by ,
|
||||
if strings.Contains(fs, ",") {
|
||||
sizeSplit := strings.SplitN(fs, ",", 2)
|
||||
if len(sizeSplit) == 2 {
|
||||
var sizeMin int
|
||||
var sizeMax int
|
||||
|
||||
sizeMin, err = strconv.Atoi(sizeSplit[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if fakesize is set
|
||||
size := -1 // Set to -1 to indicate fakesize was not set
|
||||
fs, ok := elementT.Tag.Lookup("fakesize")
|
||||
if ok {
|
||||
var err error
|
||||
|
||||
// Check if size has params separated by ,
|
||||
if strings.Contains(fs, ",") {
|
||||
sizeSplit := strings.SplitN(fs, ",", 2)
|
||||
if len(sizeSplit) == 2 {
|
||||
var sizeMin int
|
||||
var sizeMax int
|
||||
|
||||
sizeMin, err = strconv.Atoi(sizeSplit[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sizeMax, err = strconv.Atoi(sizeSplit[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
size = f.Rand.Intn(sizeMax-sizeMin+1) + sizeMin
|
||||
}
|
||||
} else {
|
||||
size, err = strconv.Atoi(fs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sizeMax, err = strconv.Atoi(sizeSplit[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
size = f.Rand.Intn(sizeMax-sizeMin+1) + sizeMin
|
||||
}
|
||||
err := r(f, elementT.Type, elementV, fakeTag, size)
|
||||
} else {
|
||||
size, err = strconv.Atoi(fs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recursively call r() to fill in the struct
|
||||
err := r(f, elementT.Type, elementV, fakeTag, size)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
// Package cmp determines equality of values.
|
||||
//
|
||||
// This package is intended to be a more powerful and safer alternative to
|
||||
// reflect.DeepEqual for comparing whether two values are semantically equal.
|
||||
// [reflect.DeepEqual] for comparing whether two values are semantically equal.
|
||||
// It is intended to only be used in tests, as performance is not a goal and
|
||||
// it may panic if it cannot compare the values. Its propensity towards
|
||||
// panicking means that its unsuitable for production environments where a
|
||||
|
@ -18,16 +18,17 @@
|
|||
// For example, an equality function may report floats as equal so long as
|
||||
// they are within some tolerance of each other.
|
||||
//
|
||||
// - Types with an Equal method may use that method to determine equality.
|
||||
// This allows package authors to determine the equality operation
|
||||
// for the types that they define.
|
||||
// - Types with an Equal method (e.g., [time.Time.Equal]) may use that method
|
||||
// to determine equality. This allows package authors to determine
|
||||
// the equality operation for the types that they define.
|
||||
//
|
||||
// - If no custom equality functions are used and no Equal method is defined,
|
||||
// equality is determined by recursively comparing the primitive kinds on
|
||||
// both values, much like reflect.DeepEqual. Unlike reflect.DeepEqual,
|
||||
// both values, much like [reflect.DeepEqual]. Unlike [reflect.DeepEqual],
|
||||
// unexported fields are not compared by default; they result in panics
|
||||
// unless suppressed by using an Ignore option (see cmpopts.IgnoreUnexported)
|
||||
// or explicitly compared using the Exporter option.
|
||||
// unless suppressed by using an [Ignore] option
|
||||
// (see [github.com/google/go-cmp/cmp/cmpopts.IgnoreUnexported])
|
||||
// or explicitly compared using the [Exporter] option.
|
||||
package cmp
|
||||
|
||||
import (
|
||||
|
@ -45,14 +46,14 @@ import (
|
|||
// Equal reports whether x and y are equal by recursively applying the
|
||||
// following rules in the given order to x and y and all of their sub-values:
|
||||
//
|
||||
// - Let S be the set of all Ignore, Transformer, and Comparer options that
|
||||
// - Let S be the set of all [Ignore], [Transformer], and [Comparer] options that
|
||||
// remain after applying all path filters, value filters, and type filters.
|
||||
// If at least one Ignore exists in S, then the comparison is ignored.
|
||||
// If the number of Transformer and Comparer options in S is non-zero,
|
||||
// If at least one [Ignore] exists in S, then the comparison is ignored.
|
||||
// If the number of [Transformer] and [Comparer] options in S is non-zero,
|
||||
// then Equal panics because it is ambiguous which option to use.
|
||||
// If S contains a single Transformer, then use that to transform
|
||||
// If S contains a single [Transformer], then use that to transform
|
||||
// the current values and recursively call Equal on the output values.
|
||||
// If S contains a single Comparer, then use that to compare the current values.
|
||||
// If S contains a single [Comparer], then use that to compare the current values.
|
||||
// Otherwise, evaluation proceeds to the next rule.
|
||||
//
|
||||
// - If the values have an Equal method of the form "(T) Equal(T) bool" or
|
||||
|
@ -66,21 +67,22 @@ import (
|
|||
// Functions are only equal if they are both nil, otherwise they are unequal.
|
||||
//
|
||||
// Structs are equal if recursively calling Equal on all fields report equal.
|
||||
// If a struct contains unexported fields, Equal panics unless an Ignore option
|
||||
// (e.g., cmpopts.IgnoreUnexported) ignores that field or the Exporter option
|
||||
// explicitly permits comparing the unexported field.
|
||||
// If a struct contains unexported fields, Equal panics unless an [Ignore] option
|
||||
// (e.g., [github.com/google/go-cmp/cmp/cmpopts.IgnoreUnexported]) ignores that field
|
||||
// or the [Exporter] option explicitly permits comparing the unexported field.
|
||||
//
|
||||
// Slices are equal if they are both nil or both non-nil, where recursively
|
||||
// calling Equal on all non-ignored slice or array elements report equal.
|
||||
// Empty non-nil slices and nil slices are not equal; to equate empty slices,
|
||||
// consider using cmpopts.EquateEmpty.
|
||||
// consider using [github.com/google/go-cmp/cmp/cmpopts.EquateEmpty].
|
||||
//
|
||||
// Maps are equal if they are both nil or both non-nil, where recursively
|
||||
// calling Equal on all non-ignored map entries report equal.
|
||||
// Map keys are equal according to the == operator.
|
||||
// To use custom comparisons for map keys, consider using cmpopts.SortMaps.
|
||||
// To use custom comparisons for map keys, consider using
|
||||
// [github.com/google/go-cmp/cmp/cmpopts.SortMaps].
|
||||
// Empty non-nil maps and nil maps are not equal; to equate empty maps,
|
||||
// consider using cmpopts.EquateEmpty.
|
||||
// consider using [github.com/google/go-cmp/cmp/cmpopts.EquateEmpty].
|
||||
//
|
||||
// Pointers and interfaces are equal if they are both nil or both non-nil,
|
||||
// where they have the same underlying concrete type and recursively
|
||||
|
|
|
@ -2,9 +2,6 @@
|
|||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !purego
|
||||
// +build !purego
|
||||
|
||||
package cmp
|
||||
|
||||
import (
|
||||
|
@ -12,8 +9,6 @@ import (
|
|||
"unsafe"
|
||||
)
|
||||
|
||||
const supportExporters = true
|
||||
|
||||
// retrieveUnexportedField uses unsafe to forcibly retrieve any field from
|
||||
// a struct such that the value has read-write permissions.
|
||||
//
|
|
@ -1,16 +0,0 @@
|
|||
// Copyright 2017, 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.
|
||||
|
||||
//go:build purego
|
||||
// +build purego
|
||||
|
||||
package cmp
|
||||
|
||||
import "reflect"
|
||||
|
||||
const supportExporters = false
|
||||
|
||||
func retrieveUnexportedField(reflect.Value, reflect.StructField, bool) reflect.Value {
|
||||
panic("no support for forcibly accessing unexported fields")
|
||||
}
|
|
@ -2,9 +2,6 @@
|
|||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build !purego
|
||||
// +build !purego
|
||||
|
||||
package value
|
||||
|
||||
import (
|
|
@ -1,34 +0,0 @@
|
|||
// Copyright 2018, 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.
|
||||
|
||||
//go:build purego
|
||||
// +build purego
|
||||
|
||||
package value
|
||||
|
||||
import "reflect"
|
||||
|
||||
// Pointer is an opaque typed pointer and is guaranteed to be comparable.
|
||||
type Pointer struct {
|
||||
p uintptr
|
||||
t reflect.Type
|
||||
}
|
||||
|
||||
// PointerOf returns a Pointer from v, which must be a
|
||||
// reflect.Ptr, reflect.Slice, or reflect.Map.
|
||||
func PointerOf(v reflect.Value) Pointer {
|
||||
// NOTE: Storing a pointer as an uintptr is technically incorrect as it
|
||||
// assumes that the GC implementation does not use a moving collector.
|
||||
return Pointer{v.Pointer(), v.Type()}
|
||||
}
|
||||
|
||||
// IsNil reports whether the pointer is nil.
|
||||
func (p Pointer) IsNil() bool {
|
||||
return p.p == 0
|
||||
}
|
||||
|
||||
// Uintptr returns the pointer as a uintptr.
|
||||
func (p Pointer) Uintptr() uintptr {
|
||||
return p.p
|
||||
}
|
|
@ -13,15 +13,15 @@ import (
|
|||
"github.com/google/go-cmp/cmp/internal/function"
|
||||
)
|
||||
|
||||
// Option configures for specific behavior of Equal and Diff. In particular,
|
||||
// the fundamental Option functions (Ignore, Transformer, and Comparer),
|
||||
// Option configures for specific behavior of [Equal] and [Diff]. In particular,
|
||||
// the fundamental Option functions ([Ignore], [Transformer], and [Comparer]),
|
||||
// configure how equality is determined.
|
||||
//
|
||||
// The fundamental options may be composed with filters (FilterPath and
|
||||
// FilterValues) to control the scope over which they are applied.
|
||||
// The fundamental options may be composed with filters ([FilterPath] and
|
||||
// [FilterValues]) to control the scope over which they are applied.
|
||||
//
|
||||
// The cmp/cmpopts package provides helper functions for creating options that
|
||||
// may be used with Equal and Diff.
|
||||
// The [github.com/google/go-cmp/cmp/cmpopts] package provides helper functions
|
||||
// for creating options that may be used with [Equal] and [Diff].
|
||||
type Option interface {
|
||||
// filter applies all filters and returns the option that remains.
|
||||
// Each option may only read s.curPath and call s.callTTBFunc.
|
||||
|
@ -56,9 +56,9 @@ type core struct{}
|
|||
|
||||
func (core) isCore() {}
|
||||
|
||||
// Options is a list of Option values that also satisfies the Option interface.
|
||||
// Options is a list of [Option] values that also satisfies the [Option] interface.
|
||||
// Helper comparison packages may return an Options value when packing multiple
|
||||
// Option values into a single Option. When this package processes an Options,
|
||||
// [Option] values into a single [Option]. When this package processes an Options,
|
||||
// it will be implicitly expanded into a flat list.
|
||||
//
|
||||
// Applying a filter on an Options is equivalent to applying that same filter
|
||||
|
@ -105,16 +105,16 @@ func (opts Options) String() string {
|
|||
return fmt.Sprintf("Options{%s}", strings.Join(ss, ", "))
|
||||
}
|
||||
|
||||
// FilterPath returns a new Option where opt is only evaluated if filter f
|
||||
// returns true for the current Path in the value tree.
|
||||
// FilterPath returns a new [Option] where opt is only evaluated if filter f
|
||||
// returns true for the current [Path] in the value tree.
|
||||
//
|
||||
// This filter is called even if a slice element or map entry is missing and
|
||||
// provides an opportunity to ignore such cases. The filter function must be
|
||||
// symmetric such that the filter result is identical regardless of whether the
|
||||
// missing value is from x or y.
|
||||
//
|
||||
// The option passed in may be an Ignore, Transformer, Comparer, Options, or
|
||||
// a previously filtered Option.
|
||||
// The option passed in may be an [Ignore], [Transformer], [Comparer], [Options], or
|
||||
// a previously filtered [Option].
|
||||
func FilterPath(f func(Path) bool, opt Option) Option {
|
||||
if f == nil {
|
||||
panic("invalid path filter function")
|
||||
|
@ -142,7 +142,7 @@ func (f pathFilter) String() string {
|
|||
return fmt.Sprintf("FilterPath(%s, %v)", function.NameOf(reflect.ValueOf(f.fnc)), f.opt)
|
||||
}
|
||||
|
||||
// FilterValues returns a new Option where opt is only evaluated if filter f,
|
||||
// FilterValues returns a new [Option] where opt is only evaluated if filter f,
|
||||
// which is a function of the form "func(T, T) bool", returns true for the
|
||||
// current pair of values being compared. If either value is invalid or
|
||||
// the type of the values is not assignable to T, then this filter implicitly
|
||||
|
@ -154,8 +154,8 @@ func (f pathFilter) String() string {
|
|||
// If T is an interface, it is possible that f is called with two values with
|
||||
// different concrete types that both implement T.
|
||||
//
|
||||
// The option passed in may be an Ignore, Transformer, Comparer, Options, or
|
||||
// a previously filtered Option.
|
||||
// The option passed in may be an [Ignore], [Transformer], [Comparer], [Options], or
|
||||
// a previously filtered [Option].
|
||||
func FilterValues(f interface{}, opt Option) Option {
|
||||
v := reflect.ValueOf(f)
|
||||
if !function.IsType(v.Type(), function.ValueFilter) || v.IsNil() {
|
||||
|
@ -192,9 +192,9 @@ func (f valuesFilter) String() string {
|
|||
return fmt.Sprintf("FilterValues(%s, %v)", function.NameOf(f.fnc), f.opt)
|
||||
}
|
||||
|
||||
// Ignore is an Option that causes all comparisons to be ignored.
|
||||
// This value is intended to be combined with FilterPath or FilterValues.
|
||||
// It is an error to pass an unfiltered Ignore option to Equal.
|
||||
// Ignore is an [Option] that causes all comparisons to be ignored.
|
||||
// This value is intended to be combined with [FilterPath] or [FilterValues].
|
||||
// It is an error to pass an unfiltered Ignore option to [Equal].
|
||||
func Ignore() Option { return ignore{} }
|
||||
|
||||
type ignore struct{ core }
|
||||
|
@ -234,6 +234,8 @@ func (validator) apply(s *state, vx, vy reflect.Value) {
|
|||
name = fmt.Sprintf("%q.%v", t.PkgPath(), t.Name()) // e.g., "path/to/package".MyType
|
||||
if _, ok := reflect.New(t).Interface().(error); ok {
|
||||
help = "consider using cmpopts.EquateErrors to compare error values"
|
||||
} else if t.Comparable() {
|
||||
help = "consider using cmpopts.EquateComparable to compare comparable Go types"
|
||||
}
|
||||
} else {
|
||||
// Unnamed type with unexported fields. Derive PkgPath from field.
|
||||
|
@ -254,7 +256,7 @@ const identRx = `[_\p{L}][_\p{L}\p{N}]*`
|
|||
|
||||
var identsRx = regexp.MustCompile(`^` + identRx + `(\.` + identRx + `)*$`)
|
||||
|
||||
// Transformer returns an Option that applies a transformation function that
|
||||
// Transformer returns an [Option] that applies a transformation function that
|
||||
// converts values of a certain type into that of another.
|
||||
//
|
||||
// The transformer f must be a function "func(T) R" that converts values of
|
||||
|
@ -265,13 +267,14 @@ var identsRx = regexp.MustCompile(`^` + identRx + `(\.` + identRx + `)*$`)
|
|||
// same transform to the output of itself (e.g., in the case where the
|
||||
// input and output types are the same), an implicit filter is added such that
|
||||
// a transformer is applicable only if that exact transformer is not already
|
||||
// in the tail of the Path since the last non-Transform step.
|
||||
// in the tail of the [Path] since the last non-[Transform] step.
|
||||
// For situations where the implicit filter is still insufficient,
|
||||
// consider using cmpopts.AcyclicTransformer, which adds a filter
|
||||
// to prevent the transformer from being recursively applied upon itself.
|
||||
// consider using [github.com/google/go-cmp/cmp/cmpopts.AcyclicTransformer],
|
||||
// which adds a filter to prevent the transformer from
|
||||
// being recursively applied upon itself.
|
||||
//
|
||||
// The name is a user provided label that is used as the Transform.Name in the
|
||||
// transformation PathStep (and eventually shown in the Diff output).
|
||||
// The name is a user provided label that is used as the [Transform.Name] in the
|
||||
// transformation [PathStep] (and eventually shown in the [Diff] output).
|
||||
// The name must be a valid identifier or qualified identifier in Go syntax.
|
||||
// If empty, an arbitrary name is used.
|
||||
func Transformer(name string, f interface{}) Option {
|
||||
|
@ -329,7 +332,7 @@ func (tr transformer) String() string {
|
|||
return fmt.Sprintf("Transformer(%s, %s)", tr.name, function.NameOf(tr.fnc))
|
||||
}
|
||||
|
||||
// Comparer returns an Option that determines whether two values are equal
|
||||
// Comparer returns an [Option] that determines whether two values are equal
|
||||
// to each other.
|
||||
//
|
||||
// The comparer f must be a function "func(T, T) bool" and is implicitly
|
||||
|
@ -377,35 +380,32 @@ func (cm comparer) String() string {
|
|||
return fmt.Sprintf("Comparer(%s)", function.NameOf(cm.fnc))
|
||||
}
|
||||
|
||||
// Exporter returns an Option that specifies whether Equal is allowed to
|
||||
// Exporter returns an [Option] that specifies whether [Equal] is allowed to
|
||||
// introspect into the unexported fields of certain struct types.
|
||||
//
|
||||
// Users of this option must understand that comparing on unexported fields
|
||||
// from external packages is not safe since changes in the internal
|
||||
// implementation of some external package may cause the result of Equal
|
||||
// implementation of some external package may cause the result of [Equal]
|
||||
// to unexpectedly change. However, it may be valid to use this option on types
|
||||
// defined in an internal package where the semantic meaning of an unexported
|
||||
// field is in the control of the user.
|
||||
//
|
||||
// In many cases, a custom Comparer should be used instead that defines
|
||||
// In many cases, a custom [Comparer] should be used instead that defines
|
||||
// equality as a function of the public API of a type rather than the underlying
|
||||
// unexported implementation.
|
||||
//
|
||||
// For example, the reflect.Type documentation defines equality to be determined
|
||||
// For example, the [reflect.Type] documentation defines equality to be determined
|
||||
// by the == operator on the interface (essentially performing a shallow pointer
|
||||
// comparison) and most attempts to compare *regexp.Regexp types are interested
|
||||
// comparison) and most attempts to compare *[regexp.Regexp] types are interested
|
||||
// in only checking that the regular expression strings are equal.
|
||||
// Both of these are accomplished using Comparers:
|
||||
// Both of these are accomplished using [Comparer] options:
|
||||
//
|
||||
// Comparer(func(x, y reflect.Type) bool { return x == y })
|
||||
// Comparer(func(x, y *regexp.Regexp) bool { return x.String() == y.String() })
|
||||
//
|
||||
// In other cases, the cmpopts.IgnoreUnexported option can be used to ignore
|
||||
// all unexported fields on specified struct types.
|
||||
// In other cases, the [github.com/google/go-cmp/cmp/cmpopts.IgnoreUnexported]
|
||||
// option can be used to ignore all unexported fields on specified struct types.
|
||||
func Exporter(f func(reflect.Type) bool) Option {
|
||||
if !supportExporters {
|
||||
panic("Exporter is not supported on purego builds")
|
||||
}
|
||||
return exporter(f)
|
||||
}
|
||||
|
||||
|
@ -415,10 +415,10 @@ func (exporter) filter(_ *state, _ reflect.Type, _, _ reflect.Value) applicableO
|
|||
panic("not implemented")
|
||||
}
|
||||
|
||||
// AllowUnexported returns an Options that allows Equal to forcibly introspect
|
||||
// AllowUnexported returns an [Option] that allows [Equal] to forcibly introspect
|
||||
// unexported fields of the specified struct types.
|
||||
//
|
||||
// See Exporter for the proper use of this option.
|
||||
// See [Exporter] for the proper use of this option.
|
||||
func AllowUnexported(types ...interface{}) Option {
|
||||
m := make(map[reflect.Type]bool)
|
||||
for _, typ := range types {
|
||||
|
@ -432,7 +432,7 @@ func AllowUnexported(types ...interface{}) Option {
|
|||
}
|
||||
|
||||
// Result represents the comparison result for a single node and
|
||||
// is provided by cmp when calling Report (see Reporter).
|
||||
// is provided by cmp when calling Report (see [Reporter]).
|
||||
type Result struct {
|
||||
_ [0]func() // Make Result incomparable
|
||||
flags resultFlags
|
||||
|
@ -445,7 +445,7 @@ func (r Result) Equal() bool {
|
|||
}
|
||||
|
||||
// ByIgnore reports whether the node is equal because it was ignored.
|
||||
// This never reports true if Equal reports false.
|
||||
// This never reports true if [Result.Equal] reports false.
|
||||
func (r Result) ByIgnore() bool {
|
||||
return r.flags&reportByIgnore != 0
|
||||
}
|
||||
|
@ -455,7 +455,7 @@ func (r Result) ByMethod() bool {
|
|||
return r.flags&reportByMethod != 0
|
||||
}
|
||||
|
||||
// ByFunc reports whether a Comparer function determined equality.
|
||||
// ByFunc reports whether a [Comparer] function determined equality.
|
||||
func (r Result) ByFunc() bool {
|
||||
return r.flags&reportByFunc != 0
|
||||
}
|
||||
|
@ -478,7 +478,7 @@ const (
|
|||
reportByCycle
|
||||
)
|
||||
|
||||
// Reporter is an Option that can be passed to Equal. When Equal traverses
|
||||
// Reporter is an [Option] that can be passed to [Equal]. When [Equal] traverses
|
||||
// the value trees, it calls PushStep as it descends into each node in the
|
||||
// tree and PopStep as it ascend out of the node. The leaves of the tree are
|
||||
// either compared (determined to be equal or not equal) or ignored and reported
|
||||
|
|
|
@ -14,9 +14,9 @@ import (
|
|||
"github.com/google/go-cmp/cmp/internal/value"
|
||||
)
|
||||
|
||||
// Path is a list of PathSteps describing the sequence of operations to get
|
||||
// Path is a list of [PathStep] describing the sequence of operations to get
|
||||
// from some root type to the current position in the value tree.
|
||||
// The first Path element is always an operation-less PathStep that exists
|
||||
// The first Path element is always an operation-less [PathStep] that exists
|
||||
// simply to identify the initial type.
|
||||
//
|
||||
// When traversing structs with embedded structs, the embedded struct will
|
||||
|
@ -29,8 +29,13 @@ type Path []PathStep
|
|||
// a value's tree structure. Users of this package never need to implement
|
||||
// these types as values of this type will be returned by this package.
|
||||
//
|
||||
// Implementations of this interface are
|
||||
// StructField, SliceIndex, MapIndex, Indirect, TypeAssertion, and Transform.
|
||||
// Implementations of this interface:
|
||||
// - [StructField]
|
||||
// - [SliceIndex]
|
||||
// - [MapIndex]
|
||||
// - [Indirect]
|
||||
// - [TypeAssertion]
|
||||
// - [Transform]
|
||||
type PathStep interface {
|
||||
String() string
|
||||
|
||||
|
@ -70,8 +75,9 @@ func (pa *Path) pop() {
|
|||
*pa = (*pa)[:len(*pa)-1]
|
||||
}
|
||||
|
||||
// Last returns the last PathStep in the Path.
|
||||
// If the path is empty, this returns a non-nil PathStep that reports a nil Type.
|
||||
// Last returns the last [PathStep] in the Path.
|
||||
// If the path is empty, this returns a non-nil [PathStep]
|
||||
// that reports a nil [PathStep.Type].
|
||||
func (pa Path) Last() PathStep {
|
||||
return pa.Index(-1)
|
||||
}
|
||||
|
@ -79,7 +85,8 @@ func (pa Path) Last() PathStep {
|
|||
// Index returns the ith step in the Path and supports negative indexing.
|
||||
// A negative index starts counting from the tail of the Path such that -1
|
||||
// refers to the last step, -2 refers to the second-to-last step, and so on.
|
||||
// If index is invalid, this returns a non-nil PathStep that reports a nil Type.
|
||||
// If index is invalid, this returns a non-nil [PathStep]
|
||||
// that reports a nil [PathStep.Type].
|
||||
func (pa Path) Index(i int) PathStep {
|
||||
if i < 0 {
|
||||
i = len(pa) + i
|
||||
|
@ -168,7 +175,8 @@ func (ps pathStep) String() string {
|
|||
return fmt.Sprintf("{%s}", s)
|
||||
}
|
||||
|
||||
// StructField represents a struct field access on a field called Name.
|
||||
// StructField is a [PathStep] that represents a struct field access
|
||||
// on a field called [StructField.Name].
|
||||
type StructField struct{ *structField }
|
||||
type structField struct {
|
||||
pathStep
|
||||
|
@ -204,10 +212,11 @@ func (sf StructField) String() string { return fmt.Sprintf(".%s", sf.name) }
|
|||
func (sf StructField) Name() string { return sf.name }
|
||||
|
||||
// Index is the index of the field in the parent struct type.
|
||||
// See reflect.Type.Field.
|
||||
// See [reflect.Type.Field].
|
||||
func (sf StructField) Index() int { return sf.idx }
|
||||
|
||||
// SliceIndex is an index operation on a slice or array at some index Key.
|
||||
// SliceIndex is a [PathStep] that represents an index operation on
|
||||
// a slice or array at some index [SliceIndex.Key].
|
||||
type SliceIndex struct{ *sliceIndex }
|
||||
type sliceIndex struct {
|
||||
pathStep
|
||||
|
@ -247,12 +256,12 @@ func (si SliceIndex) Key() int {
|
|||
// all of the indexes to be shifted. If an index is -1, then that
|
||||
// indicates that the element does not exist in the associated slice.
|
||||
//
|
||||
// Key is guaranteed to return -1 if and only if the indexes returned
|
||||
// by SplitKeys are not the same. SplitKeys will never return -1 for
|
||||
// [SliceIndex.Key] is guaranteed to return -1 if and only if the indexes
|
||||
// returned by SplitKeys are not the same. SplitKeys will never return -1 for
|
||||
// both indexes.
|
||||
func (si SliceIndex) SplitKeys() (ix, iy int) { return si.xkey, si.ykey }
|
||||
|
||||
// MapIndex is an index operation on a map at some index Key.
|
||||
// MapIndex is a [PathStep] that represents an index operation on a map at some index Key.
|
||||
type MapIndex struct{ *mapIndex }
|
||||
type mapIndex struct {
|
||||
pathStep
|
||||
|
@ -266,7 +275,7 @@ func (mi MapIndex) String() string { return fmt.Sprintf("[%#v]",
|
|||
// Key is the value of the map key.
|
||||
func (mi MapIndex) Key() reflect.Value { return mi.key }
|
||||
|
||||
// Indirect represents pointer indirection on the parent type.
|
||||
// Indirect is a [PathStep] that represents pointer indirection on the parent type.
|
||||
type Indirect struct{ *indirect }
|
||||
type indirect struct {
|
||||
pathStep
|
||||
|
@ -276,7 +285,7 @@ func (in Indirect) Type() reflect.Type { return in.typ }
|
|||
func (in Indirect) Values() (vx, vy reflect.Value) { return in.vx, in.vy }
|
||||
func (in Indirect) String() string { return "*" }
|
||||
|
||||
// TypeAssertion represents a type assertion on an interface.
|
||||
// TypeAssertion is a [PathStep] that represents a type assertion on an interface.
|
||||
type TypeAssertion struct{ *typeAssertion }
|
||||
type typeAssertion struct {
|
||||
pathStep
|
||||
|
@ -286,7 +295,8 @@ func (ta TypeAssertion) Type() reflect.Type { return ta.typ }
|
|||
func (ta TypeAssertion) Values() (vx, vy reflect.Value) { return ta.vx, ta.vy }
|
||||
func (ta TypeAssertion) String() string { return fmt.Sprintf(".(%v)", value.TypeString(ta.typ, false)) }
|
||||
|
||||
// Transform is a transformation from the parent type to the current type.
|
||||
// Transform is a [PathStep] that represents a transformation
|
||||
// from the parent type to the current type.
|
||||
type Transform struct{ *transform }
|
||||
type transform struct {
|
||||
pathStep
|
||||
|
@ -297,13 +307,13 @@ func (tf Transform) Type() reflect.Type { return tf.typ }
|
|||
func (tf Transform) Values() (vx, vy reflect.Value) { return tf.vx, tf.vy }
|
||||
func (tf Transform) String() string { return fmt.Sprintf("%s()", tf.trans.name) }
|
||||
|
||||
// Name is the name of the Transformer.
|
||||
// Name is the name of the [Transformer].
|
||||
func (tf Transform) Name() string { return tf.trans.name }
|
||||
|
||||
// Func is the function pointer to the transformer function.
|
||||
func (tf Transform) Func() reflect.Value { return tf.trans.fnc }
|
||||
|
||||
// Option returns the originally constructed Transformer option.
|
||||
// Option returns the originally constructed [Transformer] option.
|
||||
// The == operator can be used to detect the exact option used.
|
||||
func (tf Transform) Option() Option { return tf.trans }
|
||||
|
||||
|
|
|
@ -199,7 +199,7 @@ func (opts formatOptions) FormatValue(v reflect.Value, parentKind reflect.Kind,
|
|||
break
|
||||
}
|
||||
sf := t.Field(i)
|
||||
if supportExporters && !isExported(sf.Name) {
|
||||
if !isExported(sf.Name) {
|
||||
vv = retrieveUnexportedField(v, sf, true)
|
||||
}
|
||||
s := opts.WithTypeMode(autoType).FormatValue(vv, t.Kind(), ptrs)
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
language: go
|
||||
|
||||
go:
|
||||
- 1.4.3
|
||||
- 1.5.3
|
||||
- tip
|
||||
|
||||
script:
|
||||
- go test -v ./...
|
|
@ -0,0 +1,21 @@
|
|||
# Changelog
|
||||
|
||||
## [1.4.0](https://github.com/google/uuid/compare/v1.3.1...v1.4.0) (2023-10-26)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* UUIDs slice type with Strings() convenience method ([#133](https://github.com/google/uuid/issues/133)) ([cd5fbbd](https://github.com/google/uuid/commit/cd5fbbdd02f3e3467ac18940e07e062be1f864b4))
|
||||
|
||||
### Fixes
|
||||
|
||||
* Clarify that Parse's job is to parse but not necessarily validate strings. (Documents current behavior)
|
||||
|
||||
## [1.3.1](https://github.com/google/uuid/compare/v1.3.0...v1.3.1) (2023-08-18)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* Use .EqualFold() to parse urn prefixed UUIDs ([#118](https://github.com/google/uuid/issues/118)) ([574e687](https://github.com/google/uuid/commit/574e6874943741fb99d41764c705173ada5293f0))
|
||||
|
||||
## Changelog
|
|
@ -2,6 +2,22 @@
|
|||
|
||||
We definitely welcome patches and contribution to this project!
|
||||
|
||||
### Tips
|
||||
|
||||
Commits must be formatted according to the [Conventional Commits Specification](https://www.conventionalcommits.org).
|
||||
|
||||
Always try to include a test case! If it is not possible or not necessary,
|
||||
please explain why in the pull request description.
|
||||
|
||||
### Releasing
|
||||
|
||||
Commits that would precipitate a SemVer change, as described in the Conventional
|
||||
Commits Specification, will trigger [`release-please`](https://github.com/google-github-actions/release-please-action)
|
||||
to create a release candidate pull request. Once submitted, `release-please`
|
||||
will create a release.
|
||||
|
||||
For tips on how to work with `release-please`, see its documentation.
|
||||
|
||||
### Legal requirements
|
||||
|
||||
In order to protect both you and ourselves, you will need to sign the
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# uuid ![build status](https://travis-ci.org/google/uuid.svg?branch=master)
|
||||
# uuid
|
||||
The uuid package generates and inspects UUIDs based on
|
||||
[RFC 4122](http://tools.ietf.org/html/rfc4122)
|
||||
[RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122)
|
||||
and DCE 1.1: Authentication and Security Services.
|
||||
|
||||
This package is based on the github.com/pborman/uuid package (previously named
|
||||
|
@ -9,10 +9,12 @@ a UUID is a 16 byte array rather than a byte slice. One loss due to this
|
|||
change is the ability to represent an invalid UUID (vs a NIL UUID).
|
||||
|
||||
###### Install
|
||||
`go get github.com/google/uuid`
|
||||
```sh
|
||||
go get github.com/google/uuid
|
||||
```
|
||||
|
||||
###### Documentation
|
||||
[![GoDoc](https://godoc.org/github.com/google/uuid?status.svg)](http://godoc.org/github.com/google/uuid)
|
||||
[![Go Reference](https://pkg.go.dev/badge/github.com/google/uuid.svg)](https://pkg.go.dev/github.com/google/uuid)
|
||||
|
||||
Full `go doc` style documentation for the package can be viewed online without
|
||||
installing this package by using the GoDoc site here:
|
||||
|
|
|
@ -7,6 +7,6 @@
|
|||
package uuid
|
||||
|
||||
// getHardwareInterface returns nil values for the JS version of the code.
|
||||
// This remvoves the "net" dependency, because it is not used in the browser.
|
||||
// This removes the "net" dependency, because it is not used in the browser.
|
||||
// Using the "net" library inflates the size of the transpiled JS code by 673k bytes.
|
||||
func getHardwareInterface(name string) (string, []byte) { return "", nil }
|
||||
|
|
|
@ -56,11 +56,15 @@ func IsInvalidLengthError(err error) bool {
|
|||
return ok
|
||||
}
|
||||
|
||||
// Parse decodes s into a UUID or returns an error. Both the standard UUID
|
||||
// forms of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and
|
||||
// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded as well as the
|
||||
// Microsoft encoding {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} and the raw hex
|
||||
// encoding: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
|
||||
// Parse decodes s into a UUID or returns an error if it cannot be parsed. Both
|
||||
// the standard UUID forms defined in RFC 4122
|
||||
// (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and
|
||||
// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) are decoded. In addition,
|
||||
// Parse accepts non-standard strings such as the raw hex encoding
|
||||
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx and 38 byte "Microsoft style" encodings,
|
||||
// e.g. {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}. Only the middle 36 bytes are
|
||||
// examined in the latter case. Parse should not be used to validate strings as
|
||||
// it parses non-standard encodings as indicated above.
|
||||
func Parse(s string) (UUID, error) {
|
||||
var uuid UUID
|
||||
switch len(s) {
|
||||
|
@ -69,7 +73,7 @@ func Parse(s string) (UUID, error) {
|
|||
|
||||
// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
||||
case 36 + 9:
|
||||
if strings.ToLower(s[:9]) != "urn:uuid:" {
|
||||
if !strings.EqualFold(s[:9], "urn:uuid:") {
|
||||
return uuid, fmt.Errorf("invalid urn prefix: %q", s[:9])
|
||||
}
|
||||
s = s[9:]
|
||||
|
@ -101,7 +105,8 @@ func Parse(s string) (UUID, error) {
|
|||
9, 11,
|
||||
14, 16,
|
||||
19, 21,
|
||||
24, 26, 28, 30, 32, 34} {
|
||||
24, 26, 28, 30, 32, 34,
|
||||
} {
|
||||
v, ok := xtob(s[x], s[x+1])
|
||||
if !ok {
|
||||
return uuid, errors.New("invalid UUID format")
|
||||
|
@ -117,7 +122,7 @@ func ParseBytes(b []byte) (UUID, error) {
|
|||
switch len(b) {
|
||||
case 36: // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
||||
case 36 + 9: // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
||||
if !bytes.Equal(bytes.ToLower(b[:9]), []byte("urn:uuid:")) {
|
||||
if !bytes.EqualFold(b[:9], []byte("urn:uuid:")) {
|
||||
return uuid, fmt.Errorf("invalid urn prefix: %q", b[:9])
|
||||
}
|
||||
b = b[9:]
|
||||
|
@ -145,7 +150,8 @@ func ParseBytes(b []byte) (UUID, error) {
|
|||
9, 11,
|
||||
14, 16,
|
||||
19, 21,
|
||||
24, 26, 28, 30, 32, 34} {
|
||||
24, 26, 28, 30, 32, 34,
|
||||
} {
|
||||
v, ok := xtob(b[x], b[x+1])
|
||||
if !ok {
|
||||
return uuid, errors.New("invalid UUID format")
|
||||
|
@ -292,3 +298,15 @@ func DisableRandPool() {
|
|||
poolMu.Lock()
|
||||
poolPos = randPoolSize
|
||||
}
|
||||
|
||||
// UUIDs is a slice of UUID types.
|
||||
type UUIDs []UUID
|
||||
|
||||
// Strings returns a string slice containing the string form of each UUID in uuids.
|
||||
func (uuids UUIDs) Strings() []string {
|
||||
var uuidStrs = make([]string, len(uuids))
|
||||
for i, uuid := range uuids {
|
||||
uuidStrs[i] = uuid.String()
|
||||
}
|
||||
return uuidStrs
|
||||
}
|
||||
|
|
|
@ -5,6 +5,43 @@ import (
|
|||
"reflect"
|
||||
)
|
||||
|
||||
// AssignField is a convenience function to assign a value to
|
||||
// an optional struct field. In Go, an optional struct field is
|
||||
// usually denoted by a pointer to T instead of T:
|
||||
//
|
||||
// type Object struct {
|
||||
// Optional *T
|
||||
// }
|
||||
//
|
||||
// This gets a bit cumbersome when you want to assign literals
|
||||
// or you do not want to worry about taking the address of a
|
||||
// variable.
|
||||
//
|
||||
// Object.Optional = &"foo" // doesn't compile!
|
||||
//
|
||||
// Instead you can use this function to do it in one line:
|
||||
//
|
||||
// blackmagic.AssignOptionalField(&Object.Optionl, "foo")
|
||||
func AssignOptionalField(dst, src interface{}) error {
|
||||
dstRV := reflect.ValueOf(dst)
|
||||
srcRV := reflect.ValueOf(src)
|
||||
if dstRV.Kind() != reflect.Pointer || dstRV.Elem().Kind() != reflect.Pointer {
|
||||
return fmt.Errorf(`dst must be a pointer to a field that is turn a pointer of src (%T)`, src)
|
||||
}
|
||||
|
||||
if !dstRV.Elem().CanSet() {
|
||||
return fmt.Errorf(`dst (%T) is not assignable`, dstRV.Elem().Interface())
|
||||
}
|
||||
if !reflect.PtrTo(srcRV.Type()).AssignableTo(dstRV.Elem().Type()) {
|
||||
return fmt.Errorf(`cannot assign src (%T) to dst (%T)`, src, dst)
|
||||
}
|
||||
|
||||
ptr := reflect.New(srcRV.Type())
|
||||
ptr.Elem().Set(srcRV)
|
||||
dstRV.Elem().Set(ptr)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AssignIfCompatible is a convenience function to safely
|
||||
// assign arbitrary values. dst must be a pointer to an
|
||||
// empty interface, or it must be a pointer to a compatible
|
||||
|
@ -15,19 +52,18 @@ func AssignIfCompatible(dst, src interface{}) error {
|
|||
|
||||
// t can be a pointer or a slice, and the code will slightly change
|
||||
// depending on this
|
||||
var isPtr bool
|
||||
var isSlice bool
|
||||
switch result.Kind() {
|
||||
case reflect.Ptr:
|
||||
// no op
|
||||
isPtr = true
|
||||
case reflect.Slice:
|
||||
isSlice = true
|
||||
default:
|
||||
return fmt.Errorf("argument t to AssignIfCompatible must be a pointer or a slice: %T", src)
|
||||
}
|
||||
|
||||
rv := reflect.ValueOf(dst)
|
||||
if rv.Kind() != reflect.Ptr {
|
||||
return fmt.Errorf(`argument to AssignIfCompatible() must be a pointer: %T`, dst)
|
||||
return fmt.Errorf(`destination argument to AssignIfCompatible() must be a pointer: %T`, dst)
|
||||
}
|
||||
|
||||
actualDst := rv.Elem()
|
||||
|
@ -37,7 +73,7 @@ func AssignIfCompatible(dst, src interface{}) error {
|
|||
default:
|
||||
// If it's a pointer to the struct we're looking for, we need to set
|
||||
// the de-referenced struct
|
||||
if !isSlice {
|
||||
if !isSlice && isPtr {
|
||||
result = result.Elem()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,8 @@ linters:
|
|||
enable-all: true
|
||||
disable:
|
||||
- cyclop
|
||||
- deadcode # deprecated
|
||||
- depguard
|
||||
- dupl
|
||||
- exhaustive
|
||||
- exhaustivestruct
|
||||
|
@ -44,9 +46,11 @@ linters:
|
|||
- nosnakecase
|
||||
- paralleltest
|
||||
- scopelint # deprecated
|
||||
- structcheck # deprecated
|
||||
- tagliatelle
|
||||
- testpackage
|
||||
- thelper # Tests are fine
|
||||
- varcheck # deprecated
|
||||
- varnamelen # Short names are ok
|
||||
- wrapcheck
|
||||
- wsl
|
||||
|
|
|
@ -4,6 +4,96 @@ Changes
|
|||
v2 has many incompatibilities with v1. To see the full list of differences between
|
||||
v1 and v2, please read the Changes-v2.md file (https://github.com/lestrrat-go/jwx/blob/develop/v2/Changes-v2.md)
|
||||
|
||||
v2.0.16 31 Oct 2023
|
||||
[Security]
|
||||
* [jws] ECDSA signature verification requires us to check if the signature
|
||||
is of the desired length of bytes, but this check that used to exist before
|
||||
had been removed in #65, resulting in certain malformed signatures to pass
|
||||
verification.
|
||||
|
||||
One of the ways this could happen if R is a 31 byte integer and S is 32 byte integer,
|
||||
both containing the correct signature values, but R is not zero-padded.
|
||||
|
||||
Correct = R: [ 0 , ... ] (32 bytes) S: [ ... ] (32 bytes)
|
||||
Wrong = R: [ ... ] (31 bytes) S: [ ... ] (32 bytes)
|
||||
|
||||
In order for this check to pass, you would still need to have all 63 bytes
|
||||
populated with the correct signature. The only modification a bad actor
|
||||
may be able to do is to add one more byte at the end, in which case the
|
||||
first 32 bytes (including what would have been S's first byte) is used for R,
|
||||
and S would contain the rest. But this will only result in the verification to
|
||||
fail. Therefore this in itself should not pose any security risk, albeit
|
||||
allowing some illegally formated messages to be verified.
|
||||
|
||||
* [jwk] `jwk.Key` objects now have a `Validate()` method to validate the data
|
||||
stored in the keys. However, this still does not necessarily mean that the key's
|
||||
are valid for use in cryptographic operations. If `Validate()` is successful,
|
||||
it only means that the keys are in the right _format_, including the presence
|
||||
of required fields and that certain fields have proper length, etc.
|
||||
|
||||
[New Features]
|
||||
* [jws] Added `jws.WithValidateKey()` to force calling `key.Validate()` before
|
||||
signing or verification.
|
||||
|
||||
* [jws] `jws.Sign()` now returns a special type of error that can hold the
|
||||
individual errors from the signers. The stringification is still the same
|
||||
as before to preserve backwards compatibility.
|
||||
|
||||
* [jwk] Added `jwk.IsKeyValidationError` that checks if an error is an error
|
||||
from `key.Validate()`.
|
||||
|
||||
[Bug Fixes]
|
||||
* [jwt] `jwt.ParseInsecure()` was running verification if you provided a key
|
||||
via `jwt.WithKey()` or `jwt.WithKeySet()` (#1007)
|
||||
|
||||
v2.0.15 19 20 Oct 2023
|
||||
[Bug fixes]
|
||||
* [jws] jws.Sign() now properly check for valid algorithm / key type pair when
|
||||
the key implements crypto.Signer. This was caused by the fact that when
|
||||
jws.WithKey() accepted keys that implemented crypto.Signer, there really
|
||||
is no way to robustly check what algorithm the crypto.Signer implements.
|
||||
|
||||
The code has now been modified to check for KNOWN key types, i.e. those
|
||||
that are defined in Go standard library, and those that are defined in
|
||||
this library. For example, now calling jws.Sign() with jws.WithKey(jwa.RS256, ecdsaKey)
|
||||
where ecdsaKey is either an instance of *ecdsa.PrivateKey or jwk.ECDSAPrivateKey
|
||||
will produce an error.
|
||||
|
||||
However, if you use a separate library that wraps some KMS library which implements
|
||||
crypto.Signer, this same check will not be performed due to the fact that
|
||||
it is an unknown library to us. And there's no way to query a crypto.Signer
|
||||
for its algorithm family.
|
||||
|
||||
v2.0.14 17 Oct 2023
|
||||
[New Features]
|
||||
* [jwk] jwk.IsPrivateKey(), as well as jwk.AsymmetricKey has been added.
|
||||
The function can be used to tell if a jwk.Key is a private key of an
|
||||
asymmetric key pair.
|
||||
[Security]
|
||||
* golang.org/x/crypto has been updated to 0.14.0. The update contains a fix for HTTP/2
|
||||
rapid reset DoS vulnerability, which some security scanning softwares may flag.
|
||||
However, do note that this library is NOT affected by the issue, as it does not have
|
||||
the capability to serve as an HTTP/2 server. This is included in this release
|
||||
document so that users will be able to tell why this library may be flagged
|
||||
when/if their scanning software do so.
|
||||
|
||||
v2.0.13 26 Sep 2023
|
||||
[New Features]
|
||||
* [jwk] jwk.Equal has been added. Please note that this is equivalent to
|
||||
comparing the keys' thumbprints, therefore it does NOT take in consideration
|
||||
non-essential fields.
|
||||
|
||||
[Miscellaneous]
|
||||
* Various documentation fixes and additions.
|
||||
|
||||
v2.0.12 - 11 Aug 2023
|
||||
[Bug fixes]
|
||||
* [jwt] jwt.Serializer was ignoring JWE flags (#951)
|
||||
|
||||
[Miscellaneous]
|
||||
* [jwk] Check for seed length on OKP JWKs to avoid panics (#947)
|
||||
* [jws] Documentation for jws.WithKeySet()
|
||||
|
||||
v2.0.11 - 14 Jun 2023
|
||||
[Security]
|
||||
* Potential Padding Oracle Attack Vulnerability and Timing Attack Vulnerability
|
||||
|
|
|
@ -33,8 +33,8 @@ def go_dependencies():
|
|||
name = "com_github_lestrrat_go_blackmagic",
|
||||
build_file_proto_mode = "disable_global",
|
||||
importpath = "github.com/lestrrat-go/blackmagic",
|
||||
sum = "h1:lS5Zts+5HIC/8og6cGHb0uCcNCa3OUt1ygh3Qz2Fe80=",
|
||||
version = "v1.0.1",
|
||||
sum = "h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=",
|
||||
version = "v1.0.2",
|
||||
)
|
||||
go_repository(
|
||||
name = "com_github_lestrrat_go_httpcc",
|
||||
|
@ -122,8 +122,8 @@ def go_dependencies():
|
|||
name = "org_golang_x_crypto",
|
||||
build_file_proto_mode = "disable_global",
|
||||
importpath = "golang.org/x/crypto",
|
||||
sum = "h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=",
|
||||
version = "v0.9.0",
|
||||
sum = "h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=",
|
||||
version = "v0.14.0",
|
||||
)
|
||||
go_repository(
|
||||
name = "org_golang_x_mod",
|
||||
|
@ -152,23 +152,23 @@ def go_dependencies():
|
|||
name = "org_golang_x_sys",
|
||||
build_file_proto_mode = "disable_global",
|
||||
importpath = "golang.org/x/sys",
|
||||
sum = "h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=",
|
||||
version = "v0.8.0",
|
||||
sum = "h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=",
|
||||
version = "v0.13.0",
|
||||
)
|
||||
go_repository(
|
||||
name = "org_golang_x_term",
|
||||
build_file_proto_mode = "disable_global",
|
||||
importpath = "golang.org/x/term",
|
||||
sum = "h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=",
|
||||
version = "v0.8.0",
|
||||
sum = "h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=",
|
||||
version = "v0.13.0",
|
||||
)
|
||||
|
||||
go_repository(
|
||||
name = "org_golang_x_text",
|
||||
build_file_proto_mode = "disable_global",
|
||||
importpath = "golang.org/x/text",
|
||||
sum = "h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=",
|
||||
version = "v0.9.0",
|
||||
sum = "h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=",
|
||||
version = "v0.13.0",
|
||||
)
|
||||
go_repository(
|
||||
name = "org_golang_x_tools",
|
||||
|
|
|
@ -80,10 +80,7 @@ func ReleaseECPointBuffer(buf []byte) {
|
|||
ecpointBufferPool.Put(&buf)
|
||||
}
|
||||
|
||||
// AllocECPointBuffer allocates a buffer for the given point in the given
|
||||
// curve. This buffer should be released using the ReleaseECPointBuffer
|
||||
// function.
|
||||
func AllocECPointBuffer(v *big.Int, crv elliptic.Curve) []byte {
|
||||
func CalculateKeySize(crv elliptic.Curve) int {
|
||||
// We need to create a buffer that fits the entire curve.
|
||||
// If the curve size is 66, that fits in 9 bytes. If the curve
|
||||
// size is 64, it fits in 8 bytes.
|
||||
|
@ -104,7 +101,14 @@ func AllocECPointBuffer(v *big.Int, crv elliptic.Curve) []byte {
|
|||
}
|
||||
}
|
||||
|
||||
buf := getCrvFixedBuffer(inBytes)
|
||||
return inBytes
|
||||
}
|
||||
|
||||
// AllocECPointBuffer allocates a buffer for the given point in the given
|
||||
// curve. This buffer should be released using the ReleaseECPointBuffer
|
||||
// function.
|
||||
func AllocECPointBuffer(v *big.Int, crv elliptic.Curve) []byte {
|
||||
buf := getCrvFixedBuffer(CalculateKeySize(crv))
|
||||
v.FillBytes(buf)
|
||||
return buf
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ func (h *stdHeaders) Clone(ctx context.Context) (Headers, error) {
|
|||
return dst, nil
|
||||
}
|
||||
|
||||
func (h *stdHeaders) Copy(ctx context.Context, dst Headers) error {
|
||||
func (h *stdHeaders) Copy(_ context.Context, dst Headers) error {
|
||||
for _, pair := range h.makePairs() {
|
||||
//nolint:forcetypeassert
|
||||
key := pair.Key.(string)
|
||||
|
|
|
@ -140,17 +140,20 @@ func (c Hmac) Overhead() int {
|
|||
}
|
||||
|
||||
func (c Hmac) ComputeAuthTag(aad, nonce, ciphertext []byte) ([]byte, error) {
|
||||
buf := make([]byte, len(aad)+len(nonce)+len(ciphertext)+8)
|
||||
n := 0
|
||||
n += copy(buf, aad)
|
||||
n += copy(buf[n:], nonce)
|
||||
n += copy(buf[n:], ciphertext)
|
||||
binary.BigEndian.PutUint64(buf[n:], uint64(len(aad)*8))
|
||||
var buf [8]byte
|
||||
binary.BigEndian.PutUint64(buf[:], uint64(len(aad)*8))
|
||||
|
||||
h := hmac.New(c.hash, c.integrityKey)
|
||||
if _, err := h.Write(buf); err != nil {
|
||||
return nil, fmt.Errorf(`failed to write ComputeAuthTag using Hmac: %w`, err)
|
||||
}
|
||||
|
||||
// compute the tag
|
||||
// no need to check errors because Write never returns an error: https://pkg.go.dev/hash#Hash
|
||||
//
|
||||
// > Write (via the embedded io.Writer interface) adds more data to the running hash.
|
||||
// > It never returns an error.
|
||||
h.Write(aad)
|
||||
h.Write(nonce)
|
||||
h.Write(ciphertext)
|
||||
h.Write(buf[:])
|
||||
s := h.Sum(nil)
|
||||
return s[:c.tagsize], nil
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ func (kw *Noop) KeyID() string {
|
|||
return kw.keyID
|
||||
}
|
||||
|
||||
func (kw *Noop) EncryptKey(cek []byte) (keygen.ByteSource, error) {
|
||||
func (kw *Noop) EncryptKey(_ []byte) (keygen.ByteSource, error) {
|
||||
return keygen.ByteKey(kw.sharedkey), nil
|
||||
}
|
||||
|
||||
|
|
|
@ -137,7 +137,7 @@ func ExampleJWK_Usage() {
|
|||
log.Printf("%s", jsonbuf)
|
||||
}
|
||||
|
||||
for it := set.Iterate(context.Background()); it.Next(context.Background()); {
|
||||
for it := set.Keys(context.Background()); it.Next(context.Background()); {
|
||||
pair := it.Pair()
|
||||
key := pair.Value.(jwk.Key)
|
||||
|
||||
|
|
|
@ -226,3 +226,48 @@ func (k ecdsaPrivateKey) Thumbprint(hash crypto.Hash) ([]byte, error) {
|
|||
base64.EncodeToString(ybuf),
|
||||
), nil
|
||||
}
|
||||
|
||||
func ecdsaValidateKey(k interface {
|
||||
Crv() jwa.EllipticCurveAlgorithm
|
||||
X() []byte
|
||||
Y() []byte
|
||||
}, checkPrivate bool) error {
|
||||
crv, ok := ecutil.CurveForAlgorithm(k.Crv())
|
||||
if !ok {
|
||||
return fmt.Errorf(`invalid curve algorithm %q`, k.Crv())
|
||||
}
|
||||
|
||||
keySize := ecutil.CalculateKeySize(crv)
|
||||
if x := k.X(); len(x) != keySize {
|
||||
return fmt.Errorf(`invalid "x" length (%d) for curve %q`, len(x), crv.Params().Name)
|
||||
}
|
||||
|
||||
if y := k.Y(); len(y) != keySize {
|
||||
return fmt.Errorf(`invalid "y" length (%d) for curve %q`, len(y), crv.Params().Name)
|
||||
}
|
||||
|
||||
if checkPrivate {
|
||||
if priv, ok := k.(interface{ D() []byte }); ok {
|
||||
if len(priv.D()) != keySize {
|
||||
return fmt.Errorf(`invalid "d" length (%d) for curve %q`, len(priv.D()), crv.Params().Name)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf(`missing "d" value`)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *ecdsaPrivateKey) Validate() error {
|
||||
if err := ecdsaValidateKey(k, true); err != nil {
|
||||
return NewKeyValidationError(fmt.Errorf(`jwk.ECDSAPrivateKey: %w`, err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *ecdsaPublicKey) Validate() error {
|
||||
if err := ecdsaValidateKey(k, false); err != nil {
|
||||
return NewKeyValidationError(fmt.Errorf(`jwk.ECDSAPublicKey: %w`, err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -65,6 +65,10 @@ func (h ecdsaPublicKey) KeyType() jwa.KeyType {
|
|||
return jwa.EC
|
||||
}
|
||||
|
||||
func (h ecdsaPublicKey) IsPrivate() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (h *ecdsaPublicKey) Algorithm() jwa.KeyAlgorithm {
|
||||
if h.algorithm != nil {
|
||||
return *(h.algorithm)
|
||||
|
@ -629,6 +633,10 @@ func (h ecdsaPrivateKey) KeyType() jwa.KeyType {
|
|||
return jwa.EC
|
||||
}
|
||||
|
||||
func (h ecdsaPrivateKey) IsPrivate() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (h *ecdsaPrivateKey) Algorithm() jwa.KeyAlgorithm {
|
||||
if h.algorithm != nil {
|
||||
return *(h.algorithm)
|
||||
|
|
|
@ -60,7 +60,7 @@ func getGlobalFetcher() httprc.Fetcher {
|
|||
}
|
||||
|
||||
// SetGlobalFetcher allows users to specify a custom global fetcher,
|
||||
// which is used by the `Fetch` function. Assigning `nil` forces the
|
||||
// which is used by the `Fetch` function. Assigning `nil` forces
|
||||
// the default fetcher to be (re)created when the next call to
|
||||
// `jwk.Fetch` occurs
|
||||
//
|
||||
|
|
|
@ -10,6 +10,13 @@ import (
|
|||
"github.com/lestrrat-go/jwx/v2/internal/json"
|
||||
)
|
||||
|
||||
// AsymmetricKey describes a Key that represents an key in an asymmetric key pair,
|
||||
// which in turn can be either a private or a public key. This interface
|
||||
// allows those keys to be queried if they are one or the other.
|
||||
type AsymmetricKey interface {
|
||||
IsPrivate() bool
|
||||
}
|
||||
|
||||
// KeyUsageType is used to denote what this key should be used for
|
||||
type KeyUsageType string
|
||||
|
||||
|
|
|
@ -46,6 +46,20 @@ type Key interface {
|
|||
// Remove removes the field associated with the specified key.
|
||||
// There is no way to remove the `kty` (key type). You will ALWAYS be left with one field in a jwk.Key.
|
||||
Remove(string) error
|
||||
// Validate performs _minimal_ checks if the data stored in the key are valid.
|
||||
// By minimal, we mean that it does not check if the key is valid for use in
|
||||
// cryptographic operations. For example, it does not check if an RSA key's
|
||||
// `e` field is a valid exponent, or if the `n` field is a valid modulus.
|
||||
// Instead, it checks for things such as the _presence_ of some required fields,
|
||||
// or if certain keys' values are of particular length.
|
||||
//
|
||||
// Note that depending on th underlying key type, use of this method requires
|
||||
// that multiple fields in the key are properly populated. For example, an EC
|
||||
// key's "x", "y" fields cannot be validated unless the "crv" field is populated first.
|
||||
//
|
||||
// Validate is never called by `UnmarshalJSON()` or `Set`. It must explicitly be
|
||||
// called by the user
|
||||
Validate() error
|
||||
|
||||
// Raw creates the corresponding raw key. For example,
|
||||
// EC types would create *ecdsa.PublicKey or *ecdsa.PrivateKey,
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/big"
|
||||
|
@ -727,3 +728,62 @@ func AvailableCurves() []elliptic.Curve {
|
|||
func CurveForAlgorithm(alg jwa.EllipticCurveAlgorithm) (elliptic.Curve, bool) {
|
||||
return ecutil.CurveForAlgorithm(alg)
|
||||
}
|
||||
|
||||
// Equal compares two keys and returns true if they are equal. The comparison
|
||||
// is solely done against the thumbprints of k1 and k2. It is possible for keys
|
||||
// that have, for example, different key IDs, key usage, etc, to be considered equal.
|
||||
func Equal(k1, k2 Key) bool {
|
||||
h := crypto.SHA256
|
||||
tp1, err := k1.Thumbprint(h)
|
||||
if err != nil {
|
||||
return false // can't report error
|
||||
}
|
||||
tp2, err := k2.Thumbprint(h)
|
||||
if err != nil {
|
||||
return false // can't report error
|
||||
}
|
||||
|
||||
return bytes.Equal(tp1, tp2)
|
||||
}
|
||||
|
||||
// IsPrivateKey returns true if the supplied key is a private key of an
|
||||
// asymmetric key pair. The argument `k` must implement the `AsymmetricKey`
|
||||
// interface.
|
||||
//
|
||||
// An error is returned if the supplied key is not an `AsymmetricKey`.
|
||||
func IsPrivateKey(k Key) (bool, error) {
|
||||
asymmetric, ok := k.(AsymmetricKey)
|
||||
if ok {
|
||||
return asymmetric.IsPrivate(), nil
|
||||
}
|
||||
return false, fmt.Errorf("jwk.IsPrivateKey: %T is not an asymmetric key", k)
|
||||
}
|
||||
|
||||
type keyValidationError struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *keyValidationError) Error() string {
|
||||
return fmt.Sprintf(`key validation failed: %s`, e.err)
|
||||
}
|
||||
|
||||
func (e *keyValidationError) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
func (e *keyValidationError) Is(target error) bool {
|
||||
_, ok := target.(*keyValidationError)
|
||||
return ok
|
||||
}
|
||||
|
||||
// NewKeyValidationError wraps the given error with an error that denotes
|
||||
// `key.Validate()` has failed. This error type should ONLY be used as
|
||||
// return value from the `Validate()` method.
|
||||
func NewKeyValidationError(err error) error {
|
||||
return &keyValidationError{err: err}
|
||||
}
|
||||
|
||||
func IsKeyValidationError(err error) bool {
|
||||
var kve keyValidationError
|
||||
return errors.Is(err, &kve)
|
||||
}
|
||||
|
|
|
@ -81,8 +81,14 @@ func (k *okpPublicKey) Raw(v interface{}) error {
|
|||
}
|
||||
|
||||
func buildOKPPrivateKey(alg jwa.EllipticCurveAlgorithm, xbuf []byte, dbuf []byte) (interface{}, error) {
|
||||
if len(dbuf) == 0 {
|
||||
return nil, fmt.Errorf(`cannot use empty seed`)
|
||||
}
|
||||
switch alg {
|
||||
case jwa.Ed25519:
|
||||
if len(dbuf) != ed25519.SeedSize {
|
||||
return nil, fmt.Errorf(`wrong private key size`)
|
||||
}
|
||||
ret := ed25519.NewKeyFromSeed(dbuf)
|
||||
//nolint:forcetypeassert
|
||||
if !bytes.Equal(xbuf, ret.Public().(ed25519.PublicKey)) {
|
||||
|
@ -181,3 +187,41 @@ func (k okpPrivateKey) Thumbprint(hash crypto.Hash) ([]byte, error) {
|
|||
base64.EncodeToString(k.x),
|
||||
), nil
|
||||
}
|
||||
|
||||
func validateOKPKey(key interface {
|
||||
Crv() jwa.EllipticCurveAlgorithm
|
||||
X() []byte
|
||||
}) error {
|
||||
if key.Crv() == jwa.InvalidEllipticCurve {
|
||||
return fmt.Errorf(`invalid curve algorithm`)
|
||||
}
|
||||
|
||||
if len(key.X()) == 0 {
|
||||
return fmt.Errorf(`missing "x" field`)
|
||||
}
|
||||
|
||||
if priv, ok := key.(interface{ D() []byte }); ok {
|
||||
if len(priv.D()) == 0 {
|
||||
return fmt.Errorf(`missing "d" field`)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *okpPublicKey) Validate() error {
|
||||
k.mu.RLock()
|
||||
defer k.mu.RUnlock()
|
||||
if err := validateOKPKey(k); err != nil {
|
||||
return NewKeyValidationError(fmt.Errorf(`jwk.OKPPublicKey: %w`, err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *okpPrivateKey) Validate() error {
|
||||
k.mu.RLock()
|
||||
defer k.mu.RUnlock()
|
||||
if err := validateOKPKey(k); err != nil {
|
||||
return NewKeyValidationError(fmt.Errorf(`jwk.OKPPrivateKey: %w`, err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -61,6 +61,10 @@ func (h okpPublicKey) KeyType() jwa.KeyType {
|
|||
return jwa.OKP
|
||||
}
|
||||
|
||||
func (h okpPublicKey) IsPrivate() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (h *okpPublicKey) Algorithm() jwa.KeyAlgorithm {
|
||||
if h.algorithm != nil {
|
||||
return *(h.algorithm)
|
||||
|
@ -595,6 +599,10 @@ func (h okpPrivateKey) KeyType() jwa.KeyType {
|
|||
return jwa.OKP
|
||||
}
|
||||
|
||||
func (h okpPrivateKey) IsPrivate() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (h *okpPrivateKey) Algorithm() jwa.KeyAlgorithm {
|
||||
if h.algorithm != nil {
|
||||
return *(h.algorithm)
|
||||
|
|
|
@ -4,7 +4,7 @@ interfaces:
|
|||
- name: CacheOption
|
||||
comment: |
|
||||
CacheOption is a type of Option that can be passed to the
|
||||
`jwk.Cache` object.
|
||||
the `jwk.NewCache()` function.
|
||||
- name: AssignKeyIDOption
|
||||
- name: FetchOption
|
||||
methods:
|
||||
|
@ -13,8 +13,8 @@ interfaces:
|
|||
- registerOption
|
||||
comment: |
|
||||
FetchOption is a type of Option that can be passed to `jwk.Fetch()`
|
||||
FetchOption also implements the `CacheOption`, and thus can
|
||||
safely be passed to `(*jwk.Cache).Configure()`
|
||||
FetchOption also implements the `RegisterOption`, and thus can
|
||||
safely be passed to `(*jwk.Cache).Register()`
|
||||
- name: ParseOption
|
||||
methods:
|
||||
- fetchOption
|
||||
|
|
|
@ -25,7 +25,7 @@ type assignKeyIDOption struct {
|
|||
func (*assignKeyIDOption) assignKeyIDOption() {}
|
||||
|
||||
// CacheOption is a type of Option that can be passed to the
|
||||
// `jwk.Cache` object.
|
||||
// the `jwk.NewCache()` function.
|
||||
type CacheOption interface {
|
||||
Option
|
||||
cacheOption()
|
||||
|
@ -38,8 +38,8 @@ type cacheOption struct {
|
|||
func (*cacheOption) cacheOption() {}
|
||||
|
||||
// FetchOption is a type of Option that can be passed to `jwk.Fetch()`
|
||||
// FetchOption also implements the `CacheOption`, and thus can
|
||||
// safely be passed to `(*jwk.Cache).Configure()`
|
||||
// FetchOption also implements the `RegisterOption`, and thus can
|
||||
// safely be passed to `(*jwk.Cache).Register()`
|
||||
type FetchOption interface {
|
||||
Option
|
||||
fetchOption()
|
||||
|
|
|
@ -241,3 +241,43 @@ func rsaThumbprint(hash crypto.Hash, key *rsa.PublicKey) ([]byte, error) {
|
|||
}
|
||||
return h.Sum(nil), nil
|
||||
}
|
||||
|
||||
func validateRSAKey(key interface {
|
||||
N() []byte
|
||||
E() []byte
|
||||
}, checkPrivate bool) error {
|
||||
if len(key.N()) == 0 {
|
||||
// Ideally we would like to check for the actual length, but unlike
|
||||
// EC keys, we have nothing in the key itself that will tell us
|
||||
// how many bits this key should have.
|
||||
return fmt.Errorf(`missing "n" value`)
|
||||
}
|
||||
if len(key.E()) == 0 {
|
||||
return fmt.Errorf(`missing "e" value`)
|
||||
}
|
||||
if checkPrivate {
|
||||
if priv, ok := key.(interface{ D() []byte }); ok {
|
||||
if len(priv.D()) == 0 {
|
||||
return fmt.Errorf(`missing "d" value`)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf(`missing "d" value`)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *rsaPrivateKey) Validate() error {
|
||||
if err := validateRSAKey(k, true); err != nil {
|
||||
return NewKeyValidationError(fmt.Errorf(`jwk.RSAPrivateKey: %w`, err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *rsaPublicKey) Validate() error {
|
||||
if err := validateRSAKey(k, false); err != nil {
|
||||
return NewKeyValidationError(fmt.Errorf(`jwk.RSAPublicKey: %w`, err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -67,6 +67,10 @@ func (h rsaPublicKey) KeyType() jwa.KeyType {
|
|||
return jwa.RSA
|
||||
}
|
||||
|
||||
func (h rsaPublicKey) IsPrivate() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (h *rsaPublicKey) Algorithm() jwa.KeyAlgorithm {
|
||||
if h.algorithm != nil {
|
||||
return *(h.algorithm)
|
||||
|
@ -606,6 +610,10 @@ func (h rsaPrivateKey) KeyType() jwa.KeyType {
|
|||
return jwa.RSA
|
||||
}
|
||||
|
||||
func (h rsaPrivateKey) IsPrivate() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (h *rsaPrivateKey) Algorithm() jwa.KeyAlgorithm {
|
||||
if h.algorithm != nil {
|
||||
return *(h.algorithm)
|
||||
|
|
|
@ -58,3 +58,10 @@ func (k *symmetricKey) PublicKey() (Key, error) {
|
|||
}
|
||||
return newKey, nil
|
||||
}
|
||||
|
||||
func (k *symmetricKey) Validate() error {
|
||||
if len(k.Octets()) == 0 {
|
||||
return NewKeyValidationError(fmt.Errorf(`jwk.SymmetricKey: missing "k" field`))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ go_library(
|
|||
deps = [
|
||||
"//cert",
|
||||
"//internal/base64",
|
||||
"//internal/ecutil",
|
||||
"//internal/iter",
|
||||
"//internal/json",
|
||||
"//internal/keyconv",
|
||||
|
@ -50,6 +51,7 @@ go_test(
|
|||
deps = [
|
||||
"//cert",
|
||||
"//internal/base64",
|
||||
"//internal/ecutil",
|
||||
"//internal/json",
|
||||
"//internal/jwxtest",
|
||||
"//jwa",
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/lestrrat-go/jwx/v2/internal/ecutil"
|
||||
"github.com/lestrrat-go/jwx/v2/internal/keyconv"
|
||||
"github.com/lestrrat-go/jwx/v2/internal/pool"
|
||||
"github.com/lestrrat-go/jwx/v2/jwa"
|
||||
|
@ -64,6 +65,9 @@ func (es *ecdsaSigner) Sign(payload []byte, key interface{}) ([]byte, error) {
|
|||
|
||||
signer, ok := key.(crypto.Signer)
|
||||
if ok {
|
||||
if !isValidECDSAKey(key) {
|
||||
return nil, fmt.Errorf(`cannot use key of type %T to generate ECDSA based signatures`, key)
|
||||
}
|
||||
switch key.(type) {
|
||||
case ecdsa.PrivateKey, *ecdsa.PrivateKey:
|
||||
// if it's a ecdsa.PrivateKey, it's more efficient to
|
||||
|
@ -133,6 +137,7 @@ func (es *ecdsaSigner) Sign(payload []byte, key interface{}) ([]byte, error) {
|
|||
copy(sBytesPadded[keyBytes-len(sBytes):], sBytes)
|
||||
|
||||
out := append(rBytesPadded, sBytesPadded...)
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
|
@ -181,9 +186,13 @@ func (v *ecdsaVerifier) Verify(payload []byte, signature []byte, key interface{}
|
|||
defer pool.ReleaseBigInt(r)
|
||||
defer pool.ReleaseBigInt(s)
|
||||
|
||||
n := len(signature) / 2
|
||||
r.SetBytes(signature[:n])
|
||||
s.SetBytes(signature[n:])
|
||||
keySize := ecutil.CalculateKeySize(pubkey.Curve)
|
||||
if len(signature) != keySize*2 {
|
||||
return fmt.Errorf(`invalid signature length for curve %q`, pubkey.Curve.Params().Name)
|
||||
}
|
||||
|
||||
r.SetBytes(signature[:keySize])
|
||||
s.SetBytes(signature[keySize:])
|
||||
|
||||
h := v.hash.New()
|
||||
if _, err := h.Write(payload); err != nil {
|
||||
|
|
|
@ -28,7 +28,11 @@ func (s eddsaSigner) Sign(payload []byte, key interface{}) ([]byte, error) {
|
|||
// The ed25519.PrivateKey object implements crypto.Signer, so we should
|
||||
// simply accept a crypto.Signer here.
|
||||
signer, ok := key.(crypto.Signer)
|
||||
if !ok {
|
||||
if ok {
|
||||
if !isValidEDDSAKey(key) {
|
||||
return nil, fmt.Errorf(`cannot use key of type %T to generate EdDSA based signatures`, key)
|
||||
}
|
||||
} else {
|
||||
// This fallback exists for cases when jwk.Key was passed, or
|
||||
// users gave us a pointer instead of non-pointer, etc.
|
||||
var privkey ed25519.PrivateKey
|
||||
|
|
|
@ -34,7 +34,7 @@ func (h *stdHeaders) AsMap(ctx context.Context) (map[string]interface{}, error)
|
|||
return iter.AsMap(ctx, h)
|
||||
}
|
||||
|
||||
func (h *stdHeaders) Copy(ctx context.Context, dst Headers) error {
|
||||
func (h *stdHeaders) Copy(_ context.Context, dst Headers) error {
|
||||
for _, pair := range h.makePairs() {
|
||||
//nolint:forcetypeassert
|
||||
key := pair.Key.(string)
|
||||
|
|
|
@ -28,6 +28,7 @@ import (
|
|||
"crypto/ecdsa"
|
||||
"crypto/ed25519"
|
||||
"crypto/rsa"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
|
@ -107,6 +108,18 @@ const (
|
|||
var _ = fmtInvalid
|
||||
var _ = fmtMax
|
||||
|
||||
func validateKeyBeforeUse(key interface{}) error {
|
||||
jwkKey, ok := key.(jwk.Key)
|
||||
if !ok {
|
||||
converted, err := jwk.FromRaw(key)
|
||||
if err != nil {
|
||||
return fmt.Errorf(`could not convert key of type %T to jwk.Key for validation: %w`, key, err)
|
||||
}
|
||||
jwkKey = converted
|
||||
}
|
||||
return jwkKey.Validate()
|
||||
}
|
||||
|
||||
// Sign generates a JWS message for the given payload and returns
|
||||
// it in serialized form, which can be in either compact or
|
||||
// JSON format. Default is compact.
|
||||
|
@ -149,6 +162,7 @@ func Sign(payload []byte, options ...SignOption) ([]byte, error) {
|
|||
var signers []*payloadSigner
|
||||
var detached bool
|
||||
var noneSignature *payloadSigner
|
||||
var validateKey bool
|
||||
for _, option := range options {
|
||||
//nolint:forcetypeassert
|
||||
switch option.Ident() {
|
||||
|
@ -185,6 +199,8 @@ func Sign(payload []byte, options ...SignOption) ([]byte, error) {
|
|||
return nil, fmt.Errorf(`jws.Sign: payload must be nil when jws.WithDetachedPayload() is specified`)
|
||||
}
|
||||
payload = option.Value().([]byte)
|
||||
case identValidateKey{}:
|
||||
validateKey = option.Value().(bool)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -239,6 +255,12 @@ func Sign(payload []byte, options ...SignOption) ([]byte, error) {
|
|||
// cheat. FIXXXXXXMEEEEEE
|
||||
detached: detached,
|
||||
}
|
||||
|
||||
if validateKey {
|
||||
if err := validateKeyBeforeUse(signer.key); err != nil {
|
||||
return nil, fmt.Errorf(`jws.Verify: %w`, err)
|
||||
}
|
||||
}
|
||||
_, _, err := sig.Sign(payload, signer.signer, signer.key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`failed to generate signature for signer #%d (alg=%s): %w`, i, signer.Algorithm(), err)
|
||||
|
@ -290,6 +312,7 @@ func Verify(buf []byte, options ...VerifyOption) ([]byte, error) {
|
|||
var detachedPayload []byte
|
||||
var keyProviders []KeyProvider
|
||||
var keyUsed interface{}
|
||||
var validateKey bool
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
|
@ -316,6 +339,8 @@ func Verify(buf []byte, options ...VerifyOption) ([]byte, error) {
|
|||
keyUsed = option.Value()
|
||||
case identContext{}:
|
||||
ctx = option.Value().(context.Context)
|
||||
case identValidateKey{}:
|
||||
validateKey = option.Value().(bool)
|
||||
default:
|
||||
return nil, fmt.Errorf(`invalid jws.VerifyOption %q passed`, `With`+strings.TrimPrefix(fmt.Sprintf(`%T`, option.Ident()), `jws.ident`))
|
||||
}
|
||||
|
@ -350,6 +375,7 @@ func Verify(buf []byte, options ...VerifyOption) ([]byte, error) {
|
|||
verifyBuf := pool.GetBytesBuffer()
|
||||
defer pool.ReleaseBytesBuffer(verifyBuf)
|
||||
|
||||
var errs []error
|
||||
for i, sig := range msg.signatures {
|
||||
verifyBuf.Reset()
|
||||
|
||||
|
@ -386,12 +412,19 @@ func Verify(buf []byte, options ...VerifyOption) ([]byte, error) {
|
|||
//nolint:forcetypeassert
|
||||
alg := pair.alg.(jwa.SignatureAlgorithm)
|
||||
key := pair.key
|
||||
|
||||
if validateKey {
|
||||
if err := validateKeyBeforeUse(key); err != nil {
|
||||
return nil, fmt.Errorf(`jws.Verify: %w`, err)
|
||||
}
|
||||
}
|
||||
verifier, err := NewVerifier(alg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`failed to create verifier for algorithm %q: %w`, alg, err)
|
||||
}
|
||||
|
||||
if err := verifier.Verify(verifyBuf.Bytes(), sig.signature, key); err != nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -409,7 +442,33 @@ func Verify(buf []byte, options ...VerifyOption) ([]byte, error) {
|
|||
}
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf(`could not verify message using any of the signatures or keys`)
|
||||
return nil, &verifyError{errs: errs}
|
||||
}
|
||||
|
||||
type verifyError struct {
|
||||
// Note: when/if we can ditch Go < 1.20, we can change this to a simple
|
||||
// `err error`, where the value is the result of `errors.Join()`
|
||||
//
|
||||
// We also need to implement Unwrap:
|
||||
// func (e *verifyError) Unwrap() error {
|
||||
// return e.err
|
||||
//}
|
||||
//
|
||||
// And finally, As() can go away
|
||||
errs []error
|
||||
}
|
||||
|
||||
func (e *verifyError) Error() string {
|
||||
return `could not verify message using any of the signatures or keys`
|
||||
}
|
||||
|
||||
func (e *verifyError) As(target interface{}) bool {
|
||||
for _, err := range e.errs {
|
||||
if errors.As(err, target) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// get the value of b64 header field.
|
||||
|
@ -747,3 +806,51 @@ func AlgorithmsForKey(key interface{}) ([]jwa.SignatureAlgorithm, error) {
|
|||
}
|
||||
return algs, nil
|
||||
}
|
||||
|
||||
// Because the keys defined in github.com/lestrrat-go/jwx/jwk may also implement
|
||||
// crypto.Signer, it would be possible for to mix up key types when signing/verifying
|
||||
// for example, when we specify jws.WithKey(jwa.RSA256, cryptoSigner), the cryptoSigner
|
||||
// can be for RSA, or any other type that implements crypto.Signer... even if it's for the
|
||||
// wrong algorithm.
|
||||
//
|
||||
// These functions are there to differentiate between the valid KNOWN key types.
|
||||
// For any other key type that is outside of the Go std library and our own code,
|
||||
// we must rely on the user to be vigilant.
|
||||
//
|
||||
// Notes: symmetric keys are obviously not part of this. for v2 OKP keys,
|
||||
// x25519 does not implement Sign()
|
||||
func isValidRSAKey(key interface{}) bool {
|
||||
switch key.(type) {
|
||||
case
|
||||
ecdsa.PrivateKey, *ecdsa.PrivateKey,
|
||||
ed25519.PrivateKey,
|
||||
jwk.ECDSAPrivateKey, jwk.OKPPrivateKey:
|
||||
// these are NOT ok
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isValidECDSAKey(key interface{}) bool {
|
||||
switch key.(type) {
|
||||
case
|
||||
ed25519.PrivateKey,
|
||||
rsa.PrivateKey, *rsa.PrivateKey,
|
||||
jwk.RSAPrivateKey, jwk.OKPPrivateKey:
|
||||
// these are NOT ok
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isValidEDDSAKey(key interface{}) bool {
|
||||
switch key.(type) {
|
||||
case
|
||||
ecdsa.PrivateKey, *ecdsa.PrivateKey,
|
||||
rsa.PrivateKey, *rsa.PrivateKey,
|
||||
jwk.RSAPrivateKey, jwk.ECDSAPrivateKey:
|
||||
// these are NOT ok
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ func (w *withKey) Protected(v Headers) Headers {
|
|||
// You will have to use a separate, more explicit option to allow the use of "none"
|
||||
// algorithm.
|
||||
//
|
||||
// The algorithm specified in the `alg` parameter must be able to support
|
||||
// The algorithm specified in the `alg` parameter MUST be able to support
|
||||
// the type of key you provided, otherwise an error is returned.
|
||||
//
|
||||
// Any of the followin is accepted for the `key` parameter:
|
||||
|
@ -72,6 +72,16 @@ func (w *withKey) Protected(v Headers) Headers {
|
|||
// * A crypto.Signer
|
||||
// * A jwk.Key
|
||||
//
|
||||
// Note that due to technical reasons, this library is NOT able to differentiate
|
||||
// between a valid/invalid key for given algorithm if the key implements crypto.Signer
|
||||
// and the key is from an external library. For example, while we can tell that it is
|
||||
// invalid to use `jwk.WithKey(jwa.RSA256, ecdsaPrivateKey)` because the key is
|
||||
// presumably from `crypto/ecdsa` or this library, if you use a KMS wrapper
|
||||
// that implements crypto.Signer that is outside of the go standard library or this
|
||||
// library, we will not be able to properly catch the misuse of such keys --
|
||||
// the output will happily generate an ECDSA signature even in the presence of
|
||||
// `jwa.RSA256`
|
||||
//
|
||||
// A `crypto.Signer` is used when the private part of a key is
|
||||
// kept in an inaccessible location, such as hardware.
|
||||
// `crypto.Signer` is currently supported for RSA, ECDSA, and EdDSA
|
||||
|
@ -89,7 +99,7 @@ func (w *withKey) Protected(v Headers) Headers {
|
|||
// is respected when serializing. That is, if you specify a header with
|
||||
// `{"b64": false}`, then the payload is not base64 encoded.
|
||||
//
|
||||
// These suboptions are ignored whe the `jws.WithKey()` option is used with `jws.Verify()`.
|
||||
// These suboptions are ignored when the `jws.WithKey()` option is used with `jws.Verify()`.
|
||||
func WithKey(alg jwa.KeyAlgorithm, key interface{}, options ...WithKeySuboption) SignVerifyOption {
|
||||
// Implementation note: this option is shared between Sign() and
|
||||
// Verify(). As such we don't create a KeyProvider here because
|
||||
|
@ -117,11 +127,23 @@ func WithKey(alg jwa.KeyAlgorithm, key interface{}, options ...WithKeySuboption)
|
|||
|
||||
// WithKeySet specifies a JWKS (jwk.Set) to use for verification.
|
||||
//
|
||||
// By default both `alg` and `kid` fields in the JWS _and_ the
|
||||
// key must match for a key in the JWKS to be considered to be used.
|
||||
// Because a JWKS can contain multiple keys and this library cannot tell
|
||||
// which one of the keys should be used for verification, we by default
|
||||
// require that both `alg` and `kid` fields in the JWS _and_ the
|
||||
// key match before a key is considered to be used.
|
||||
//
|
||||
// The behavior can be tweaked by using the `jws.WithKeySetSuboption`
|
||||
// suboption types.
|
||||
// There are ways to override this behavior, but they must be explicitly
|
||||
// specified by the caller.
|
||||
//
|
||||
// To work with keys/JWS messages not having a `kid` field, you may specify
|
||||
// the suboption `WithKeySetRequired` via `jws.WithKeySetSuboption(jws.WithKeySetRequireKid(false))`.
|
||||
// This will allow the library to proceed without having to match the `kid` field.
|
||||
//
|
||||
// However, it will still check if the `alg` fields in the JWS message and the key(s)
|
||||
// match. If you must work with JWS messages that do not have an `alg` field,
|
||||
// you will need to use `jws.WithKeySetSuboption(jws.WithInferAlgorithm(true))`.
|
||||
//
|
||||
// See the documentation for `WithInferAlgorithm()` for more details.
|
||||
func WithKeySet(set jwk.Set, options ...WithKeySetSuboption) VerifyOption {
|
||||
requireKid := true
|
||||
var useDefault, inferAlgorithm, multipleKeysPerKeyID bool
|
||||
|
|
|
@ -88,6 +88,27 @@ options:
|
|||
`jwk.Key` here unless you are 100% sure that all keys that you
|
||||
have provided are instances of `jwk.Key` (remember that the
|
||||
jwx API allows users to specify a raw key such as *rsa.PublicKey)
|
||||
- ident: ValidateKey
|
||||
interface: SignVerifyOption
|
||||
argument_type: bool
|
||||
comment: |
|
||||
WithValidateKey specifies whether the key used for signing or verification
|
||||
should be validated before using. Note that this means calling
|
||||
`key.Validate()` on the key, which in turn means that your key
|
||||
must be a `jwk.Key` instance, or a key that can be converted to
|
||||
a `jwk.Key` by calling `jwk.FromRaw()`. This means that your
|
||||
custom hardware-backed keys will probably not work.
|
||||
|
||||
You can directly call `key.Validate()` yourself if you need to
|
||||
mix keys that cannot be converted to `jwk.Key`.
|
||||
|
||||
Please also note that use of this option will also result in
|
||||
one extra conversion of raw keys to a `jwk.Key` instance. If you
|
||||
care about shaving off as much as possible, consider using a
|
||||
pre-validated key instead of using this option to validate
|
||||
the key on-demand each time.
|
||||
|
||||
By default, the key is not validated.
|
||||
- ident: InferAlgorithmFromKey
|
||||
interface: WithKeySetSuboption
|
||||
argument_type: bool
|
||||
|
@ -95,9 +116,14 @@ options:
|
|||
WithInferAlgorithmFromKey specifies whether the JWS signing algorithm name
|
||||
should be inferred by looking at the provided key, in case the JWS
|
||||
message or the key does not have a proper `alg` header.
|
||||
|
||||
|
||||
When this option is set to true, a list of algorithm(s) that is compatible
|
||||
with the key type will be enumerated, and _ALL_ of them will be tried
|
||||
against the key/message pair. If any of them succeeds, the verification
|
||||
will be considered successful.
|
||||
|
||||
Compared to providing explicit `alg` from the key this is slower, and
|
||||
verification may fail to verify if some how our heuristics are wrong
|
||||
verification may fail to verify if somehow our heuristics are wrong
|
||||
or outdated.
|
||||
|
||||
Also, automatic detection of signature verification methods are always
|
||||
|
|
|
@ -139,6 +139,7 @@ type identPublicHeaders struct{}
|
|||
type identRequireKid struct{}
|
||||
type identSerialization struct{}
|
||||
type identUseDefault struct{}
|
||||
type identValidateKey struct{}
|
||||
|
||||
func (identContext) String() string {
|
||||
return "WithContext"
|
||||
|
@ -204,6 +205,10 @@ func (identUseDefault) String() string {
|
|||
return "WithUseDefault"
|
||||
}
|
||||
|
||||
func (identValidateKey) String() string {
|
||||
return "WithValidateKey"
|
||||
}
|
||||
|
||||
func WithContext(v context.Context) VerifyOption {
|
||||
return &verifyOption{option.New(identContext{}, v)}
|
||||
}
|
||||
|
@ -235,8 +240,13 @@ func WithFS(v fs.FS) ReadFileOption {
|
|||
// should be inferred by looking at the provided key, in case the JWS
|
||||
// message or the key does not have a proper `alg` header.
|
||||
//
|
||||
// When this option is set to true, a list of algorithm(s) that is compatible
|
||||
// with the key type will be enumerated, and _ALL_ of them will be tried
|
||||
// against the key/message pair. If any of them succeeds, the verification
|
||||
// will be considered successful.
|
||||
//
|
||||
// Compared to providing explicit `alg` from the key this is slower, and
|
||||
// verification may fail to verify if some how our heuristics are wrong
|
||||
// verification may fail to verify if somehow our heuristics are wrong
|
||||
// or outdated.
|
||||
//
|
||||
// Also, automatic detection of signature verification methods are always
|
||||
|
@ -329,3 +339,24 @@ func WithCompact() SignOption {
|
|||
func WithUseDefault(v bool) WithKeySetSuboption {
|
||||
return &withKeySetSuboption{option.New(identUseDefault{}, v)}
|
||||
}
|
||||
|
||||
// WithValidateKey specifies whether the key used for signing or verification
|
||||
// should be validated before using. Note that this means calling
|
||||
// `key.Validate()` on the key, which in turn means that your key
|
||||
// must be a `jwk.Key` instance, or a key that can be converted to
|
||||
// a `jwk.Key` by calling `jwk.FromRaw()`. This means that your
|
||||
// custom hardware-backed keys will probably not work.
|
||||
//
|
||||
// You can directly call `key.Validate()` yourself if you need to
|
||||
// mix keys that cannot be converted to `jwk.Key`.
|
||||
//
|
||||
// Please also note that use of this option will also result in
|
||||
// one extra conversion of raw keys to a `jwk.Key` instance. If you
|
||||
// care about shaving off as much as possible, consider using a
|
||||
// pre-validated key instead of using this option to validate
|
||||
// the key on-demand each time.
|
||||
//
|
||||
// By default, the key is not validated.
|
||||
func WithValidateKey(v bool) SignVerifyOption {
|
||||
return &signVerifyOption{option.New(identValidateKey{}, v)}
|
||||
}
|
||||
|
|
|
@ -77,7 +77,11 @@ func (rs *rsaSigner) Sign(payload []byte, key interface{}) ([]byte, error) {
|
|||
}
|
||||
|
||||
signer, ok := key.(crypto.Signer)
|
||||
if !ok {
|
||||
if ok {
|
||||
if !isValidRSAKey(key) {
|
||||
return nil, fmt.Errorf(`cannot use key of type %T to generate RSA based signatures`, key)
|
||||
}
|
||||
} else {
|
||||
var privkey rsa.PrivateKey
|
||||
if err := keyconv.RSAPrivateKey(&privkey, key); err != nil {
|
||||
return nil, fmt.Errorf(`failed to retrieve rsa.PrivateKey out of %T: %w`, key, err)
|
||||
|
|
|
@ -209,6 +209,10 @@ func parseBytes(data []byte, options ...ParseOption) (Token, error) {
|
|||
}
|
||||
}
|
||||
|
||||
if !verification {
|
||||
ctx.skipVerification = true
|
||||
}
|
||||
|
||||
lvo := len(verifyOpts)
|
||||
if lvo == 0 && verification {
|
||||
return nil, fmt.Errorf(`jwt.Parse: no keys for verification are provided (use jwt.WithVerify(false) to explicitly skip)`)
|
||||
|
|
|
@ -18,7 +18,7 @@ type identTypedClaim struct{}
|
|||
type identVerifyAuto struct{}
|
||||
|
||||
func toSignOptions(options ...Option) ([]jws.SignOption, error) {
|
||||
var soptions []jws.SignOption
|
||||
soptions := make([]jws.SignOption, 0, len(options))
|
||||
for _, option := range options {
|
||||
//nolint:forcetypeassert
|
||||
switch option.Ident() {
|
||||
|
@ -36,13 +36,16 @@ func toSignOptions(options ...Option) ([]jws.SignOption, error) {
|
|||
}
|
||||
|
||||
soptions = append(soptions, jws.WithKey(wk.alg, wk.key, wksoptions...))
|
||||
case identSignOption{}:
|
||||
sigOpt := option.Value().(jws.SignOption) // this always succeeds
|
||||
soptions = append(soptions, sigOpt)
|
||||
}
|
||||
}
|
||||
return soptions, nil
|
||||
}
|
||||
|
||||
func toEncryptOptions(options ...Option) ([]jwe.EncryptOption, error) {
|
||||
var soptions []jwe.EncryptOption
|
||||
soptions := make([]jwe.EncryptOption, 0, len(options))
|
||||
for _, option := range options {
|
||||
//nolint:forcetypeassert
|
||||
switch option.Ident() {
|
||||
|
@ -58,13 +61,16 @@ func toEncryptOptions(options ...Option) ([]jwe.EncryptOption, error) {
|
|||
}
|
||||
|
||||
soptions = append(soptions, jwe.WithKey(wk.alg, wk.key, wksoptions...))
|
||||
case identEncryptOption{}:
|
||||
encOpt := option.Value().(jwe.EncryptOption) // this always succeeds
|
||||
soptions = append(soptions, encOpt)
|
||||
}
|
||||
}
|
||||
return soptions, nil
|
||||
}
|
||||
|
||||
func toVerifyOptions(options ...Option) ([]jws.VerifyOption, error) {
|
||||
var voptions []jws.VerifyOption
|
||||
voptions := make([]jws.VerifyOption, 0, len(options))
|
||||
for _, option := range options {
|
||||
//nolint:forcetypeassert
|
||||
switch option.Ident() {
|
||||
|
@ -113,10 +119,13 @@ type withKey struct {
|
|||
}
|
||||
|
||||
// WithKey is a multi-purpose option. It can be used for either jwt.Sign, jwt.Parse (and
|
||||
// its siblings), and jwt.Serializer methods.
|
||||
// its siblings), and jwt.Serializer methods. For signatures, please see the documentation
|
||||
// for `jws.WithKey` for more details. For encryption, please see the documentation
|
||||
// for `jwe.WithKey`.
|
||||
//
|
||||
// It is the caller's responsibility to match the suboptions to the operation that they
|
||||
// are performing. For example, you are not allowed to do this:
|
||||
// are performing. For example, you are not allowed to do this, because the operation
|
||||
// is to generate a signature, and yet you are passing options for jwe:
|
||||
//
|
||||
// jwt.Sign(token, jwt.WithKey(alg, key, jweOptions...))
|
||||
//
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
//go:build (darwin || freebsd || openbsd || netbsd || dragonfly || hurd) && !appengine
|
||||
//go:build (darwin || freebsd || openbsd || netbsd || dragonfly || hurd) && !appengine && !tinygo
|
||||
// +build darwin freebsd openbsd netbsd dragonfly hurd
|
||||
// +build !appengine
|
||||
// +build !tinygo
|
||||
|
||||
package isatty
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
//go:build appengine || js || nacl || wasm
|
||||
// +build appengine js nacl wasm
|
||||
//go:build (appengine || js || nacl || tinygo || wasm) && !windows
|
||||
// +build appengine js nacl tinygo wasm
|
||||
// +build !windows
|
||||
|
||||
package isatty
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
//go:build (linux || aix || zos) && !appengine
|
||||
//go:build (linux || aix || zos) && !appengine && !tinygo
|
||||
// +build linux aix zos
|
||||
// +build !appengine
|
||||
// +build !tinygo
|
||||
|
||||
package isatty
|
||||
|
||||
|
|
|
@ -1,22 +1,11 @@
|
|||
package bbolt
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
msAsync = 1 << iota // perform asynchronous writes
|
||||
msSync // perform synchronous writes
|
||||
msInvalidate // invalidate cached data
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func msync(db *DB) error {
|
||||
_, _, errno := syscall.Syscall(syscall.SYS_MSYNC, uintptr(unsafe.Pointer(db.data)), uintptr(db.datasz), msInvalidate)
|
||||
if errno != 0 {
|
||||
return errno
|
||||
}
|
||||
return nil
|
||||
return unix.Msync(db.data[:db.datasz], unix.MS_INVALIDATE)
|
||||
}
|
||||
|
||||
func fdatasync(db *DB) error {
|
||||
|
|
|
@ -57,6 +57,12 @@ const (
|
|||
// All data access is performed through transactions which can be obtained through the DB.
|
||||
// All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called.
|
||||
type DB struct {
|
||||
// Put `stats` at the first field to ensure it's 64-bit aligned. Note that
|
||||
// the first word in an allocated struct can be relied upon to be 64-bit
|
||||
// aligned. Refer to https://pkg.go.dev/sync/atomic#pkg-note-BUG. Also
|
||||
// refer to discussion in https://github.com/etcd-io/bbolt/issues/577.
|
||||
stats Stats
|
||||
|
||||
// When enabled, the database will perform a Check() after every commit.
|
||||
// A panic is issued if the database is in an inconsistent state. This
|
||||
// flag has a large performance impact so it should only be used for
|
||||
|
@ -147,7 +153,6 @@ type DB struct {
|
|||
opened bool
|
||||
rwtx *Tx
|
||||
txs []*Tx
|
||||
stats Stats
|
||||
|
||||
freelist *freelist
|
||||
freelistLoad sync.Once
|
||||
|
@ -424,7 +429,7 @@ func (db *DB) hasSyncedFreelist() bool {
|
|||
|
||||
// mmap opens the underlying memory-mapped file and initializes the meta references.
|
||||
// minsz is the minimum size that the new mmap can be.
|
||||
func (db *DB) mmap(minsz int) error {
|
||||
func (db *DB) mmap(minsz int) (err error) {
|
||||
db.mmaplock.Lock()
|
||||
defer db.mmaplock.Unlock()
|
||||
|
||||
|
@ -459,17 +464,27 @@ func (db *DB) mmap(minsz int) error {
|
|||
}
|
||||
|
||||
// Unmap existing data before continuing.
|
||||
if err := db.munmap(); err != nil {
|
||||
if err = db.munmap(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Memory-map the data file as a byte slice.
|
||||
// gofail: var mapError string
|
||||
// return errors.New(mapError)
|
||||
if err := mmap(db, size); err != nil {
|
||||
if err = mmap(db, size); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Perform unmmap on any error to reset all data fields:
|
||||
// dataref, data, datasz, meta0 and meta1.
|
||||
defer func() {
|
||||
if err != nil {
|
||||
if unmapErr := db.munmap(); unmapErr != nil {
|
||||
err = fmt.Errorf("%w; rollback unmap also failed: %v", err, unmapErr)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
if db.Mlock {
|
||||
// Don't allow swapping of data file
|
||||
if err := db.mlock(fileSize); err != nil {
|
||||
|
@ -553,6 +568,8 @@ func (db *DB) mmapSize(size int) (int, error) {
|
|||
}
|
||||
|
||||
func (db *DB) munlock(fileSize int) error {
|
||||
// gofail: var munlockError string
|
||||
// return errors.New(munlockError)
|
||||
if err := munlock(db, fileSize); err != nil {
|
||||
return fmt.Errorf("munlock error: " + err.Error())
|
||||
}
|
||||
|
@ -560,6 +577,8 @@ func (db *DB) munlock(fileSize int) error {
|
|||
}
|
||||
|
||||
func (db *DB) mlock(fileSize int) error {
|
||||
// gofail: var mlockError string
|
||||
// return errors.New(mlockError)
|
||||
if err := mlock(db, fileSize); err != nil {
|
||||
return fmt.Errorf("mlock error: " + err.Error())
|
||||
}
|
||||
|
@ -649,9 +668,10 @@ func (db *DB) close() error {
|
|||
// Clear ops.
|
||||
db.ops.writeAt = nil
|
||||
|
||||
var errs []error
|
||||
// Close the mmap.
|
||||
if err := db.munmap(); err != nil {
|
||||
return err
|
||||
errs = append(errs, err)
|
||||
}
|
||||
|
||||
// Close file handles.
|
||||
|
@ -660,18 +680,22 @@ func (db *DB) close() error {
|
|||
if !db.readOnly {
|
||||
// Unlock the file.
|
||||
if err := funlock(db); err != nil {
|
||||
return fmt.Errorf("bolt.Close(): funlock error: %w", err)
|
||||
errs = append(errs, fmt.Errorf("bolt.Close(): funlock error: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
// Close the file descriptor.
|
||||
if err := db.file.Close(); err != nil {
|
||||
return fmt.Errorf("db file close: %s", err)
|
||||
errs = append(errs, fmt.Errorf("db file close: %w", err))
|
||||
}
|
||||
db.file = nil
|
||||
}
|
||||
|
||||
db.path = ""
|
||||
|
||||
if len(errs) > 0 {
|
||||
return errs[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -1263,6 +1287,12 @@ var DefaultOptions = &Options{
|
|||
|
||||
// Stats represents statistics about the database.
|
||||
type Stats struct {
|
||||
// Put `TxStats` at the first field to ensure it's 64-bit aligned. Note
|
||||
// that the first word in an allocated struct can be relied upon to be
|
||||
// 64-bit aligned. Refer to https://pkg.go.dev/sync/atomic#pkg-note-BUG.
|
||||
// Also refer to discussion in https://github.com/etcd-io/bbolt/issues/577.
|
||||
TxStats TxStats // global, ongoing stats.
|
||||
|
||||
// Freelist stats
|
||||
FreePageN int // total number of free pages on the freelist
|
||||
PendingPageN int // total number of pending pages on the freelist
|
||||
|
@ -1272,8 +1302,6 @@ type Stats struct {
|
|||
// Transaction stats
|
||||
TxN int // total number of started read transactions
|
||||
OpenTxN int // number of currently open read transactions
|
||||
|
||||
TxStats TxStats // global, ongoing stats.
|
||||
}
|
||||
|
||||
// Sub calculates and returns the difference between two sets of database stats.
|
||||
|
|
|
@ -1,102 +0,0 @@
|
|||
// Copyright 2022 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.
|
||||
|
||||
package slog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// An Attr is a key-value pair.
|
||||
type Attr struct {
|
||||
Key string
|
||||
Value Value
|
||||
}
|
||||
|
||||
// String returns an Attr for a string value.
|
||||
func String(key, value string) Attr {
|
||||
return Attr{key, StringValue(value)}
|
||||
}
|
||||
|
||||
// Int64 returns an Attr for an int64.
|
||||
func Int64(key string, value int64) Attr {
|
||||
return Attr{key, Int64Value(value)}
|
||||
}
|
||||
|
||||
// Int converts an int to an int64 and returns
|
||||
// an Attr with that value.
|
||||
func Int(key string, value int) Attr {
|
||||
return Int64(key, int64(value))
|
||||
}
|
||||
|
||||
// Uint64 returns an Attr for a uint64.
|
||||
func Uint64(key string, v uint64) Attr {
|
||||
return Attr{key, Uint64Value(v)}
|
||||
}
|
||||
|
||||
// Float64 returns an Attr for a floating-point number.
|
||||
func Float64(key string, v float64) Attr {
|
||||
return Attr{key, Float64Value(v)}
|
||||
}
|
||||
|
||||
// Bool returns an Attr for a bool.
|
||||
func Bool(key string, v bool) Attr {
|
||||
return Attr{key, BoolValue(v)}
|
||||
}
|
||||
|
||||
// Time returns an Attr for a time.Time.
|
||||
// It discards the monotonic portion.
|
||||
func Time(key string, v time.Time) Attr {
|
||||
return Attr{key, TimeValue(v)}
|
||||
}
|
||||
|
||||
// Duration returns an Attr for a time.Duration.
|
||||
func Duration(key string, v time.Duration) Attr {
|
||||
return Attr{key, DurationValue(v)}
|
||||
}
|
||||
|
||||
// Group returns an Attr for a Group Value.
|
||||
// The first argument is the key; the remaining arguments
|
||||
// are converted to Attrs as in [Logger.Log].
|
||||
//
|
||||
// Use Group to collect several key-value pairs under a single
|
||||
// key on a log line, or as the result of LogValue
|
||||
// in order to log a single value as multiple Attrs.
|
||||
func Group(key string, args ...any) Attr {
|
||||
return Attr{key, GroupValue(argsToAttrSlice(args)...)}
|
||||
}
|
||||
|
||||
func argsToAttrSlice(args []any) []Attr {
|
||||
var (
|
||||
attr Attr
|
||||
attrs []Attr
|
||||
)
|
||||
for len(args) > 0 {
|
||||
attr, args = argsToAttr(args)
|
||||
attrs = append(attrs, attr)
|
||||
}
|
||||
return attrs
|
||||
}
|
||||
|
||||
// Any returns an Attr for the supplied value.
|
||||
// See [Value.AnyValue] for how values are treated.
|
||||
func Any(key string, value any) Attr {
|
||||
return Attr{key, AnyValue(value)}
|
||||
}
|
||||
|
||||
// Equal reports whether a and b have equal keys and values.
|
||||
func (a Attr) Equal(b Attr) bool {
|
||||
return a.Key == b.Key && a.Value.Equal(b.Value)
|
||||
}
|
||||
|
||||
func (a Attr) String() string {
|
||||
return fmt.Sprintf("%s=%s", a.Key, a.Value)
|
||||
}
|
||||
|
||||
// isEmpty reports whether a has an empty key and a nil value.
|
||||
// That can be written as Attr{} or Any("", nil).
|
||||
func (a Attr) isEmpty() bool {
|
||||
return a.Key == "" && a.Value.num == 0 && a.Value.any == nil
|
||||
}
|
|
@ -1,316 +0,0 @@
|
|||
// Copyright 2022 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.
|
||||
|
||||
/*
|
||||
Package slog provides structured logging,
|
||||
in which log records include a message,
|
||||
a severity level, and various other attributes
|
||||
expressed as key-value pairs.
|
||||
|
||||
It defines a type, [Logger],
|
||||
which provides several methods (such as [Logger.Info] and [Logger.Error])
|
||||
for reporting events of interest.
|
||||
|
||||
Each Logger is associated with a [Handler].
|
||||
A Logger output method creates a [Record] from the method arguments
|
||||
and passes it to the Handler, which decides how to handle it.
|
||||
There is a default Logger accessible through top-level functions
|
||||
(such as [Info] and [Error]) that call the corresponding Logger methods.
|
||||
|
||||
A log record consists of a time, a level, a message, and a set of key-value
|
||||
pairs, where the keys are strings and the values may be of any type.
|
||||
As an example,
|
||||
|
||||
slog.Info("hello", "count", 3)
|
||||
|
||||
creates a record containing the time of the call,
|
||||
a level of Info, the message "hello", and a single
|
||||
pair with key "count" and value 3.
|
||||
|
||||
The [Info] top-level function calls the [Logger.Info] method on the default Logger.
|
||||
In addition to [Logger.Info], there are methods for Debug, Warn and Error levels.
|
||||
Besides these convenience methods for common levels,
|
||||
there is also a [Logger.Log] method which takes the level as an argument.
|
||||
Each of these methods has a corresponding top-level function that uses the
|
||||
default logger.
|
||||
|
||||
The default handler formats the log record's message, time, level, and attributes
|
||||
as a string and passes it to the [log] package.
|
||||
|
||||
2022/11/08 15:28:26 INFO hello count=3
|
||||
|
||||
For more control over the output format, create a logger with a different handler.
|
||||
This statement uses [New] to create a new logger with a TextHandler
|
||||
that writes structured records in text form to standard error:
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
|
||||
[TextHandler] output is a sequence of key=value pairs, easily and unambiguously
|
||||
parsed by machine. This statement:
|
||||
|
||||
logger.Info("hello", "count", 3)
|
||||
|
||||
produces this output:
|
||||
|
||||
time=2022-11-08T15:28:26.000-05:00 level=INFO msg=hello count=3
|
||||
|
||||
The package also provides [JSONHandler], whose output is line-delimited JSON:
|
||||
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
logger.Info("hello", "count", 3)
|
||||
|
||||
produces this output:
|
||||
|
||||
{"time":"2022-11-08T15:28:26.000000000-05:00","level":"INFO","msg":"hello","count":3}
|
||||
|
||||
Both [TextHandler] and [JSONHandler] can be configured with [HandlerOptions].
|
||||
There are options for setting the minimum level (see Levels, below),
|
||||
displaying the source file and line of the log call, and
|
||||
modifying attributes before they are logged.
|
||||
|
||||
Setting a logger as the default with
|
||||
|
||||
slog.SetDefault(logger)
|
||||
|
||||
will cause the top-level functions like [Info] to use it.
|
||||
[SetDefault] also updates the default logger used by the [log] package,
|
||||
so that existing applications that use [log.Printf] and related functions
|
||||
will send log records to the logger's handler without needing to be rewritten.
|
||||
|
||||
Some attributes are common to many log calls.
|
||||
For example, you may wish to include the URL or trace identifier of a server request
|
||||
with all log events arising from the request.
|
||||
Rather than repeat the attribute with every log call, you can use [Logger.With]
|
||||
to construct a new Logger containing the attributes:
|
||||
|
||||
logger2 := logger.With("url", r.URL)
|
||||
|
||||
The arguments to With are the same key-value pairs used in [Logger.Info].
|
||||
The result is a new Logger with the same handler as the original, but additional
|
||||
attributes that will appear in the output of every call.
|
||||
|
||||
# Levels
|
||||
|
||||
A [Level] is an integer representing the importance or severity of a log event.
|
||||
The higher the level, the more severe the event.
|
||||
This package defines constants for the most common levels,
|
||||
but any int can be used as a level.
|
||||
|
||||
In an application, you may wish to log messages only at a certain level or greater.
|
||||
One common configuration is to log messages at Info or higher levels,
|
||||
suppressing debug logging until it is needed.
|
||||
The built-in handlers can be configured with the minimum level to output by
|
||||
setting [HandlerOptions.Level].
|
||||
The program's `main` function typically does this.
|
||||
The default value is LevelInfo.
|
||||
|
||||
Setting the [HandlerOptions.Level] field to a [Level] value
|
||||
fixes the handler's minimum level throughout its lifetime.
|
||||
Setting it to a [LevelVar] allows the level to be varied dynamically.
|
||||
A LevelVar holds a Level and is safe to read or write from multiple
|
||||
goroutines.
|
||||
To vary the level dynamically for an entire program, first initialize
|
||||
a global LevelVar:
|
||||
|
||||
var programLevel = new(slog.LevelVar) // Info by default
|
||||
|
||||
Then use the LevelVar to construct a handler, and make it the default:
|
||||
|
||||
h := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: programLevel})
|
||||
slog.SetDefault(slog.New(h))
|
||||
|
||||
Now the program can change its logging level with a single statement:
|
||||
|
||||
programLevel.Set(slog.LevelDebug)
|
||||
|
||||
# Groups
|
||||
|
||||
Attributes can be collected into groups.
|
||||
A group has a name that is used to qualify the names of its attributes.
|
||||
How this qualification is displayed depends on the handler.
|
||||
[TextHandler] separates the group and attribute names with a dot.
|
||||
[JSONHandler] treats each group as a separate JSON object, with the group name as the key.
|
||||
|
||||
Use [Group] to create a Group attribute from a name and a list of key-value pairs:
|
||||
|
||||
slog.Group("request",
|
||||
"method", r.Method,
|
||||
"url", r.URL)
|
||||
|
||||
TextHandler would display this group as
|
||||
|
||||
request.method=GET request.url=http://example.com
|
||||
|
||||
JSONHandler would display it as
|
||||
|
||||
"request":{"method":"GET","url":"http://example.com"}
|
||||
|
||||
Use [Logger.WithGroup] to qualify all of a Logger's output
|
||||
with a group name. Calling WithGroup on a Logger results in a
|
||||
new Logger with the same Handler as the original, but with all
|
||||
its attributes qualified by the group name.
|
||||
|
||||
This can help prevent duplicate attribute keys in large systems,
|
||||
where subsystems might use the same keys.
|
||||
Pass each subsystem a different Logger with its own group name so that
|
||||
potential duplicates are qualified:
|
||||
|
||||
logger := slog.Default().With("id", systemID)
|
||||
parserLogger := logger.WithGroup("parser")
|
||||
parseInput(input, parserLogger)
|
||||
|
||||
When parseInput logs with parserLogger, its keys will be qualified with "parser",
|
||||
so even if it uses the common key "id", the log line will have distinct keys.
|
||||
|
||||
# Contexts
|
||||
|
||||
Some handlers may wish to include information from the [context.Context] that is
|
||||
available at the call site. One example of such information
|
||||
is the identifier for the current span when tracing is enabled.
|
||||
|
||||
The [Logger.Log] and [Logger.LogAttrs] methods take a context as a first
|
||||
argument, as do their corresponding top-level functions.
|
||||
|
||||
Although the convenience methods on Logger (Info and so on) and the
|
||||
corresponding top-level functions do not take a context, the alternatives ending
|
||||
in "Context" do. For example,
|
||||
|
||||
slog.InfoContext(ctx, "message")
|
||||
|
||||
It is recommended to pass a context to an output method if one is available.
|
||||
|
||||
# Attrs and Values
|
||||
|
||||
An [Attr] is a key-value pair. The Logger output methods accept Attrs as well as
|
||||
alternating keys and values. The statement
|
||||
|
||||
slog.Info("hello", slog.Int("count", 3))
|
||||
|
||||
behaves the same as
|
||||
|
||||
slog.Info("hello", "count", 3)
|
||||
|
||||
There are convenience constructors for [Attr] such as [Int], [String], and [Bool]
|
||||
for common types, as well as the function [Any] for constructing Attrs of any
|
||||
type.
|
||||
|
||||
The value part of an Attr is a type called [Value].
|
||||
Like an [any], a Value can hold any Go value,
|
||||
but it can represent typical values, including all numbers and strings,
|
||||
without an allocation.
|
||||
|
||||
For the most efficient log output, use [Logger.LogAttrs].
|
||||
It is similar to [Logger.Log] but accepts only Attrs, not alternating
|
||||
keys and values; this allows it, too, to avoid allocation.
|
||||
|
||||
The call
|
||||
|
||||
logger.LogAttrs(nil, slog.LevelInfo, "hello", slog.Int("count", 3))
|
||||
|
||||
is the most efficient way to achieve the same output as
|
||||
|
||||
slog.Info("hello", "count", 3)
|
||||
|
||||
# Customizing a type's logging behavior
|
||||
|
||||
If a type implements the [LogValuer] interface, the [Value] returned from its LogValue
|
||||
method is used for logging. You can use this to control how values of the type
|
||||
appear in logs. For example, you can redact secret information like passwords,
|
||||
or gather a struct's fields in a Group. See the examples under [LogValuer] for
|
||||
details.
|
||||
|
||||
A LogValue method may return a Value that itself implements [LogValuer]. The [Value.Resolve]
|
||||
method handles these cases carefully, avoiding infinite loops and unbounded recursion.
|
||||
Handler authors and others may wish to use Value.Resolve instead of calling LogValue directly.
|
||||
|
||||
# Wrapping output methods
|
||||
|
||||
The logger functions use reflection over the call stack to find the file name
|
||||
and line number of the logging call within the application. This can produce
|
||||
incorrect source information for functions that wrap slog. For instance, if you
|
||||
define this function in file mylog.go:
|
||||
|
||||
func Infof(format string, args ...any) {
|
||||
slog.Default().Info(fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
and you call it like this in main.go:
|
||||
|
||||
Infof(slog.Default(), "hello, %s", "world")
|
||||
|
||||
then slog will report the source file as mylog.go, not main.go.
|
||||
|
||||
A correct implementation of Infof will obtain the source location
|
||||
(pc) and pass it to NewRecord.
|
||||
The Infof function in the package-level example called "wrapping"
|
||||
demonstrates how to do this.
|
||||
|
||||
# Working with Records
|
||||
|
||||
Sometimes a Handler will need to modify a Record
|
||||
before passing it on to another Handler or backend.
|
||||
A Record contains a mixture of simple public fields (e.g. Time, Level, Message)
|
||||
and hidden fields that refer to state (such as attributes) indirectly. This
|
||||
means that modifying a simple copy of a Record (e.g. by calling
|
||||
[Record.Add] or [Record.AddAttrs] to add attributes)
|
||||
may have unexpected effects on the original.
|
||||
Before modifying a Record, use [Clone] to
|
||||
create a copy that shares no state with the original,
|
||||
or create a new Record with [NewRecord]
|
||||
and build up its Attrs by traversing the old ones with [Record.Attrs].
|
||||
|
||||
# Performance considerations
|
||||
|
||||
If profiling your application demonstrates that logging is taking significant time,
|
||||
the following suggestions may help.
|
||||
|
||||
If many log lines have a common attribute, use [Logger.With] to create a Logger with
|
||||
that attribute. The built-in handlers will format that attribute only once, at the
|
||||
call to [Logger.With]. The [Handler] interface is designed to allow that optimization,
|
||||
and a well-written Handler should take advantage of it.
|
||||
|
||||
The arguments to a log call are always evaluated, even if the log event is discarded.
|
||||
If possible, defer computation so that it happens only if the value is actually logged.
|
||||
For example, consider the call
|
||||
|
||||
slog.Info("starting request", "url", r.URL.String()) // may compute String unnecessarily
|
||||
|
||||
The URL.String method will be called even if the logger discards Info-level events.
|
||||
Instead, pass the URL directly:
|
||||
|
||||
slog.Info("starting request", "url", &r.URL) // calls URL.String only if needed
|
||||
|
||||
The built-in [TextHandler] will call its String method, but only
|
||||
if the log event is enabled.
|
||||
Avoiding the call to String also preserves the structure of the underlying value.
|
||||
For example [JSONHandler] emits the components of the parsed URL as a JSON object.
|
||||
If you want to avoid eagerly paying the cost of the String call
|
||||
without causing the handler to potentially inspect the structure of the value,
|
||||
wrap the value in a fmt.Stringer implementation that hides its Marshal methods.
|
||||
|
||||
You can also use the [LogValuer] interface to avoid unnecessary work in disabled log
|
||||
calls. Say you need to log some expensive value:
|
||||
|
||||
slog.Debug("frobbing", "value", computeExpensiveValue(arg))
|
||||
|
||||
Even if this line is disabled, computeExpensiveValue will be called.
|
||||
To avoid that, define a type implementing LogValuer:
|
||||
|
||||
type expensive struct { arg int }
|
||||
|
||||
func (e expensive) LogValue() slog.Value {
|
||||
return slog.AnyValue(computeExpensiveValue(e.arg))
|
||||
}
|
||||
|
||||
Then use a value of that type in log calls:
|
||||
|
||||
slog.Debug("frobbing", "value", expensive{arg})
|
||||
|
||||
Now computeExpensiveValue will only be called when the line is enabled.
|
||||
|
||||
The built-in handlers acquire a lock before calling [io.Writer.Write]
|
||||
to ensure that each record is written in one piece. User-defined
|
||||
handlers are responsible for their own locking.
|
||||
*/
|
||||
package slog
|
|
@ -1,559 +0,0 @@
|
|||
// Copyright 2022 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.
|
||||
|
||||
package slog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/slices"
|
||||
"golang.org/x/exp/slog/internal/buffer"
|
||||
)
|
||||
|
||||
// A Handler handles log records produced by a Logger..
|
||||
//
|
||||
// A typical handler may print log records to standard error,
|
||||
// or write them to a file or database, or perhaps augment them
|
||||
// with additional attributes and pass them on to another handler.
|
||||
//
|
||||
// Any of the Handler's methods may be called concurrently with itself
|
||||
// or with other methods. It is the responsibility of the Handler to
|
||||
// manage this concurrency.
|
||||
//
|
||||
// Users of the slog package should not invoke Handler methods directly.
|
||||
// They should use the methods of [Logger] instead.
|
||||
type Handler interface {
|
||||
// Enabled reports whether the handler handles records at the given level.
|
||||
// The handler ignores records whose level is lower.
|
||||
// It is called early, before any arguments are processed,
|
||||
// to save effort if the log event should be discarded.
|
||||
// If called from a Logger method, the first argument is the context
|
||||
// passed to that method, or context.Background() if nil was passed
|
||||
// or the method does not take a context.
|
||||
// The context is passed so Enabled can use its values
|
||||
// to make a decision.
|
||||
Enabled(context.Context, Level) bool
|
||||
|
||||
// Handle handles the Record.
|
||||
// It will only be called when Enabled returns true.
|
||||
// The Context argument is as for Enabled.
|
||||
// It is present solely to provide Handlers access to the context's values.
|
||||
// Canceling the context should not affect record processing.
|
||||
// (Among other things, log messages may be necessary to debug a
|
||||
// cancellation-related problem.)
|
||||
//
|
||||
// Handle methods that produce output should observe the following rules:
|
||||
// - If r.Time is the zero time, ignore the time.
|
||||
// - If r.PC is zero, ignore it.
|
||||
// - Attr's values should be resolved.
|
||||
// - If an Attr's key and value are both the zero value, ignore the Attr.
|
||||
// This can be tested with attr.Equal(Attr{}).
|
||||
// - If a group's key is empty, inline the group's Attrs.
|
||||
// - If a group has no Attrs (even if it has a non-empty key),
|
||||
// ignore it.
|
||||
Handle(context.Context, Record) error
|
||||
|
||||
// WithAttrs returns a new Handler whose attributes consist of
|
||||
// both the receiver's attributes and the arguments.
|
||||
// The Handler owns the slice: it may retain, modify or discard it.
|
||||
WithAttrs(attrs []Attr) Handler
|
||||
|
||||
// WithGroup returns a new Handler with the given group appended to
|
||||
// the receiver's existing groups.
|
||||
// The keys of all subsequent attributes, whether added by With or in a
|
||||
// Record, should be qualified by the sequence of group names.
|
||||
//
|
||||
// How this qualification happens is up to the Handler, so long as
|
||||
// this Handler's attribute keys differ from those of another Handler
|
||||
// with a different sequence of group names.
|
||||
//
|
||||
// A Handler should treat WithGroup as starting a Group of Attrs that ends
|
||||
// at the end of the log event. That is,
|
||||
//
|
||||
// logger.WithGroup("s").LogAttrs(level, msg, slog.Int("a", 1), slog.Int("b", 2))
|
||||
//
|
||||
// should behave like
|
||||
//
|
||||
// logger.LogAttrs(level, msg, slog.Group("s", slog.Int("a", 1), slog.Int("b", 2)))
|
||||
//
|
||||
// If the name is empty, WithGroup returns the receiver.
|
||||
WithGroup(name string) Handler
|
||||
}
|
||||
|
||||
type defaultHandler struct {
|
||||
ch *commonHandler
|
||||
// log.Output, except for testing
|
||||
output func(calldepth int, message string) error
|
||||
}
|
||||
|
||||
func newDefaultHandler(output func(int, string) error) *defaultHandler {
|
||||
return &defaultHandler{
|
||||
ch: &commonHandler{json: false},
|
||||
output: output,
|
||||
}
|
||||
}
|
||||
|
||||
func (*defaultHandler) Enabled(_ context.Context, l Level) bool {
|
||||
return l >= LevelInfo
|
||||
}
|
||||
|
||||
// Collect the level, attributes and message in a string and
|
||||
// write it with the default log.Logger.
|
||||
// Let the log.Logger handle time and file/line.
|
||||
func (h *defaultHandler) Handle(ctx context.Context, r Record) error {
|
||||
buf := buffer.New()
|
||||
buf.WriteString(r.Level.String())
|
||||
buf.WriteByte(' ')
|
||||
buf.WriteString(r.Message)
|
||||
state := h.ch.newHandleState(buf, true, " ", nil)
|
||||
defer state.free()
|
||||
state.appendNonBuiltIns(r)
|
||||
|
||||
// skip [h.output, defaultHandler.Handle, handlerWriter.Write, log.Output]
|
||||
return h.output(4, buf.String())
|
||||
}
|
||||
|
||||
func (h *defaultHandler) WithAttrs(as []Attr) Handler {
|
||||
return &defaultHandler{h.ch.withAttrs(as), h.output}
|
||||
}
|
||||
|
||||
func (h *defaultHandler) WithGroup(name string) Handler {
|
||||
return &defaultHandler{h.ch.withGroup(name), h.output}
|
||||
}
|
||||
|
||||
// HandlerOptions are options for a TextHandler or JSONHandler.
|
||||
// A zero HandlerOptions consists entirely of default values.
|
||||
type HandlerOptions struct {
|
||||
// AddSource causes the handler to compute the source code position
|
||||
// of the log statement and add a SourceKey attribute to the output.
|
||||
AddSource bool
|
||||
|
||||
// Level reports the minimum record level that will be logged.
|
||||
// The handler discards records with lower levels.
|
||||
// If Level is nil, the handler assumes LevelInfo.
|
||||
// The handler calls Level.Level for each record processed;
|
||||
// to adjust the minimum level dynamically, use a LevelVar.
|
||||
Level Leveler
|
||||
|
||||
// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
|
||||
// The attribute's value has been resolved (see [Value.Resolve]).
|
||||
// If ReplaceAttr returns an Attr with Key == "", the attribute is discarded.
|
||||
//
|
||||
// The built-in attributes with keys "time", "level", "source", and "msg"
|
||||
// are passed to this function, except that time is omitted
|
||||
// if zero, and source is omitted if AddSource is false.
|
||||
//
|
||||
// The first argument is a list of currently open groups that contain the
|
||||
// Attr. It must not be retained or modified. ReplaceAttr is never called
|
||||
// for Group attributes, only their contents. For example, the attribute
|
||||
// list
|
||||
//
|
||||
// Int("a", 1), Group("g", Int("b", 2)), Int("c", 3)
|
||||
//
|
||||
// results in consecutive calls to ReplaceAttr with the following arguments:
|
||||
//
|
||||
// nil, Int("a", 1)
|
||||
// []string{"g"}, Int("b", 2)
|
||||
// nil, Int("c", 3)
|
||||
//
|
||||
// ReplaceAttr can be used to change the default keys of the built-in
|
||||
// attributes, convert types (for example, to replace a `time.Time` with the
|
||||
// integer seconds since the Unix epoch), sanitize personal information, or
|
||||
// remove attributes from the output.
|
||||
ReplaceAttr func(groups []string, a Attr) Attr
|
||||
}
|
||||
|
||||
// Keys for "built-in" attributes.
|
||||
const (
|
||||
// TimeKey is the key used by the built-in handlers for the time
|
||||
// when the log method is called. The associated Value is a [time.Time].
|
||||
TimeKey = "time"
|
||||
// LevelKey is the key used by the built-in handlers for the level
|
||||
// of the log call. The associated value is a [Level].
|
||||
LevelKey = "level"
|
||||
// MessageKey is the key used by the built-in handlers for the
|
||||
// message of the log call. The associated value is a string.
|
||||
MessageKey = "msg"
|
||||
// SourceKey is the key used by the built-in handlers for the source file
|
||||
// and line of the log call. The associated value is a string.
|
||||
SourceKey = "source"
|
||||
)
|
||||
|
||||
type commonHandler struct {
|
||||
json bool // true => output JSON; false => output text
|
||||
opts HandlerOptions
|
||||
preformattedAttrs []byte
|
||||
groupPrefix string // for text: prefix of groups opened in preformatting
|
||||
groups []string // all groups started from WithGroup
|
||||
nOpenGroups int // the number of groups opened in preformattedAttrs
|
||||
mu sync.Mutex
|
||||
w io.Writer
|
||||
}
|
||||
|
||||
func (h *commonHandler) clone() *commonHandler {
|
||||
// We can't use assignment because we can't copy the mutex.
|
||||
return &commonHandler{
|
||||
json: h.json,
|
||||
opts: h.opts,
|
||||
preformattedAttrs: slices.Clip(h.preformattedAttrs),
|
||||
groupPrefix: h.groupPrefix,
|
||||
groups: slices.Clip(h.groups),
|
||||
nOpenGroups: h.nOpenGroups,
|
||||
w: h.w,
|
||||
}
|
||||
}
|
||||
|
||||
// enabled reports whether l is greater than or equal to the
|
||||
// minimum level.
|
||||
func (h *commonHandler) enabled(l Level) bool {
|
||||
minLevel := LevelInfo
|
||||
if h.opts.Level != nil {
|
||||
minLevel = h.opts.Level.Level()
|
||||
}
|
||||
return l >= minLevel
|
||||
}
|
||||
|
||||
func (h *commonHandler) withAttrs(as []Attr) *commonHandler {
|
||||
h2 := h.clone()
|
||||
// Pre-format the attributes as an optimization.
|
||||
prefix := buffer.New()
|
||||
defer prefix.Free()
|
||||
prefix.WriteString(h.groupPrefix)
|
||||
state := h2.newHandleState((*buffer.Buffer)(&h2.preformattedAttrs), false, "", prefix)
|
||||
defer state.free()
|
||||
if len(h2.preformattedAttrs) > 0 {
|
||||
state.sep = h.attrSep()
|
||||
}
|
||||
state.openGroups()
|
||||
for _, a := range as {
|
||||
state.appendAttr(a)
|
||||
}
|
||||
// Remember the new prefix for later keys.
|
||||
h2.groupPrefix = state.prefix.String()
|
||||
// Remember how many opened groups are in preformattedAttrs,
|
||||
// so we don't open them again when we handle a Record.
|
||||
h2.nOpenGroups = len(h2.groups)
|
||||
return h2
|
||||
}
|
||||
|
||||
func (h *commonHandler) withGroup(name string) *commonHandler {
|
||||
if name == "" {
|
||||
return h
|
||||
}
|
||||
h2 := h.clone()
|
||||
h2.groups = append(h2.groups, name)
|
||||
return h2
|
||||
}
|
||||
|
||||
func (h *commonHandler) handle(r Record) error {
|
||||
state := h.newHandleState(buffer.New(), true, "", nil)
|
||||
defer state.free()
|
||||
if h.json {
|
||||
state.buf.WriteByte('{')
|
||||
}
|
||||
// Built-in attributes. They are not in a group.
|
||||
stateGroups := state.groups
|
||||
state.groups = nil // So ReplaceAttrs sees no groups instead of the pre groups.
|
||||
rep := h.opts.ReplaceAttr
|
||||
// time
|
||||
if !r.Time.IsZero() {
|
||||
key := TimeKey
|
||||
val := r.Time.Round(0) // strip monotonic to match Attr behavior
|
||||
if rep == nil {
|
||||
state.appendKey(key)
|
||||
state.appendTime(val)
|
||||
} else {
|
||||
state.appendAttr(Time(key, val))
|
||||
}
|
||||
}
|
||||
// level
|
||||
key := LevelKey
|
||||
val := r.Level
|
||||
if rep == nil {
|
||||
state.appendKey(key)
|
||||
state.appendString(val.String())
|
||||
} else {
|
||||
state.appendAttr(Any(key, val))
|
||||
}
|
||||
// source
|
||||
if h.opts.AddSource {
|
||||
state.appendAttr(Any(SourceKey, r.source()))
|
||||
}
|
||||
key = MessageKey
|
||||
msg := r.Message
|
||||
if rep == nil {
|
||||
state.appendKey(key)
|
||||
state.appendString(msg)
|
||||
} else {
|
||||
state.appendAttr(String(key, msg))
|
||||
}
|
||||
state.groups = stateGroups // Restore groups passed to ReplaceAttrs.
|
||||
state.appendNonBuiltIns(r)
|
||||
state.buf.WriteByte('\n')
|
||||
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
_, err := h.w.Write(*state.buf)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *handleState) appendNonBuiltIns(r Record) {
|
||||
// preformatted Attrs
|
||||
if len(s.h.preformattedAttrs) > 0 {
|
||||
s.buf.WriteString(s.sep)
|
||||
s.buf.Write(s.h.preformattedAttrs)
|
||||
s.sep = s.h.attrSep()
|
||||
}
|
||||
// Attrs in Record -- unlike the built-in ones, they are in groups started
|
||||
// from WithGroup.
|
||||
s.prefix = buffer.New()
|
||||
defer s.prefix.Free()
|
||||
s.prefix.WriteString(s.h.groupPrefix)
|
||||
s.openGroups()
|
||||
r.Attrs(func(a Attr) bool {
|
||||
s.appendAttr(a)
|
||||
return true
|
||||
})
|
||||
if s.h.json {
|
||||
// Close all open groups.
|
||||
for range s.h.groups {
|
||||
s.buf.WriteByte('}')
|
||||
}
|
||||
// Close the top-level object.
|
||||
s.buf.WriteByte('}')
|
||||
}
|
||||
}
|
||||
|
||||
// attrSep returns the separator between attributes.
|
||||
func (h *commonHandler) attrSep() string {
|
||||
if h.json {
|
||||
return ","
|
||||
}
|
||||
return " "
|
||||
}
|
||||
|
||||
// handleState holds state for a single call to commonHandler.handle.
|
||||
// The initial value of sep determines whether to emit a separator
|
||||
// before the next key, after which it stays true.
|
||||
type handleState struct {
|
||||
h *commonHandler
|
||||
buf *buffer.Buffer
|
||||
freeBuf bool // should buf be freed?
|
||||
sep string // separator to write before next key
|
||||
prefix *buffer.Buffer // for text: key prefix
|
||||
groups *[]string // pool-allocated slice of active groups, for ReplaceAttr
|
||||
}
|
||||
|
||||
var groupPool = sync.Pool{New: func() any {
|
||||
s := make([]string, 0, 10)
|
||||
return &s
|
||||
}}
|
||||
|
||||
func (h *commonHandler) newHandleState(buf *buffer.Buffer, freeBuf bool, sep string, prefix *buffer.Buffer) handleState {
|
||||
s := handleState{
|
||||
h: h,
|
||||
buf: buf,
|
||||
freeBuf: freeBuf,
|
||||
sep: sep,
|
||||
prefix: prefix,
|
||||
}
|
||||
if h.opts.ReplaceAttr != nil {
|
||||
s.groups = groupPool.Get().(*[]string)
|
||||
*s.groups = append(*s.groups, h.groups[:h.nOpenGroups]...)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *handleState) free() {
|
||||
if s.freeBuf {
|
||||
s.buf.Free()
|
||||
}
|
||||
if gs := s.groups; gs != nil {
|
||||
*gs = (*gs)[:0]
|
||||
groupPool.Put(gs)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *handleState) openGroups() {
|
||||
for _, n := range s.h.groups[s.h.nOpenGroups:] {
|
||||
s.openGroup(n)
|
||||
}
|
||||
}
|
||||
|
||||
// Separator for group names and keys.
|
||||
const keyComponentSep = '.'
|
||||
|
||||
// openGroup starts a new group of attributes
|
||||
// with the given name.
|
||||
func (s *handleState) openGroup(name string) {
|
||||
if s.h.json {
|
||||
s.appendKey(name)
|
||||
s.buf.WriteByte('{')
|
||||
s.sep = ""
|
||||
} else {
|
||||
s.prefix.WriteString(name)
|
||||
s.prefix.WriteByte(keyComponentSep)
|
||||
}
|
||||
// Collect group names for ReplaceAttr.
|
||||
if s.groups != nil {
|
||||
*s.groups = append(*s.groups, name)
|
||||
}
|
||||
}
|
||||
|
||||
// closeGroup ends the group with the given name.
|
||||
func (s *handleState) closeGroup(name string) {
|
||||
if s.h.json {
|
||||
s.buf.WriteByte('}')
|
||||
} else {
|
||||
(*s.prefix) = (*s.prefix)[:len(*s.prefix)-len(name)-1 /* for keyComponentSep */]
|
||||
}
|
||||
s.sep = s.h.attrSep()
|
||||
if s.groups != nil {
|
||||
*s.groups = (*s.groups)[:len(*s.groups)-1]
|
||||
}
|
||||
}
|
||||
|
||||
// appendAttr appends the Attr's key and value using app.
|
||||
// It handles replacement and checking for an empty key.
|
||||
// after replacement).
|
||||
func (s *handleState) appendAttr(a Attr) {
|
||||
if rep := s.h.opts.ReplaceAttr; rep != nil && a.Value.Kind() != KindGroup {
|
||||
var gs []string
|
||||
if s.groups != nil {
|
||||
gs = *s.groups
|
||||
}
|
||||
// Resolve before calling ReplaceAttr, so the user doesn't have to.
|
||||
a.Value = a.Value.Resolve()
|
||||
a = rep(gs, a)
|
||||
}
|
||||
a.Value = a.Value.Resolve()
|
||||
// Elide empty Attrs.
|
||||
if a.isEmpty() {
|
||||
return
|
||||
}
|
||||
// Special case: Source.
|
||||
if v := a.Value; v.Kind() == KindAny {
|
||||
if src, ok := v.Any().(*Source); ok {
|
||||
if s.h.json {
|
||||
a.Value = src.group()
|
||||
} else {
|
||||
a.Value = StringValue(fmt.Sprintf("%s:%d", src.File, src.Line))
|
||||
}
|
||||
}
|
||||
}
|
||||
if a.Value.Kind() == KindGroup {
|
||||
attrs := a.Value.Group()
|
||||
// Output only non-empty groups.
|
||||
if len(attrs) > 0 {
|
||||
// Inline a group with an empty key.
|
||||
if a.Key != "" {
|
||||
s.openGroup(a.Key)
|
||||
}
|
||||
for _, aa := range attrs {
|
||||
s.appendAttr(aa)
|
||||
}
|
||||
if a.Key != "" {
|
||||
s.closeGroup(a.Key)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
s.appendKey(a.Key)
|
||||
s.appendValue(a.Value)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *handleState) appendError(err error) {
|
||||
s.appendString(fmt.Sprintf("!ERROR:%v", err))
|
||||
}
|
||||
|
||||
func (s *handleState) appendKey(key string) {
|
||||
s.buf.WriteString(s.sep)
|
||||
if s.prefix != nil {
|
||||
// TODO: optimize by avoiding allocation.
|
||||
s.appendString(string(*s.prefix) + key)
|
||||
} else {
|
||||
s.appendString(key)
|
||||
}
|
||||
if s.h.json {
|
||||
s.buf.WriteByte(':')
|
||||
} else {
|
||||
s.buf.WriteByte('=')
|
||||
}
|
||||
s.sep = s.h.attrSep()
|
||||
}
|
||||
|
||||
func (s *handleState) appendString(str string) {
|
||||
if s.h.json {
|
||||
s.buf.WriteByte('"')
|
||||
*s.buf = appendEscapedJSONString(*s.buf, str)
|
||||
s.buf.WriteByte('"')
|
||||
} else {
|
||||
// text
|
||||
if needsQuoting(str) {
|
||||
*s.buf = strconv.AppendQuote(*s.buf, str)
|
||||
} else {
|
||||
s.buf.WriteString(str)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *handleState) appendValue(v Value) {
|
||||
var err error
|
||||
if s.h.json {
|
||||
err = appendJSONValue(s, v)
|
||||
} else {
|
||||
err = appendTextValue(s, v)
|
||||
}
|
||||
if err != nil {
|
||||
s.appendError(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *handleState) appendTime(t time.Time) {
|
||||
if s.h.json {
|
||||
appendJSONTime(s, t)
|
||||
} else {
|
||||
writeTimeRFC3339Millis(s.buf, t)
|
||||
}
|
||||
}
|
||||
|
||||
// This takes half the time of Time.AppendFormat.
|
||||
func writeTimeRFC3339Millis(buf *buffer.Buffer, t time.Time) {
|
||||
year, month, day := t.Date()
|
||||
buf.WritePosIntWidth(year, 4)
|
||||
buf.WriteByte('-')
|
||||
buf.WritePosIntWidth(int(month), 2)
|
||||
buf.WriteByte('-')
|
||||
buf.WritePosIntWidth(day, 2)
|
||||
buf.WriteByte('T')
|
||||
hour, min, sec := t.Clock()
|
||||
buf.WritePosIntWidth(hour, 2)
|
||||
buf.WriteByte(':')
|
||||
buf.WritePosIntWidth(min, 2)
|
||||
buf.WriteByte(':')
|
||||
buf.WritePosIntWidth(sec, 2)
|
||||
ns := t.Nanosecond()
|
||||
buf.WriteByte('.')
|
||||
buf.WritePosIntWidth(ns/1e6, 3)
|
||||
_, offsetSeconds := t.Zone()
|
||||
if offsetSeconds == 0 {
|
||||
buf.WriteByte('Z')
|
||||
} else {
|
||||
offsetMinutes := offsetSeconds / 60
|
||||
if offsetMinutes < 0 {
|
||||
buf.WriteByte('-')
|
||||
offsetMinutes = -offsetMinutes
|
||||
} else {
|
||||
buf.WriteByte('+')
|
||||
}
|
||||
buf.WritePosIntWidth(offsetMinutes/60, 2)
|
||||
buf.WriteByte(':')
|
||||
buf.WritePosIntWidth(offsetMinutes%60, 2)
|
||||
}
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
// Copyright 2022 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.
|
||||
|
||||
// Package buffer provides a pool-allocated byte buffer.
|
||||
package buffer
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Buffer adapted from go/src/fmt/print.go
|
||||
type Buffer []byte
|
||||
|
||||
// Having an initial size gives a dramatic speedup.
|
||||
var bufPool = sync.Pool{
|
||||
New: func() any {
|
||||
b := make([]byte, 0, 1024)
|
||||
return (*Buffer)(&b)
|
||||
},
|
||||
}
|
||||
|
||||
func New() *Buffer {
|
||||
return bufPool.Get().(*Buffer)
|
||||
}
|
||||
|
||||
func (b *Buffer) Free() {
|
||||
// To reduce peak allocation, return only smaller buffers to the pool.
|
||||
const maxBufferSize = 16 << 10
|
||||
if cap(*b) <= maxBufferSize {
|
||||
*b = (*b)[:0]
|
||||
bufPool.Put(b)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Buffer) Reset() {
|
||||
*b = (*b)[:0]
|
||||
}
|
||||
|
||||
func (b *Buffer) Write(p []byte) (int, error) {
|
||||
*b = append(*b, p...)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (b *Buffer) WriteString(s string) {
|
||||
*b = append(*b, s...)
|
||||
}
|
||||
|
||||
func (b *Buffer) WriteByte(c byte) {
|
||||
*b = append(*b, c)
|
||||
}
|
||||
|
||||
func (b *Buffer) WritePosInt(i int) {
|
||||
b.WritePosIntWidth(i, 0)
|
||||
}
|
||||
|
||||
// WritePosIntWidth writes non-negative integer i to the buffer, padded on the left
|
||||
// by zeroes to the given width. Use a width of 0 to omit padding.
|
||||
func (b *Buffer) WritePosIntWidth(i, width int) {
|
||||
// Cheap integer to fixed-width decimal ASCII.
|
||||
// Copied from log/log.go.
|
||||
|
||||
if i < 0 {
|
||||
panic("negative int")
|
||||
}
|
||||
|
||||
// Assemble decimal in reverse order.
|
||||
var bb [20]byte
|
||||
bp := len(bb) - 1
|
||||
for i >= 10 || width > 1 {
|
||||
width--
|
||||
q := i / 10
|
||||
bb[bp] = byte('0' + i - q*10)
|
||||
bp--
|
||||
i = q
|
||||
}
|
||||
// i < 10
|
||||
bb[bp] = byte('0' + i)
|
||||
b.Write(bb[bp:])
|
||||
}
|
||||
|
||||
func (b *Buffer) String() string {
|
||||
return string(*b)
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
// Copyright 2023 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.
|
||||
|
||||
package internal
|
||||
|
||||
// If IgnorePC is true, do not invoke runtime.Callers to get the pc.
|
||||
// This is solely for benchmarking the slowdown from runtime.Callers.
|
||||
var IgnorePC = false
|
|
@ -1,336 +0,0 @@
|
|||
// Copyright 2022 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.
|
||||
|
||||
package slog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/exp/slog/internal/buffer"
|
||||
)
|
||||
|
||||
// JSONHandler is a Handler that writes Records to an io.Writer as
|
||||
// line-delimited JSON objects.
|
||||
type JSONHandler struct {
|
||||
*commonHandler
|
||||
}
|
||||
|
||||
// NewJSONHandler creates a JSONHandler that writes to w,
|
||||
// using the given options.
|
||||
// If opts is nil, the default options are used.
|
||||
func NewJSONHandler(w io.Writer, opts *HandlerOptions) *JSONHandler {
|
||||
if opts == nil {
|
||||
opts = &HandlerOptions{}
|
||||
}
|
||||
return &JSONHandler{
|
||||
&commonHandler{
|
||||
json: true,
|
||||
w: w,
|
||||
opts: *opts,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Enabled reports whether the handler handles records at the given level.
|
||||
// The handler ignores records whose level is lower.
|
||||
func (h *JSONHandler) Enabled(_ context.Context, level Level) bool {
|
||||
return h.commonHandler.enabled(level)
|
||||
}
|
||||
|
||||
// WithAttrs returns a new JSONHandler whose attributes consists
|
||||
// of h's attributes followed by attrs.
|
||||
func (h *JSONHandler) WithAttrs(attrs []Attr) Handler {
|
||||
return &JSONHandler{commonHandler: h.commonHandler.withAttrs(attrs)}
|
||||
}
|
||||
|
||||
func (h *JSONHandler) WithGroup(name string) Handler {
|
||||
return &JSONHandler{commonHandler: h.commonHandler.withGroup(name)}
|
||||
}
|
||||
|
||||
// Handle formats its argument Record as a JSON object on a single line.
|
||||
//
|
||||
// If the Record's time is zero, the time is omitted.
|
||||
// Otherwise, the key is "time"
|
||||
// and the value is output as with json.Marshal.
|
||||
//
|
||||
// If the Record's level is zero, the level is omitted.
|
||||
// Otherwise, the key is "level"
|
||||
// and the value of [Level.String] is output.
|
||||
//
|
||||
// If the AddSource option is set and source information is available,
|
||||
// the key is "source"
|
||||
// and the value is output as "FILE:LINE".
|
||||
//
|
||||
// The message's key is "msg".
|
||||
//
|
||||
// To modify these or other attributes, or remove them from the output, use
|
||||
// [HandlerOptions.ReplaceAttr].
|
||||
//
|
||||
// Values are formatted as with an [encoding/json.Encoder] with SetEscapeHTML(false),
|
||||
// with two exceptions.
|
||||
//
|
||||
// First, an Attr whose Value is of type error is formatted as a string, by
|
||||
// calling its Error method. Only errors in Attrs receive this special treatment,
|
||||
// not errors embedded in structs, slices, maps or other data structures that
|
||||
// are processed by the encoding/json package.
|
||||
//
|
||||
// Second, an encoding failure does not cause Handle to return an error.
|
||||
// Instead, the error message is formatted as a string.
|
||||
//
|
||||
// Each call to Handle results in a single serialized call to io.Writer.Write.
|
||||
func (h *JSONHandler) Handle(_ context.Context, r Record) error {
|
||||
return h.commonHandler.handle(r)
|
||||
}
|
||||
|
||||
// Adapted from time.Time.MarshalJSON to avoid allocation.
|
||||
func appendJSONTime(s *handleState, t time.Time) {
|
||||
if y := t.Year(); y < 0 || y >= 10000 {
|
||||
// RFC 3339 is clear that years are 4 digits exactly.
|
||||
// See golang.org/issue/4556#c15 for more discussion.
|
||||
s.appendError(errors.New("time.Time year outside of range [0,9999]"))
|
||||
}
|
||||
s.buf.WriteByte('"')
|
||||
*s.buf = t.AppendFormat(*s.buf, time.RFC3339Nano)
|
||||
s.buf.WriteByte('"')
|
||||
}
|
||||
|
||||
func appendJSONValue(s *handleState, v Value) error {
|
||||
switch v.Kind() {
|
||||
case KindString:
|
||||
s.appendString(v.str())
|
||||
case KindInt64:
|
||||
*s.buf = strconv.AppendInt(*s.buf, v.Int64(), 10)
|
||||
case KindUint64:
|
||||
*s.buf = strconv.AppendUint(*s.buf, v.Uint64(), 10)
|
||||
case KindFloat64:
|
||||
// json.Marshal is funny about floats; it doesn't
|
||||
// always match strconv.AppendFloat. So just call it.
|
||||
// That's expensive, but floats are rare.
|
||||
if err := appendJSONMarshal(s.buf, v.Float64()); err != nil {
|
||||
return err
|
||||
}
|
||||
case KindBool:
|
||||
*s.buf = strconv.AppendBool(*s.buf, v.Bool())
|
||||
case KindDuration:
|
||||
// Do what json.Marshal does.
|
||||
*s.buf = strconv.AppendInt(*s.buf, int64(v.Duration()), 10)
|
||||
case KindTime:
|
||||
s.appendTime(v.Time())
|
||||
case KindAny:
|
||||
a := v.Any()
|
||||
_, jm := a.(json.Marshaler)
|
||||
if err, ok := a.(error); ok && !jm {
|
||||
s.appendString(err.Error())
|
||||
} else {
|
||||
return appendJSONMarshal(s.buf, a)
|
||||
}
|
||||
default:
|
||||
panic(fmt.Sprintf("bad kind: %s", v.Kind()))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func appendJSONMarshal(buf *buffer.Buffer, v any) error {
|
||||
// Use a json.Encoder to avoid escaping HTML.
|
||||
var bb bytes.Buffer
|
||||
enc := json.NewEncoder(&bb)
|
||||
enc.SetEscapeHTML(false)
|
||||
if err := enc.Encode(v); err != nil {
|
||||
return err
|
||||
}
|
||||
bs := bb.Bytes()
|
||||
buf.Write(bs[:len(bs)-1]) // remove final newline
|
||||
return nil
|
||||
}
|
||||
|
||||
// appendEscapedJSONString escapes s for JSON and appends it to buf.
|
||||
// It does not surround the string in quotation marks.
|
||||
//
|
||||
// Modified from encoding/json/encode.go:encodeState.string,
|
||||
// with escapeHTML set to false.
|
||||
func appendEscapedJSONString(buf []byte, s string) []byte {
|
||||
char := func(b byte) { buf = append(buf, b) }
|
||||
str := func(s string) { buf = append(buf, s...) }
|
||||
|
||||
start := 0
|
||||
for i := 0; i < len(s); {
|
||||
if b := s[i]; b < utf8.RuneSelf {
|
||||
if safeSet[b] {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if start < i {
|
||||
str(s[start:i])
|
||||
}
|
||||
char('\\')
|
||||
switch b {
|
||||
case '\\', '"':
|
||||
char(b)
|
||||
case '\n':
|
||||
char('n')
|
||||
case '\r':
|
||||
char('r')
|
||||
case '\t':
|
||||
char('t')
|
||||
default:
|
||||
// This encodes bytes < 0x20 except for \t, \n and \r.
|
||||
str(`u00`)
|
||||
char(hex[b>>4])
|
||||
char(hex[b&0xF])
|
||||
}
|
||||
i++
|
||||
start = i
|
||||
continue
|
||||
}
|
||||
c, size := utf8.DecodeRuneInString(s[i:])
|
||||
if c == utf8.RuneError && size == 1 {
|
||||
if start < i {
|
||||
str(s[start:i])
|
||||
}
|
||||
str(`\ufffd`)
|
||||
i += size
|
||||
start = i
|
||||
continue
|
||||
}
|
||||
// U+2028 is LINE SEPARATOR.
|
||||
// U+2029 is PARAGRAPH SEPARATOR.
|
||||
// They are both technically valid characters in JSON strings,
|
||||
// but don't work in JSONP, which has to be evaluated as JavaScript,
|
||||
// and can lead to security holes there. It is valid JSON to
|
||||
// escape them, so we do so unconditionally.
|
||||
// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
|
||||
if c == '\u2028' || c == '\u2029' {
|
||||
if start < i {
|
||||
str(s[start:i])
|
||||
}
|
||||
str(`\u202`)
|
||||
char(hex[c&0xF])
|
||||
i += size
|
||||
start = i
|
||||
continue
|
||||
}
|
||||
i += size
|
||||
}
|
||||
if start < len(s) {
|
||||
str(s[start:])
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
var hex = "0123456789abcdef"
|
||||
|
||||
// Copied from encoding/json/tables.go.
|
||||
//
|
||||
// safeSet holds the value true if the ASCII character with the given array
|
||||
// position can be represented inside a JSON string without any further
|
||||
// escaping.
|
||||
//
|
||||
// All values are true except for the ASCII control characters (0-31), the
|
||||
// double quote ("), and the backslash character ("\").
|
||||
var safeSet = [utf8.RuneSelf]bool{
|
||||
' ': true,
|
||||
'!': true,
|
||||
'"': false,
|
||||
'#': true,
|
||||
'$': true,
|
||||
'%': true,
|
||||
'&': true,
|
||||
'\'': true,
|
||||
'(': true,
|
||||
')': true,
|
||||
'*': true,
|
||||
'+': true,
|
||||
',': true,
|
||||
'-': true,
|
||||
'.': true,
|
||||
'/': true,
|
||||
'0': true,
|
||||
'1': true,
|
||||
'2': true,
|
||||
'3': true,
|
||||
'4': true,
|
||||
'5': true,
|
||||
'6': true,
|
||||
'7': true,
|
||||
'8': true,
|
||||
'9': true,
|
||||
':': true,
|
||||
';': true,
|
||||
'<': true,
|
||||
'=': true,
|
||||
'>': true,
|
||||
'?': true,
|
||||
'@': true,
|
||||
'A': true,
|
||||
'B': true,
|
||||
'C': true,
|
||||
'D': true,
|
||||
'E': true,
|
||||
'F': true,
|
||||
'G': true,
|
||||
'H': true,
|
||||
'I': true,
|
||||
'J': true,
|
||||
'K': true,
|
||||
'L': true,
|
||||
'M': true,
|
||||
'N': true,
|
||||
'O': true,
|
||||
'P': true,
|
||||
'Q': true,
|
||||
'R': true,
|
||||
'S': true,
|
||||
'T': true,
|
||||
'U': true,
|
||||
'V': true,
|
||||
'W': true,
|
||||
'X': true,
|
||||
'Y': true,
|
||||
'Z': true,
|
||||
'[': true,
|
||||
'\\': false,
|
||||
']': true,
|
||||
'^': true,
|
||||
'_': true,
|
||||
'`': true,
|
||||
'a': true,
|
||||
'b': true,
|
||||
'c': true,
|
||||
'd': true,
|
||||
'e': true,
|
||||
'f': true,
|
||||
'g': true,
|
||||
'h': true,
|
||||
'i': true,
|
||||
'j': true,
|
||||
'k': true,
|
||||
'l': true,
|
||||
'm': true,
|
||||
'n': true,
|
||||
'o': true,
|
||||
'p': true,
|
||||
'q': true,
|
||||
'r': true,
|
||||
's': true,
|
||||
't': true,
|
||||
'u': true,
|
||||
'v': true,
|
||||
'w': true,
|
||||
'x': true,
|
||||
'y': true,
|
||||
'z': true,
|
||||
'{': true,
|
||||
'|': true,
|
||||
'}': true,
|
||||
'~': true,
|
||||
'\u007f': true,
|
||||
}
|
|
@ -1,201 +0,0 @@
|
|||
// Copyright 2022 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.
|
||||
|
||||
package slog
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// A Level is the importance or severity of a log event.
|
||||
// The higher the level, the more important or severe the event.
|
||||
type Level int
|
||||
|
||||
// Level numbers are inherently arbitrary,
|
||||
// but we picked them to satisfy three constraints.
|
||||
// Any system can map them to another numbering scheme if it wishes.
|
||||
//
|
||||
// First, we wanted the default level to be Info, Since Levels are ints, Info is
|
||||
// the default value for int, zero.
|
||||
//
|
||||
|
||||
// Second, we wanted to make it easy to use levels to specify logger verbosity.
|
||||
// Since a larger level means a more severe event, a logger that accepts events
|
||||
// with smaller (or more negative) level means a more verbose logger. Logger
|
||||
// verbosity is thus the negation of event severity, and the default verbosity
|
||||
// of 0 accepts all events at least as severe as INFO.
|
||||
//
|
||||
// Third, we wanted some room between levels to accommodate schemes with named
|
||||
// levels between ours. For example, Google Cloud Logging defines a Notice level
|
||||
// between Info and Warn. Since there are only a few of these intermediate
|
||||
// levels, the gap between the numbers need not be large. Our gap of 4 matches
|
||||
// OpenTelemetry's mapping. Subtracting 9 from an OpenTelemetry level in the
|
||||
// DEBUG, INFO, WARN and ERROR ranges converts it to the corresponding slog
|
||||
// Level range. OpenTelemetry also has the names TRACE and FATAL, which slog
|
||||
// does not. But those OpenTelemetry levels can still be represented as slog
|
||||
// Levels by using the appropriate integers.
|
||||
//
|
||||
// Names for common levels.
|
||||
const (
|
||||
LevelDebug Level = -4
|
||||
LevelInfo Level = 0
|
||||
LevelWarn Level = 4
|
||||
LevelError Level = 8
|
||||
)
|
||||
|
||||
// String returns a name for the level.
|
||||
// If the level has a name, then that name
|
||||
// in uppercase is returned.
|
||||
// If the level is between named values, then
|
||||
// an integer is appended to the uppercased name.
|
||||
// Examples:
|
||||
//
|
||||
// LevelWarn.String() => "WARN"
|
||||
// (LevelInfo+2).String() => "INFO+2"
|
||||
func (l Level) String() string {
|
||||
str := func(base string, val Level) string {
|
||||
if val == 0 {
|
||||
return base
|
||||
}
|
||||
return fmt.Sprintf("%s%+d", base, val)
|
||||
}
|
||||
|
||||
switch {
|
||||
case l < LevelInfo:
|
||||
return str("DEBUG", l-LevelDebug)
|
||||
case l < LevelWarn:
|
||||
return str("INFO", l-LevelInfo)
|
||||
case l < LevelError:
|
||||
return str("WARN", l-LevelWarn)
|
||||
default:
|
||||
return str("ERROR", l-LevelError)
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON implements [encoding/json.Marshaler]
|
||||
// by quoting the output of [Level.String].
|
||||
func (l Level) MarshalJSON() ([]byte, error) {
|
||||
// AppendQuote is sufficient for JSON-encoding all Level strings.
|
||||
// They don't contain any runes that would produce invalid JSON
|
||||
// when escaped.
|
||||
return strconv.AppendQuote(nil, l.String()), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements [encoding/json.Unmarshaler]
|
||||
// It accepts any string produced by [Level.MarshalJSON],
|
||||
// ignoring case.
|
||||
// It also accepts numeric offsets that would result in a different string on
|
||||
// output. For example, "Error-8" would marshal as "INFO".
|
||||
func (l *Level) UnmarshalJSON(data []byte) error {
|
||||
s, err := strconv.Unquote(string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return l.parse(s)
|
||||
}
|
||||
|
||||
// MarshalText implements [encoding.TextMarshaler]
|
||||
// by calling [Level.String].
|
||||
func (l Level) MarshalText() ([]byte, error) {
|
||||
return []byte(l.String()), nil
|
||||
}
|
||||
|
||||
// UnmarshalText implements [encoding.TextUnmarshaler].
|
||||
// It accepts any string produced by [Level.MarshalText],
|
||||
// ignoring case.
|
||||
// It also accepts numeric offsets that would result in a different string on
|
||||
// output. For example, "Error-8" would marshal as "INFO".
|
||||
func (l *Level) UnmarshalText(data []byte) error {
|
||||
return l.parse(string(data))
|
||||
}
|
||||
|
||||
func (l *Level) parse(s string) (err error) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("slog: level string %q: %w", s, err)
|
||||
}
|
||||
}()
|
||||
|
||||
name := s
|
||||
offset := 0
|
||||
if i := strings.IndexAny(s, "+-"); i >= 0 {
|
||||
name = s[:i]
|
||||
offset, err = strconv.Atoi(s[i:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
switch strings.ToUpper(name) {
|
||||
case "DEBUG":
|
||||
*l = LevelDebug
|
||||
case "INFO":
|
||||
*l = LevelInfo
|
||||
case "WARN":
|
||||
*l = LevelWarn
|
||||
case "ERROR":
|
||||
*l = LevelError
|
||||
default:
|
||||
return errors.New("unknown name")
|
||||
}
|
||||
*l += Level(offset)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Level returns the receiver.
|
||||
// It implements Leveler.
|
||||
func (l Level) Level() Level { return l }
|
||||
|
||||
// A LevelVar is a Level variable, to allow a Handler level to change
|
||||
// dynamically.
|
||||
// It implements Leveler as well as a Set method,
|
||||
// and it is safe for use by multiple goroutines.
|
||||
// The zero LevelVar corresponds to LevelInfo.
|
||||
type LevelVar struct {
|
||||
val atomic.Int64
|
||||
}
|
||||
|
||||
// Level returns v's level.
|
||||
func (v *LevelVar) Level() Level {
|
||||
return Level(int(v.val.Load()))
|
||||
}
|
||||
|
||||
// Set sets v's level to l.
|
||||
func (v *LevelVar) Set(l Level) {
|
||||
v.val.Store(int64(l))
|
||||
}
|
||||
|
||||
func (v *LevelVar) String() string {
|
||||
return fmt.Sprintf("LevelVar(%s)", v.Level())
|
||||
}
|
||||
|
||||
// MarshalText implements [encoding.TextMarshaler]
|
||||
// by calling [Level.MarshalText].
|
||||
func (v *LevelVar) MarshalText() ([]byte, error) {
|
||||
return v.Level().MarshalText()
|
||||
}
|
||||
|
||||
// UnmarshalText implements [encoding.TextUnmarshaler]
|
||||
// by calling [Level.UnmarshalText].
|
||||
func (v *LevelVar) UnmarshalText(data []byte) error {
|
||||
var l Level
|
||||
if err := l.UnmarshalText(data); err != nil {
|
||||
return err
|
||||
}
|
||||
v.Set(l)
|
||||
return nil
|
||||
}
|
||||
|
||||
// A Leveler provides a Level value.
|
||||
//
|
||||
// As Level itself implements Leveler, clients typically supply
|
||||
// a Level value wherever a Leveler is needed, such as in HandlerOptions.
|
||||
// Clients who need to vary the level dynamically can provide a more complex
|
||||
// Leveler implementation such as *LevelVar.
|
||||
type Leveler interface {
|
||||
Level() Level
|
||||
}
|
|
@ -1,343 +0,0 @@
|
|||
// Copyright 2022 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.
|
||||
|
||||
package slog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"runtime"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/slog/internal"
|
||||
)
|
||||
|
||||
var defaultLogger atomic.Value
|
||||
|
||||
func init() {
|
||||
defaultLogger.Store(New(newDefaultHandler(log.Output)))
|
||||
}
|
||||
|
||||
// Default returns the default Logger.
|
||||
func Default() *Logger { return defaultLogger.Load().(*Logger) }
|
||||
|
||||
// SetDefault makes l the default Logger.
|
||||
// After this call, output from the log package's default Logger
|
||||
// (as with [log.Print], etc.) will be logged at LevelInfo using l's Handler.
|
||||
func SetDefault(l *Logger) {
|
||||
defaultLogger.Store(l)
|
||||
// If the default's handler is a defaultHandler, then don't use a handleWriter,
|
||||
// or we'll deadlock as they both try to acquire the log default mutex.
|
||||
// The defaultHandler will use whatever the log default writer is currently
|
||||
// set to, which is correct.
|
||||
// This can occur with SetDefault(Default()).
|
||||
// See TestSetDefault.
|
||||
if _, ok := l.Handler().(*defaultHandler); !ok {
|
||||
capturePC := log.Flags()&(log.Lshortfile|log.Llongfile) != 0
|
||||
log.SetOutput(&handlerWriter{l.Handler(), LevelInfo, capturePC})
|
||||
log.SetFlags(0) // we want just the log message, no time or location
|
||||
}
|
||||
}
|
||||
|
||||
// handlerWriter is an io.Writer that calls a Handler.
|
||||
// It is used to link the default log.Logger to the default slog.Logger.
|
||||
type handlerWriter struct {
|
||||
h Handler
|
||||
level Level
|
||||
capturePC bool
|
||||
}
|
||||
|
||||
func (w *handlerWriter) Write(buf []byte) (int, error) {
|
||||
if !w.h.Enabled(context.Background(), w.level) {
|
||||
return 0, nil
|
||||
}
|
||||
var pc uintptr
|
||||
if !internal.IgnorePC && w.capturePC {
|
||||
// skip [runtime.Callers, w.Write, Logger.Output, log.Print]
|
||||
var pcs [1]uintptr
|
||||
runtime.Callers(4, pcs[:])
|
||||
pc = pcs[0]
|
||||
}
|
||||
|
||||
// Remove final newline.
|
||||
origLen := len(buf) // Report that the entire buf was written.
|
||||
if len(buf) > 0 && buf[len(buf)-1] == '\n' {
|
||||
buf = buf[:len(buf)-1]
|
||||
}
|
||||
r := NewRecord(time.Now(), w.level, string(buf), pc)
|
||||
return origLen, w.h.Handle(context.Background(), r)
|
||||
}
|
||||
|
||||
// A Logger records structured information about each call to its
|
||||
// Log, Debug, Info, Warn, and Error methods.
|
||||
// For each call, it creates a Record and passes it to a Handler.
|
||||
//
|
||||
// To create a new Logger, call [New] or a Logger method
|
||||
// that begins "With".
|
||||
type Logger struct {
|
||||
handler Handler // for structured logging
|
||||
}
|
||||
|
||||
func (l *Logger) clone() *Logger {
|
||||
c := *l
|
||||
return &c
|
||||
}
|
||||
|
||||
// Handler returns l's Handler.
|
||||
func (l *Logger) Handler() Handler { return l.handler }
|
||||
|
||||
// With returns a new Logger that includes the given arguments, converted to
|
||||
// Attrs as in [Logger.Log].
|
||||
// The Attrs will be added to each output from the Logger.
|
||||
// The new Logger shares the old Logger's context.
|
||||
// The new Logger's handler is the result of calling WithAttrs on the receiver's
|
||||
// handler.
|
||||
func (l *Logger) With(args ...any) *Logger {
|
||||
c := l.clone()
|
||||
c.handler = l.handler.WithAttrs(argsToAttrSlice(args))
|
||||
return c
|
||||
}
|
||||
|
||||
// WithGroup returns a new Logger that starts a group. The keys of all
|
||||
// attributes added to the Logger will be qualified by the given name.
|
||||
// (How that qualification happens depends on the [Handler.WithGroup]
|
||||
// method of the Logger's Handler.)
|
||||
// The new Logger shares the old Logger's context.
|
||||
//
|
||||
// The new Logger's handler is the result of calling WithGroup on the receiver's
|
||||
// handler.
|
||||
func (l *Logger) WithGroup(name string) *Logger {
|
||||
c := l.clone()
|
||||
c.handler = l.handler.WithGroup(name)
|
||||
return c
|
||||
|
||||
}
|
||||
|
||||
// New creates a new Logger with the given non-nil Handler and a nil context.
|
||||
func New(h Handler) *Logger {
|
||||
if h == nil {
|
||||
panic("nil Handler")
|
||||
}
|
||||
return &Logger{handler: h}
|
||||
}
|
||||
|
||||
// With calls Logger.With on the default logger.
|
||||
func With(args ...any) *Logger {
|
||||
return Default().With(args...)
|
||||
}
|
||||
|
||||
// Enabled reports whether l emits log records at the given context and level.
|
||||
func (l *Logger) Enabled(ctx context.Context, level Level) bool {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
return l.Handler().Enabled(ctx, level)
|
||||
}
|
||||
|
||||
// NewLogLogger returns a new log.Logger such that each call to its Output method
|
||||
// dispatches a Record to the specified handler. The logger acts as a bridge from
|
||||
// the older log API to newer structured logging handlers.
|
||||
func NewLogLogger(h Handler, level Level) *log.Logger {
|
||||
return log.New(&handlerWriter{h, level, true}, "", 0)
|
||||
}
|
||||
|
||||
// Log emits a log record with the current time and the given level and message.
|
||||
// The Record's Attrs consist of the Logger's attributes followed by
|
||||
// the Attrs specified by args.
|
||||
//
|
||||
// The attribute arguments are processed as follows:
|
||||
// - If an argument is an Attr, it is used as is.
|
||||
// - If an argument is a string and this is not the last argument,
|
||||
// the following argument is treated as the value and the two are combined
|
||||
// into an Attr.
|
||||
// - Otherwise, the argument is treated as a value with key "!BADKEY".
|
||||
func (l *Logger) Log(ctx context.Context, level Level, msg string, args ...any) {
|
||||
l.log(ctx, level, msg, args...)
|
||||
}
|
||||
|
||||
// LogAttrs is a more efficient version of [Logger.Log] that accepts only Attrs.
|
||||
func (l *Logger) LogAttrs(ctx context.Context, level Level, msg string, attrs ...Attr) {
|
||||
l.logAttrs(ctx, level, msg, attrs...)
|
||||
}
|
||||
|
||||
// Debug logs at LevelDebug.
|
||||
func (l *Logger) Debug(msg string, args ...any) {
|
||||
l.log(nil, LevelDebug, msg, args...)
|
||||
}
|
||||
|
||||
// DebugContext logs at LevelDebug with the given context.
|
||||
func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any) {
|
||||
l.log(ctx, LevelDebug, msg, args...)
|
||||
}
|
||||
|
||||
// DebugCtx logs at LevelDebug with the given context.
|
||||
// Deprecated: Use Logger.DebugContext.
|
||||
func (l *Logger) DebugCtx(ctx context.Context, msg string, args ...any) {
|
||||
l.log(ctx, LevelDebug, msg, args...)
|
||||
}
|
||||
|
||||
// Info logs at LevelInfo.
|
||||
func (l *Logger) Info(msg string, args ...any) {
|
||||
l.log(nil, LevelInfo, msg, args...)
|
||||
}
|
||||
|
||||
// InfoContext logs at LevelInfo with the given context.
|
||||
func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any) {
|
||||
l.log(ctx, LevelInfo, msg, args...)
|
||||
}
|
||||
|
||||
// InfoCtx logs at LevelInfo with the given context.
|
||||
// Deprecated: Use Logger.InfoContext.
|
||||
func (l *Logger) InfoCtx(ctx context.Context, msg string, args ...any) {
|
||||
l.log(ctx, LevelInfo, msg, args...)
|
||||
}
|
||||
|
||||
// Warn logs at LevelWarn.
|
||||
func (l *Logger) Warn(msg string, args ...any) {
|
||||
l.log(nil, LevelWarn, msg, args...)
|
||||
}
|
||||
|
||||
// WarnContext logs at LevelWarn with the given context.
|
||||
func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any) {
|
||||
l.log(ctx, LevelWarn, msg, args...)
|
||||
}
|
||||
|
||||
// WarnCtx logs at LevelWarn with the given context.
|
||||
// Deprecated: Use Logger.WarnContext.
|
||||
func (l *Logger) WarnCtx(ctx context.Context, msg string, args ...any) {
|
||||
l.log(ctx, LevelWarn, msg, args...)
|
||||
}
|
||||
|
||||
// Error logs at LevelError.
|
||||
func (l *Logger) Error(msg string, args ...any) {
|
||||
l.log(nil, LevelError, msg, args...)
|
||||
}
|
||||
|
||||
// ErrorContext logs at LevelError with the given context.
|
||||
func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any) {
|
||||
l.log(ctx, LevelError, msg, args...)
|
||||
}
|
||||
|
||||
// ErrorCtx logs at LevelError with the given context.
|
||||
// Deprecated: Use Logger.ErrorContext.
|
||||
func (l *Logger) ErrorCtx(ctx context.Context, msg string, args ...any) {
|
||||
l.log(ctx, LevelError, msg, args...)
|
||||
}
|
||||
|
||||
// log is the low-level logging method for methods that take ...any.
|
||||
// It must always be called directly by an exported logging method
|
||||
// or function, because it uses a fixed call depth to obtain the pc.
|
||||
func (l *Logger) log(ctx context.Context, level Level, msg string, args ...any) {
|
||||
if !l.Enabled(ctx, level) {
|
||||
return
|
||||
}
|
||||
var pc uintptr
|
||||
if !internal.IgnorePC {
|
||||
var pcs [1]uintptr
|
||||
// skip [runtime.Callers, this function, this function's caller]
|
||||
runtime.Callers(3, pcs[:])
|
||||
pc = pcs[0]
|
||||
}
|
||||
r := NewRecord(time.Now(), level, msg, pc)
|
||||
r.Add(args...)
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
_ = l.Handler().Handle(ctx, r)
|
||||
}
|
||||
|
||||
// logAttrs is like [Logger.log], but for methods that take ...Attr.
|
||||
func (l *Logger) logAttrs(ctx context.Context, level Level, msg string, attrs ...Attr) {
|
||||
if !l.Enabled(ctx, level) {
|
||||
return
|
||||
}
|
||||
var pc uintptr
|
||||
if !internal.IgnorePC {
|
||||
var pcs [1]uintptr
|
||||
// skip [runtime.Callers, this function, this function's caller]
|
||||
runtime.Callers(3, pcs[:])
|
||||
pc = pcs[0]
|
||||
}
|
||||
r := NewRecord(time.Now(), level, msg, pc)
|
||||
r.AddAttrs(attrs...)
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
_ = l.Handler().Handle(ctx, r)
|
||||
}
|
||||
|
||||
// Debug calls Logger.Debug on the default logger.
|
||||
func Debug(msg string, args ...any) {
|
||||
Default().log(nil, LevelDebug, msg, args...)
|
||||
}
|
||||
|
||||
// DebugContext calls Logger.DebugContext on the default logger.
|
||||
func DebugContext(ctx context.Context, msg string, args ...any) {
|
||||
Default().log(ctx, LevelDebug, msg, args...)
|
||||
}
|
||||
|
||||
// Info calls Logger.Info on the default logger.
|
||||
func Info(msg string, args ...any) {
|
||||
Default().log(nil, LevelInfo, msg, args...)
|
||||
}
|
||||
|
||||
// InfoContext calls Logger.InfoContext on the default logger.
|
||||
func InfoContext(ctx context.Context, msg string, args ...any) {
|
||||
Default().log(ctx, LevelInfo, msg, args...)
|
||||
}
|
||||
|
||||
// Warn calls Logger.Warn on the default logger.
|
||||
func Warn(msg string, args ...any) {
|
||||
Default().log(nil, LevelWarn, msg, args...)
|
||||
}
|
||||
|
||||
// WarnContext calls Logger.WarnContext on the default logger.
|
||||
func WarnContext(ctx context.Context, msg string, args ...any) {
|
||||
Default().log(ctx, LevelWarn, msg, args...)
|
||||
}
|
||||
|
||||
// Error calls Logger.Error on the default logger.
|
||||
func Error(msg string, args ...any) {
|
||||
Default().log(nil, LevelError, msg, args...)
|
||||
}
|
||||
|
||||
// ErrorContext calls Logger.ErrorContext on the default logger.
|
||||
func ErrorContext(ctx context.Context, msg string, args ...any) {
|
||||
Default().log(ctx, LevelError, msg, args...)
|
||||
}
|
||||
|
||||
// DebugCtx calls Logger.DebugContext on the default logger.
|
||||
// Deprecated: call DebugContext.
|
||||
func DebugCtx(ctx context.Context, msg string, args ...any) {
|
||||
Default().log(ctx, LevelDebug, msg, args...)
|
||||
}
|
||||
|
||||
// InfoCtx calls Logger.InfoContext on the default logger.
|
||||
// Deprecated: call InfoContext.
|
||||
func InfoCtx(ctx context.Context, msg string, args ...any) {
|
||||
Default().log(ctx, LevelInfo, msg, args...)
|
||||
}
|
||||
|
||||
// WarnCtx calls Logger.WarnContext on the default logger.
|
||||
// Deprecated: call WarnContext.
|
||||
func WarnCtx(ctx context.Context, msg string, args ...any) {
|
||||
Default().log(ctx, LevelWarn, msg, args...)
|
||||
}
|
||||
|
||||
// ErrorCtx calls Logger.ErrorContext on the default logger.
|
||||
// Deprecated: call ErrorContext.
|
||||
func ErrorCtx(ctx context.Context, msg string, args ...any) {
|
||||
Default().log(ctx, LevelError, msg, args...)
|
||||
}
|
||||
|
||||
// Log calls Logger.Log on the default logger.
|
||||
func Log(ctx context.Context, level Level, msg string, args ...any) {
|
||||
Default().log(ctx, level, msg, args...)
|
||||
}
|
||||
|
||||
// LogAttrs calls Logger.LogAttrs on the default logger.
|
||||
func LogAttrs(ctx context.Context, level Level, msg string, attrs ...Attr) {
|
||||
Default().logAttrs(ctx, level, msg, attrs...)
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: golang.org/x/exp/slog
|
||||
cpu: Intel(R) Xeon(R) CPU @ 2.20GHz
|
||||
BenchmarkNopLog/attrs-8 1000000 1090 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/attrs-8 1000000 1097 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/attrs-8 1000000 1078 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/attrs-8 1000000 1095 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/attrs-8 1000000 1096 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/attrs-parallel-8 4007268 308.2 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/attrs-parallel-8 4016138 299.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/attrs-parallel-8 4020529 305.9 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/attrs-parallel-8 3977829 303.4 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/attrs-parallel-8 3225438 318.5 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/keys-values-8 1179256 994.2 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/keys-values-8 1000000 1002 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/keys-values-8 1216710 993.2 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/keys-values-8 1000000 1013 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/keys-values-8 1000000 1016 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/WithContext-8 989066 1163 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/WithContext-8 994116 1163 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/WithContext-8 1000000 1152 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/WithContext-8 991675 1165 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/WithContext-8 965268 1166 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/WithContext-parallel-8 3955503 303.3 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/WithContext-parallel-8 3861188 307.8 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/WithContext-parallel-8 3967752 303.9 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/WithContext-parallel-8 3955203 302.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/WithContext-parallel-8 3948278 301.1 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/Ctx-8 940622 1247 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/Ctx-8 936381 1257 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/Ctx-8 959730 1266 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/Ctx-8 943473 1290 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkNopLog/Ctx-8 919414 1259 ns/op 0 B/op 0 allocs/op
|
||||
PASS
|
||||
ok golang.org/x/exp/slog 40.566s
|
|
@ -1,207 +0,0 @@
|
|||
// Copyright 2022 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.
|
||||
|
||||
package slog
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
const nAttrsInline = 5
|
||||
|
||||
// A Record holds information about a log event.
|
||||
// Copies of a Record share state.
|
||||
// Do not modify a Record after handing out a copy to it.
|
||||
// Use [Record.Clone] to create a copy with no shared state.
|
||||
type Record struct {
|
||||
// The time at which the output method (Log, Info, etc.) was called.
|
||||
Time time.Time
|
||||
|
||||
// The log message.
|
||||
Message string
|
||||
|
||||
// The level of the event.
|
||||
Level Level
|
||||
|
||||
// The program counter at the time the record was constructed, as determined
|
||||
// by runtime.Callers. If zero, no program counter is available.
|
||||
//
|
||||
// The only valid use for this value is as an argument to
|
||||
// [runtime.CallersFrames]. In particular, it must not be passed to
|
||||
// [runtime.FuncForPC].
|
||||
PC uintptr
|
||||
|
||||
// Allocation optimization: an inline array sized to hold
|
||||
// the majority of log calls (based on examination of open-source
|
||||
// code). It holds the start of the list of Attrs.
|
||||
front [nAttrsInline]Attr
|
||||
|
||||
// The number of Attrs in front.
|
||||
nFront int
|
||||
|
||||
// The list of Attrs except for those in front.
|
||||
// Invariants:
|
||||
// - len(back) > 0 iff nFront == len(front)
|
||||
// - Unused array elements are zero. Used to detect mistakes.
|
||||
back []Attr
|
||||
}
|
||||
|
||||
// NewRecord creates a Record from the given arguments.
|
||||
// Use [Record.AddAttrs] to add attributes to the Record.
|
||||
//
|
||||
// NewRecord is intended for logging APIs that want to support a [Handler] as
|
||||
// a backend.
|
||||
func NewRecord(t time.Time, level Level, msg string, pc uintptr) Record {
|
||||
return Record{
|
||||
Time: t,
|
||||
Message: msg,
|
||||
Level: level,
|
||||
PC: pc,
|
||||
}
|
||||
}
|
||||
|
||||
// Clone returns a copy of the record with no shared state.
|
||||
// The original record and the clone can both be modified
|
||||
// without interfering with each other.
|
||||
func (r Record) Clone() Record {
|
||||
r.back = slices.Clip(r.back) // prevent append from mutating shared array
|
||||
return r
|
||||
}
|
||||
|
||||
// NumAttrs returns the number of attributes in the Record.
|
||||
func (r Record) NumAttrs() int {
|
||||
return r.nFront + len(r.back)
|
||||
}
|
||||
|
||||
// Attrs calls f on each Attr in the Record.
|
||||
// Iteration stops if f returns false.
|
||||
func (r Record) Attrs(f func(Attr) bool) {
|
||||
for i := 0; i < r.nFront; i++ {
|
||||
if !f(r.front[i]) {
|
||||
return
|
||||
}
|
||||
}
|
||||
for _, a := range r.back {
|
||||
if !f(a) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AddAttrs appends the given Attrs to the Record's list of Attrs.
|
||||
func (r *Record) AddAttrs(attrs ...Attr) {
|
||||
n := copy(r.front[r.nFront:], attrs)
|
||||
r.nFront += n
|
||||
// Check if a copy was modified by slicing past the end
|
||||
// and seeing if the Attr there is non-zero.
|
||||
if cap(r.back) > len(r.back) {
|
||||
end := r.back[:len(r.back)+1][len(r.back)]
|
||||
if !end.isEmpty() {
|
||||
panic("copies of a slog.Record were both modified")
|
||||
}
|
||||
}
|
||||
r.back = append(r.back, attrs[n:]...)
|
||||
}
|
||||
|
||||
// Add converts the args to Attrs as described in [Logger.Log],
|
||||
// then appends the Attrs to the Record's list of Attrs.
|
||||
func (r *Record) Add(args ...any) {
|
||||
var a Attr
|
||||
for len(args) > 0 {
|
||||
a, args = argsToAttr(args)
|
||||
if r.nFront < len(r.front) {
|
||||
r.front[r.nFront] = a
|
||||
r.nFront++
|
||||
} else {
|
||||
if r.back == nil {
|
||||
r.back = make([]Attr, 0, countAttrs(args))
|
||||
}
|
||||
r.back = append(r.back, a)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// countAttrs returns the number of Attrs that would be created from args.
|
||||
func countAttrs(args []any) int {
|
||||
n := 0
|
||||
for i := 0; i < len(args); i++ {
|
||||
n++
|
||||
if _, ok := args[i].(string); ok {
|
||||
i++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
const badKey = "!BADKEY"
|
||||
|
||||
// argsToAttr turns a prefix of the nonempty args slice into an Attr
|
||||
// and returns the unconsumed portion of the slice.
|
||||
// If args[0] is an Attr, it returns it.
|
||||
// If args[0] is a string, it treats the first two elements as
|
||||
// a key-value pair.
|
||||
// Otherwise, it treats args[0] as a value with a missing key.
|
||||
func argsToAttr(args []any) (Attr, []any) {
|
||||
switch x := args[0].(type) {
|
||||
case string:
|
||||
if len(args) == 1 {
|
||||
return String(badKey, x), nil
|
||||
}
|
||||
return Any(x, args[1]), args[2:]
|
||||
|
||||
case Attr:
|
||||
return x, args[1:]
|
||||
|
||||
default:
|
||||
return Any(badKey, x), args[1:]
|
||||
}
|
||||
}
|
||||
|
||||
// Source describes the location of a line of source code.
|
||||
type Source struct {
|
||||
// Function is the package path-qualified function name containing the
|
||||
// source line. If non-empty, this string uniquely identifies a single
|
||||
// function in the program. This may be the empty string if not known.
|
||||
Function string `json:"function"`
|
||||
// File and Line are the file name and line number (1-based) of the source
|
||||
// line. These may be the empty string and zero, respectively, if not known.
|
||||
File string `json:"file"`
|
||||
Line int `json:"line"`
|
||||
}
|
||||
|
||||
// attrs returns the non-zero fields of s as a slice of attrs.
|
||||
// It is similar to a LogValue method, but we don't want Source
|
||||
// to implement LogValuer because it would be resolved before
|
||||
// the ReplaceAttr function was called.
|
||||
func (s *Source) group() Value {
|
||||
var as []Attr
|
||||
if s.Function != "" {
|
||||
as = append(as, String("function", s.Function))
|
||||
}
|
||||
if s.File != "" {
|
||||
as = append(as, String("file", s.File))
|
||||
}
|
||||
if s.Line != 0 {
|
||||
as = append(as, Int("line", s.Line))
|
||||
}
|
||||
return GroupValue(as...)
|
||||
}
|
||||
|
||||
// source returns a Source for the log event.
|
||||
// If the Record was created without the necessary information,
|
||||
// or if the location is unavailable, it returns a non-nil *Source
|
||||
// with zero fields.
|
||||
func (r Record) source() *Source {
|
||||
fs := runtime.CallersFrames([]uintptr{r.PC})
|
||||
f, _ := fs.Next()
|
||||
return &Source{
|
||||
Function: f.Function,
|
||||
File: f.File,
|
||||
Line: f.Line,
|
||||
}
|
||||
}
|
|
@ -1,161 +0,0 @@
|
|||
// Copyright 2022 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.
|
||||
|
||||
package slog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// TextHandler is a Handler that writes Records to an io.Writer as a
|
||||
// sequence of key=value pairs separated by spaces and followed by a newline.
|
||||
type TextHandler struct {
|
||||
*commonHandler
|
||||
}
|
||||
|
||||
// NewTextHandler creates a TextHandler that writes to w,
|
||||
// using the given options.
|
||||
// If opts is nil, the default options are used.
|
||||
func NewTextHandler(w io.Writer, opts *HandlerOptions) *TextHandler {
|
||||
if opts == nil {
|
||||
opts = &HandlerOptions{}
|
||||
}
|
||||
return &TextHandler{
|
||||
&commonHandler{
|
||||
json: false,
|
||||
w: w,
|
||||
opts: *opts,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Enabled reports whether the handler handles records at the given level.
|
||||
// The handler ignores records whose level is lower.
|
||||
func (h *TextHandler) Enabled(_ context.Context, level Level) bool {
|
||||
return h.commonHandler.enabled(level)
|
||||
}
|
||||
|
||||
// WithAttrs returns a new TextHandler whose attributes consists
|
||||
// of h's attributes followed by attrs.
|
||||
func (h *TextHandler) WithAttrs(attrs []Attr) Handler {
|
||||
return &TextHandler{commonHandler: h.commonHandler.withAttrs(attrs)}
|
||||
}
|
||||
|
||||
func (h *TextHandler) WithGroup(name string) Handler {
|
||||
return &TextHandler{commonHandler: h.commonHandler.withGroup(name)}
|
||||
}
|
||||
|
||||
// Handle formats its argument Record as a single line of space-separated
|
||||
// key=value items.
|
||||
//
|
||||
// If the Record's time is zero, the time is omitted.
|
||||
// Otherwise, the key is "time"
|
||||
// and the value is output in RFC3339 format with millisecond precision.
|
||||
//
|
||||
// If the Record's level is zero, the level is omitted.
|
||||
// Otherwise, the key is "level"
|
||||
// and the value of [Level.String] is output.
|
||||
//
|
||||
// If the AddSource option is set and source information is available,
|
||||
// the key is "source" and the value is output as FILE:LINE.
|
||||
//
|
||||
// The message's key is "msg".
|
||||
//
|
||||
// To modify these or other attributes, or remove them from the output, use
|
||||
// [HandlerOptions.ReplaceAttr].
|
||||
//
|
||||
// If a value implements [encoding.TextMarshaler], the result of MarshalText is
|
||||
// written. Otherwise, the result of fmt.Sprint is written.
|
||||
//
|
||||
// Keys and values are quoted with [strconv.Quote] if they contain Unicode space
|
||||
// characters, non-printing characters, '"' or '='.
|
||||
//
|
||||
// Keys inside groups consist of components (keys or group names) separated by
|
||||
// dots. No further escaping is performed.
|
||||
// Thus there is no way to determine from the key "a.b.c" whether there
|
||||
// are two groups "a" and "b" and a key "c", or a single group "a.b" and a key "c",
|
||||
// or single group "a" and a key "b.c".
|
||||
// If it is necessary to reconstruct the group structure of a key
|
||||
// even in the presence of dots inside components, use
|
||||
// [HandlerOptions.ReplaceAttr] to encode that information in the key.
|
||||
//
|
||||
// Each call to Handle results in a single serialized call to
|
||||
// io.Writer.Write.
|
||||
func (h *TextHandler) Handle(_ context.Context, r Record) error {
|
||||
return h.commonHandler.handle(r)
|
||||
}
|
||||
|
||||
func appendTextValue(s *handleState, v Value) error {
|
||||
switch v.Kind() {
|
||||
case KindString:
|
||||
s.appendString(v.str())
|
||||
case KindTime:
|
||||
s.appendTime(v.time())
|
||||
case KindAny:
|
||||
if tm, ok := v.any.(encoding.TextMarshaler); ok {
|
||||
data, err := tm.MarshalText()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: avoid the conversion to string.
|
||||
s.appendString(string(data))
|
||||
return nil
|
||||
}
|
||||
if bs, ok := byteSlice(v.any); ok {
|
||||
// As of Go 1.19, this only allocates for strings longer than 32 bytes.
|
||||
s.buf.WriteString(strconv.Quote(string(bs)))
|
||||
return nil
|
||||
}
|
||||
s.appendString(fmt.Sprintf("%+v", v.Any()))
|
||||
default:
|
||||
*s.buf = v.append(*s.buf)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// byteSlice returns its argument as a []byte if the argument's
|
||||
// underlying type is []byte, along with a second return value of true.
|
||||
// Otherwise it returns nil, false.
|
||||
func byteSlice(a any) ([]byte, bool) {
|
||||
if bs, ok := a.([]byte); ok {
|
||||
return bs, true
|
||||
}
|
||||
// Like Printf's %s, we allow both the slice type and the byte element type to be named.
|
||||
t := reflect.TypeOf(a)
|
||||
if t != nil && t.Kind() == reflect.Slice && t.Elem().Kind() == reflect.Uint8 {
|
||||
return reflect.ValueOf(a).Bytes(), true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func needsQuoting(s string) bool {
|
||||
if len(s) == 0 {
|
||||
return true
|
||||
}
|
||||
for i := 0; i < len(s); {
|
||||
b := s[i]
|
||||
if b < utf8.RuneSelf {
|
||||
// Quote anything except a backslash that would need quoting in a
|
||||
// JSON string, as well as space and '='
|
||||
if b != '\\' && (b == ' ' || b == '=' || !safeSet[b]) {
|
||||
return true
|
||||
}
|
||||
i++
|
||||
continue
|
||||
}
|
||||
r, size := utf8.DecodeRuneInString(s[i:])
|
||||
if r == utf8.RuneError || unicode.IsSpace(r) || !unicode.IsPrint(r) {
|
||||
return true
|
||||
}
|
||||
i += size
|
||||
}
|
||||
return false
|
||||
}
|
|
@ -1,456 +0,0 @@
|
|||
// Copyright 2022 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.
|
||||
|
||||
package slog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
// A Value can represent any Go value, but unlike type any,
|
||||
// it can represent most small values without an allocation.
|
||||
// The zero Value corresponds to nil.
|
||||
type Value struct {
|
||||
_ [0]func() // disallow ==
|
||||
// num holds the value for Kinds Int64, Uint64, Float64, Bool and Duration,
|
||||
// the string length for KindString, and nanoseconds since the epoch for KindTime.
|
||||
num uint64
|
||||
// If any is of type Kind, then the value is in num as described above.
|
||||
// If any is of type *time.Location, then the Kind is Time and time.Time value
|
||||
// can be constructed from the Unix nanos in num and the location (monotonic time
|
||||
// is not preserved).
|
||||
// If any is of type stringptr, then the Kind is String and the string value
|
||||
// consists of the length in num and the pointer in any.
|
||||
// Otherwise, the Kind is Any and any is the value.
|
||||
// (This implies that Attrs cannot store values of type Kind, *time.Location
|
||||
// or stringptr.)
|
||||
any any
|
||||
}
|
||||
|
||||
// Kind is the kind of a Value.
|
||||
type Kind int
|
||||
|
||||
// The following list is sorted alphabetically, but it's also important that
|
||||
// KindAny is 0 so that a zero Value represents nil.
|
||||
|
||||
const (
|
||||
KindAny Kind = iota
|
||||
KindBool
|
||||
KindDuration
|
||||
KindFloat64
|
||||
KindInt64
|
||||
KindString
|
||||
KindTime
|
||||
KindUint64
|
||||
KindGroup
|
||||
KindLogValuer
|
||||
)
|
||||
|
||||
var kindStrings = []string{
|
||||
"Any",
|
||||
"Bool",
|
||||
"Duration",
|
||||
"Float64",
|
||||
"Int64",
|
||||
"String",
|
||||
"Time",
|
||||
"Uint64",
|
||||
"Group",
|
||||
"LogValuer",
|
||||
}
|
||||
|
||||
func (k Kind) String() string {
|
||||
if k >= 0 && int(k) < len(kindStrings) {
|
||||
return kindStrings[k]
|
||||
}
|
||||
return "<unknown slog.Kind>"
|
||||
}
|
||||
|
||||
// Unexported version of Kind, just so we can store Kinds in Values.
|
||||
// (No user-provided value has this type.)
|
||||
type kind Kind
|
||||
|
||||
// Kind returns v's Kind.
|
||||
func (v Value) Kind() Kind {
|
||||
switch x := v.any.(type) {
|
||||
case Kind:
|
||||
return x
|
||||
case stringptr:
|
||||
return KindString
|
||||
case timeLocation:
|
||||
return KindTime
|
||||
case groupptr:
|
||||
return KindGroup
|
||||
case LogValuer:
|
||||
return KindLogValuer
|
||||
case kind: // a kind is just a wrapper for a Kind
|
||||
return KindAny
|
||||
default:
|
||||
return KindAny
|
||||
}
|
||||
}
|
||||
|
||||
//////////////// Constructors
|
||||
|
||||
// IntValue returns a Value for an int.
|
||||
func IntValue(v int) Value {
|
||||
return Int64Value(int64(v))
|
||||
}
|
||||
|
||||
// Int64Value returns a Value for an int64.
|
||||
func Int64Value(v int64) Value {
|
||||
return Value{num: uint64(v), any: KindInt64}
|
||||
}
|
||||
|
||||
// Uint64Value returns a Value for a uint64.
|
||||
func Uint64Value(v uint64) Value {
|
||||
return Value{num: v, any: KindUint64}
|
||||
}
|
||||
|
||||
// Float64Value returns a Value for a floating-point number.
|
||||
func Float64Value(v float64) Value {
|
||||
return Value{num: math.Float64bits(v), any: KindFloat64}
|
||||
}
|
||||
|
||||
// BoolValue returns a Value for a bool.
|
||||
func BoolValue(v bool) Value {
|
||||
u := uint64(0)
|
||||
if v {
|
||||
u = 1
|
||||
}
|
||||
return Value{num: u, any: KindBool}
|
||||
}
|
||||
|
||||
// Unexported version of *time.Location, just so we can store *time.Locations in
|
||||
// Values. (No user-provided value has this type.)
|
||||
type timeLocation *time.Location
|
||||
|
||||
// TimeValue returns a Value for a time.Time.
|
||||
// It discards the monotonic portion.
|
||||
func TimeValue(v time.Time) Value {
|
||||
if v.IsZero() {
|
||||
// UnixNano on the zero time is undefined, so represent the zero time
|
||||
// with a nil *time.Location instead. time.Time.Location method never
|
||||
// returns nil, so a Value with any == timeLocation(nil) cannot be
|
||||
// mistaken for any other Value, time.Time or otherwise.
|
||||
return Value{any: timeLocation(nil)}
|
||||
}
|
||||
return Value{num: uint64(v.UnixNano()), any: timeLocation(v.Location())}
|
||||
}
|
||||
|
||||
// DurationValue returns a Value for a time.Duration.
|
||||
func DurationValue(v time.Duration) Value {
|
||||
return Value{num: uint64(v.Nanoseconds()), any: KindDuration}
|
||||
}
|
||||
|
||||
// AnyValue returns a Value for the supplied value.
|
||||
//
|
||||
// If the supplied value is of type Value, it is returned
|
||||
// unmodified.
|
||||
//
|
||||
// Given a value of one of Go's predeclared string, bool, or
|
||||
// (non-complex) numeric types, AnyValue returns a Value of kind
|
||||
// String, Bool, Uint64, Int64, or Float64. The width of the
|
||||
// original numeric type is not preserved.
|
||||
//
|
||||
// Given a time.Time or time.Duration value, AnyValue returns a Value of kind
|
||||
// KindTime or KindDuration. The monotonic time is not preserved.
|
||||
//
|
||||
// For nil, or values of all other types, including named types whose
|
||||
// underlying type is numeric, AnyValue returns a value of kind KindAny.
|
||||
func AnyValue(v any) Value {
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
return StringValue(v)
|
||||
case int:
|
||||
return Int64Value(int64(v))
|
||||
case uint:
|
||||
return Uint64Value(uint64(v))
|
||||
case int64:
|
||||
return Int64Value(v)
|
||||
case uint64:
|
||||
return Uint64Value(v)
|
||||
case bool:
|
||||
return BoolValue(v)
|
||||
case time.Duration:
|
||||
return DurationValue(v)
|
||||
case time.Time:
|
||||
return TimeValue(v)
|
||||
case uint8:
|
||||
return Uint64Value(uint64(v))
|
||||
case uint16:
|
||||
return Uint64Value(uint64(v))
|
||||
case uint32:
|
||||
return Uint64Value(uint64(v))
|
||||
case uintptr:
|
||||
return Uint64Value(uint64(v))
|
||||
case int8:
|
||||
return Int64Value(int64(v))
|
||||
case int16:
|
||||
return Int64Value(int64(v))
|
||||
case int32:
|
||||
return Int64Value(int64(v))
|
||||
case float64:
|
||||
return Float64Value(v)
|
||||
case float32:
|
||||
return Float64Value(float64(v))
|
||||
case []Attr:
|
||||
return GroupValue(v...)
|
||||
case Kind:
|
||||
return Value{any: kind(v)}
|
||||
case Value:
|
||||
return v
|
||||
default:
|
||||
return Value{any: v}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////// Accessors
|
||||
|
||||
// Any returns v's value as an any.
|
||||
func (v Value) Any() any {
|
||||
switch v.Kind() {
|
||||
case KindAny:
|
||||
if k, ok := v.any.(kind); ok {
|
||||
return Kind(k)
|
||||
}
|
||||
return v.any
|
||||
case KindLogValuer:
|
||||
return v.any
|
||||
case KindGroup:
|
||||
return v.group()
|
||||
case KindInt64:
|
||||
return int64(v.num)
|
||||
case KindUint64:
|
||||
return v.num
|
||||
case KindFloat64:
|
||||
return v.float()
|
||||
case KindString:
|
||||
return v.str()
|
||||
case KindBool:
|
||||
return v.bool()
|
||||
case KindDuration:
|
||||
return v.duration()
|
||||
case KindTime:
|
||||
return v.time()
|
||||
default:
|
||||
panic(fmt.Sprintf("bad kind: %s", v.Kind()))
|
||||
}
|
||||
}
|
||||
|
||||
// Int64 returns v's value as an int64. It panics
|
||||
// if v is not a signed integer.
|
||||
func (v Value) Int64() int64 {
|
||||
if g, w := v.Kind(), KindInt64; g != w {
|
||||
panic(fmt.Sprintf("Value kind is %s, not %s", g, w))
|
||||
}
|
||||
return int64(v.num)
|
||||
}
|
||||
|
||||
// Uint64 returns v's value as a uint64. It panics
|
||||
// if v is not an unsigned integer.
|
||||
func (v Value) Uint64() uint64 {
|
||||
if g, w := v.Kind(), KindUint64; g != w {
|
||||
panic(fmt.Sprintf("Value kind is %s, not %s", g, w))
|
||||
}
|
||||
return v.num
|
||||
}
|
||||
|
||||
// Bool returns v's value as a bool. It panics
|
||||
// if v is not a bool.
|
||||
func (v Value) Bool() bool {
|
||||
if g, w := v.Kind(), KindBool; g != w {
|
||||
panic(fmt.Sprintf("Value kind is %s, not %s", g, w))
|
||||
}
|
||||
return v.bool()
|
||||
}
|
||||
|
||||
func (a Value) bool() bool {
|
||||
return a.num == 1
|
||||
}
|
||||
|
||||
// Duration returns v's value as a time.Duration. It panics
|
||||
// if v is not a time.Duration.
|
||||
func (a Value) Duration() time.Duration {
|
||||
if g, w := a.Kind(), KindDuration; g != w {
|
||||
panic(fmt.Sprintf("Value kind is %s, not %s", g, w))
|
||||
}
|
||||
|
||||
return a.duration()
|
||||
}
|
||||
|
||||
func (a Value) duration() time.Duration {
|
||||
return time.Duration(int64(a.num))
|
||||
}
|
||||
|
||||
// Float64 returns v's value as a float64. It panics
|
||||
// if v is not a float64.
|
||||
func (v Value) Float64() float64 {
|
||||
if g, w := v.Kind(), KindFloat64; g != w {
|
||||
panic(fmt.Sprintf("Value kind is %s, not %s", g, w))
|
||||
}
|
||||
|
||||
return v.float()
|
||||
}
|
||||
|
||||
func (a Value) float() float64 {
|
||||
return math.Float64frombits(a.num)
|
||||
}
|
||||
|
||||
// Time returns v's value as a time.Time. It panics
|
||||
// if v is not a time.Time.
|
||||
func (v Value) Time() time.Time {
|
||||
if g, w := v.Kind(), KindTime; g != w {
|
||||
panic(fmt.Sprintf("Value kind is %s, not %s", g, w))
|
||||
}
|
||||
return v.time()
|
||||
}
|
||||
|
||||
func (v Value) time() time.Time {
|
||||
loc := v.any.(timeLocation)
|
||||
if loc == nil {
|
||||
return time.Time{}
|
||||
}
|
||||
return time.Unix(0, int64(v.num)).In(loc)
|
||||
}
|
||||
|
||||
// LogValuer returns v's value as a LogValuer. It panics
|
||||
// if v is not a LogValuer.
|
||||
func (v Value) LogValuer() LogValuer {
|
||||
return v.any.(LogValuer)
|
||||
}
|
||||
|
||||
// Group returns v's value as a []Attr.
|
||||
// It panics if v's Kind is not KindGroup.
|
||||
func (v Value) Group() []Attr {
|
||||
if sp, ok := v.any.(groupptr); ok {
|
||||
return unsafe.Slice((*Attr)(sp), v.num)
|
||||
}
|
||||
panic("Group: bad kind")
|
||||
}
|
||||
|
||||
func (v Value) group() []Attr {
|
||||
return unsafe.Slice((*Attr)(v.any.(groupptr)), v.num)
|
||||
}
|
||||
|
||||
//////////////// Other
|
||||
|
||||
// Equal reports whether v and w represent the same Go value.
|
||||
func (v Value) Equal(w Value) bool {
|
||||
k1 := v.Kind()
|
||||
k2 := w.Kind()
|
||||
if k1 != k2 {
|
||||
return false
|
||||
}
|
||||
switch k1 {
|
||||
case KindInt64, KindUint64, KindBool, KindDuration:
|
||||
return v.num == w.num
|
||||
case KindString:
|
||||
return v.str() == w.str()
|
||||
case KindFloat64:
|
||||
return v.float() == w.float()
|
||||
case KindTime:
|
||||
return v.time().Equal(w.time())
|
||||
case KindAny, KindLogValuer:
|
||||
return v.any == w.any // may panic if non-comparable
|
||||
case KindGroup:
|
||||
return slices.EqualFunc(v.group(), w.group(), Attr.Equal)
|
||||
default:
|
||||
panic(fmt.Sprintf("bad kind: %s", k1))
|
||||
}
|
||||
}
|
||||
|
||||
// append appends a text representation of v to dst.
|
||||
// v is formatted as with fmt.Sprint.
|
||||
func (v Value) append(dst []byte) []byte {
|
||||
switch v.Kind() {
|
||||
case KindString:
|
||||
return append(dst, v.str()...)
|
||||
case KindInt64:
|
||||
return strconv.AppendInt(dst, int64(v.num), 10)
|
||||
case KindUint64:
|
||||
return strconv.AppendUint(dst, v.num, 10)
|
||||
case KindFloat64:
|
||||
return strconv.AppendFloat(dst, v.float(), 'g', -1, 64)
|
||||
case KindBool:
|
||||
return strconv.AppendBool(dst, v.bool())
|
||||
case KindDuration:
|
||||
return append(dst, v.duration().String()...)
|
||||
case KindTime:
|
||||
return append(dst, v.time().String()...)
|
||||
case KindGroup:
|
||||
return fmt.Append(dst, v.group())
|
||||
case KindAny, KindLogValuer:
|
||||
return fmt.Append(dst, v.any)
|
||||
default:
|
||||
panic(fmt.Sprintf("bad kind: %s", v.Kind()))
|
||||
}
|
||||
}
|
||||
|
||||
// A LogValuer is any Go value that can convert itself into a Value for logging.
|
||||
//
|
||||
// This mechanism may be used to defer expensive operations until they are
|
||||
// needed, or to expand a single value into a sequence of components.
|
||||
type LogValuer interface {
|
||||
LogValue() Value
|
||||
}
|
||||
|
||||
const maxLogValues = 100
|
||||
|
||||
// Resolve repeatedly calls LogValue on v while it implements LogValuer,
|
||||
// and returns the result.
|
||||
// If v resolves to a group, the group's attributes' values are not recursively
|
||||
// resolved.
|
||||
// If the number of LogValue calls exceeds a threshold, a Value containing an
|
||||
// error is returned.
|
||||
// Resolve's return value is guaranteed not to be of Kind KindLogValuer.
|
||||
func (v Value) Resolve() (rv Value) {
|
||||
orig := v
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
rv = AnyValue(fmt.Errorf("LogValue panicked\n%s", stack(3, 5)))
|
||||
}
|
||||
}()
|
||||
|
||||
for i := 0; i < maxLogValues; i++ {
|
||||
if v.Kind() != KindLogValuer {
|
||||
return v
|
||||
}
|
||||
v = v.LogValuer().LogValue()
|
||||
}
|
||||
err := fmt.Errorf("LogValue called too many times on Value of type %T", orig.Any())
|
||||
return AnyValue(err)
|
||||
}
|
||||
|
||||
func stack(skip, nFrames int) string {
|
||||
pcs := make([]uintptr, nFrames+1)
|
||||
n := runtime.Callers(skip+1, pcs)
|
||||
if n == 0 {
|
||||
return "(no stack)"
|
||||
}
|
||||
frames := runtime.CallersFrames(pcs[:n])
|
||||
var b strings.Builder
|
||||
i := 0
|
||||
for {
|
||||
frame, more := frames.Next()
|
||||
fmt.Fprintf(&b, "called from %s (%s:%d)\n", frame.Function, frame.File, frame.Line)
|
||||
if !more {
|
||||
break
|
||||
}
|
||||
i++
|
||||
if i >= nFrames {
|
||||
fmt.Fprintf(&b, "(rest of stack elided)\n")
|
||||
break
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
// Copyright 2022 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.
|
||||
|
||||
//go:build go1.19 && !go1.20
|
||||
|
||||
package slog
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type (
|
||||
stringptr unsafe.Pointer // used in Value.any when the Value is a string
|
||||
groupptr unsafe.Pointer // used in Value.any when the Value is a []Attr
|
||||
)
|
||||
|
||||
// StringValue returns a new Value for a string.
|
||||
func StringValue(value string) Value {
|
||||
hdr := (*reflect.StringHeader)(unsafe.Pointer(&value))
|
||||
return Value{num: uint64(hdr.Len), any: stringptr(hdr.Data)}
|
||||
}
|
||||
|
||||
func (v Value) str() string {
|
||||
var s string
|
||||
hdr := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
||||
hdr.Data = uintptr(v.any.(stringptr))
|
||||
hdr.Len = int(v.num)
|
||||
return s
|
||||
}
|
||||
|
||||
// String returns Value's value as a string, formatted like fmt.Sprint. Unlike
|
||||
// the methods Int64, Float64, and so on, which panic if v is of the
|
||||
// wrong kind, String never panics.
|
||||
func (v Value) String() string {
|
||||
if sp, ok := v.any.(stringptr); ok {
|
||||
// Inlining this code makes a huge difference.
|
||||
var s string
|
||||
hdr := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
||||
hdr.Data = uintptr(sp)
|
||||
hdr.Len = int(v.num)
|
||||
return s
|
||||
}
|
||||
return string(v.append(nil))
|
||||
}
|
||||
|
||||
// GroupValue returns a new Value for a list of Attrs.
|
||||
// The caller must not subsequently mutate the argument slice.
|
||||
func GroupValue(as ...Attr) Value {
|
||||
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&as))
|
||||
return Value{num: uint64(hdr.Len), any: groupptr(hdr.Data)}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
// Copyright 2022 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.
|
||||
|
||||
//go:build go1.20
|
||||
|
||||
package slog
|
||||
|
||||
import "unsafe"
|
||||
|
||||
type (
|
||||
stringptr *byte // used in Value.any when the Value is a string
|
||||
groupptr *Attr // used in Value.any when the Value is a []Attr
|
||||
)
|
||||
|
||||
// StringValue returns a new Value for a string.
|
||||
func StringValue(value string) Value {
|
||||
return Value{num: uint64(len(value)), any: stringptr(unsafe.StringData(value))}
|
||||
}
|
||||
|
||||
// GroupValue returns a new Value for a list of Attrs.
|
||||
// The caller must not subsequently mutate the argument slice.
|
||||
func GroupValue(as ...Attr) Value {
|
||||
return Value{num: uint64(len(as)), any: groupptr(unsafe.SliceData(as))}
|
||||
}
|
||||
|
||||
// String returns Value's value as a string, formatted like fmt.Sprint. Unlike
|
||||
// the methods Int64, Float64, and so on, which panic if v is of the
|
||||
// wrong kind, String never panics.
|
||||
func (v Value) String() string {
|
||||
if sp, ok := v.any.(stringptr); ok {
|
||||
return unsafe.String(sp, v.num)
|
||||
}
|
||||
return string(v.append(nil))
|
||||
}
|
||||
|
||||
func (v Value) str() string {
|
||||
return unsafe.String(v.any.(stringptr), v.num)
|
||||
}
|
|
@ -38,7 +38,7 @@ var X86 struct {
|
|||
HasAVX512F bool // Advanced vector extension 512 Foundation Instructions
|
||||
HasAVX512CD bool // Advanced vector extension 512 Conflict Detection Instructions
|
||||
HasAVX512ER bool // Advanced vector extension 512 Exponential and Reciprocal Instructions
|
||||
HasAVX512PF bool // Advanced vector extension 512 Prefetch Instructions Instructions
|
||||
HasAVX512PF bool // Advanced vector extension 512 Prefetch Instructions
|
||||
HasAVX512VL bool // Advanced vector extension 512 Vector Length Extensions
|
||||
HasAVX512BW bool // Advanced vector extension 512 Byte and Word Instructions
|
||||
HasAVX512DQ bool // Advanced vector extension 512 Doubleword and Quadword Instructions
|
||||
|
@ -54,6 +54,9 @@ var X86 struct {
|
|||
HasAVX512VBMI2 bool // Advanced vector extension 512 Vector Byte Manipulation Instructions 2
|
||||
HasAVX512BITALG bool // Advanced vector extension 512 Bit Algorithms
|
||||
HasAVX512BF16 bool // Advanced vector extension 512 BFloat16 Instructions
|
||||
HasAMXTile bool // Advanced Matrix Extension Tile instructions
|
||||
HasAMXInt8 bool // Advanced Matrix Extension Int8 instructions
|
||||
HasAMXBF16 bool // Advanced Matrix Extension BFloat16 instructions
|
||||
HasBMI1 bool // Bit manipulation instruction set 1
|
||||
HasBMI2 bool // Bit manipulation instruction set 2
|
||||
HasCX16 bool // Compare and exchange 16 Bytes
|
||||
|
|
|
@ -7,6 +7,6 @@
|
|||
|
||||
package cpu
|
||||
|
||||
const cacheLineSize = 32
|
||||
const cacheLineSize = 64
|
||||
|
||||
func initOptions() {}
|
||||
|
|
|
@ -37,6 +37,9 @@ func initOptions() {
|
|||
{Name: "avx512vbmi2", Feature: &X86.HasAVX512VBMI2},
|
||||
{Name: "avx512bitalg", Feature: &X86.HasAVX512BITALG},
|
||||
{Name: "avx512bf16", Feature: &X86.HasAVX512BF16},
|
||||
{Name: "amxtile", Feature: &X86.HasAMXTile},
|
||||
{Name: "amxint8", Feature: &X86.HasAMXInt8},
|
||||
{Name: "amxbf16", Feature: &X86.HasAMXBF16},
|
||||
{Name: "bmi1", Feature: &X86.HasBMI1},
|
||||
{Name: "bmi2", Feature: &X86.HasBMI2},
|
||||
{Name: "cx16", Feature: &X86.HasCX16},
|
||||
|
@ -138,6 +141,10 @@ func archInit() {
|
|||
eax71, _, _, _ := cpuid(7, 1)
|
||||
X86.HasAVX512BF16 = isSet(5, eax71)
|
||||
}
|
||||
|
||||
X86.HasAMXTile = isSet(24, edx7)
|
||||
X86.HasAMXInt8 = isSet(25, edx7)
|
||||
X86.HasAMXBF16 = isSet(22, edx7)
|
||||
}
|
||||
|
||||
func isSet(bitpos uint, value uint32) bool {
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
package cpu
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -39,7 +39,7 @@ func readHWCAP() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
buf, err := ioutil.ReadFile(procAuxv)
|
||||
buf, err := os.ReadFile(procAuxv)
|
||||
if err != nil {
|
||||
// e.g. on android /proc/self/auxv is not accessible, so silently
|
||||
// ignore the error and leave Initialized = false. On some
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
// Copyright 2020 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.
|
||||
|
||||
// Package unsafeheader contains header declarations for the Go runtime's
|
||||
// slice and string implementations.
|
||||
//
|
||||
// This package allows x/sys to use types equivalent to
|
||||
// reflect.SliceHeader and reflect.StringHeader without introducing
|
||||
// a dependency on the (relatively heavy) "reflect" package.
|
||||
package unsafeheader
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Slice is the runtime representation of a slice.
|
||||
// It cannot be used safely or portably and its representation may change in a later release.
|
||||
type Slice struct {
|
||||
Data unsafe.Pointer
|
||||
Len int
|
||||
Cap int
|
||||
}
|
||||
|
||||
// String is the runtime representation of a string.
|
||||
// It cannot be used safely or portably and its representation may change in a later release.
|
||||
type String struct {
|
||||
Data unsafe.Pointer
|
||||
Len int
|
||||
}
|
|
@ -583,6 +583,7 @@ ccflags="$@"
|
|||
$2 ~ /^PERF_/ ||
|
||||
$2 ~ /^SECCOMP_MODE_/ ||
|
||||
$2 ~ /^SEEK_/ ||
|
||||
$2 ~ /^SCHED_/ ||
|
||||
$2 ~ /^SPLICE_/ ||
|
||||
$2 ~ /^SYNC_FILE_RANGE_/ ||
|
||||
$2 !~ /IOC_MAGIC/ &&
|
||||
|
|
|
@ -7,12 +7,6 @@
|
|||
|
||||
package unix
|
||||
|
||||
import "unsafe"
|
||||
|
||||
func ptrace(request int, pid int, addr uintptr, data uintptr) error {
|
||||
return ptrace1(request, pid, addr, data)
|
||||
}
|
||||
|
||||
func ptracePtr(request int, pid int, addr uintptr, data unsafe.Pointer) error {
|
||||
return ptrace1Ptr(request, pid, addr, data)
|
||||
}
|
||||
|
|
|
@ -7,12 +7,6 @@
|
|||
|
||||
package unix
|
||||
|
||||
import "unsafe"
|
||||
|
||||
func ptrace(request int, pid int, addr uintptr, data uintptr) (err error) {
|
||||
return ENOTSUP
|
||||
}
|
||||
|
||||
func ptracePtr(request int, pid int, addr uintptr, data unsafe.Pointer) (err error) {
|
||||
return ENOTSUP
|
||||
}
|
||||
|
|
|
@ -487,8 +487,6 @@ func Fsync(fd int) error {
|
|||
//sys Unlinkat(dirfd int, path string, flags int) (err error)
|
||||
//sys Ustat(dev int, ubuf *Ustat_t) (err error)
|
||||
//sys write(fd int, p []byte) (n int, err error)
|
||||
//sys readlen(fd int, p *byte, np int) (n int, err error) = read
|
||||
//sys writelen(fd int, p *byte, np int) (n int, err error) = write
|
||||
|
||||
//sys Dup2(oldfd int, newfd int) (err error)
|
||||
//sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = posix_fadvise64
|
||||
|
|
|
@ -644,189 +644,3 @@ func SysctlKinfoProcSlice(name string, args ...int) ([]KinfoProc, error) {
|
|||
//sys write(fd int, p []byte) (n int, err error)
|
||||
//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
|
||||
//sys munmap(addr uintptr, length uintptr) (err error)
|
||||
//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
|
||||
//sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
|
||||
|
||||
/*
|
||||
* Unimplemented
|
||||
*/
|
||||
// Profil
|
||||
// Sigaction
|
||||
// Sigprocmask
|
||||
// Getlogin
|
||||
// Sigpending
|
||||
// Sigaltstack
|
||||
// Ioctl
|
||||
// Reboot
|
||||
// Execve
|
||||
// Vfork
|
||||
// Sbrk
|
||||
// Sstk
|
||||
// Ovadvise
|
||||
// Mincore
|
||||
// Setitimer
|
||||
// Swapon
|
||||
// Select
|
||||
// Sigsuspend
|
||||
// Readv
|
||||
// Writev
|
||||
// Nfssvc
|
||||
// Getfh
|
||||
// Quotactl
|
||||
// Csops
|
||||
// Waitid
|
||||
// Add_profil
|
||||
// Kdebug_trace
|
||||
// Sigreturn
|
||||
// Atsocket
|
||||
// Kqueue_from_portset_np
|
||||
// Kqueue_portset
|
||||
// Getattrlist
|
||||
// Getdirentriesattr
|
||||
// Searchfs
|
||||
// Delete
|
||||
// Copyfile
|
||||
// Watchevent
|
||||
// Waitevent
|
||||
// Modwatch
|
||||
// Fsctl
|
||||
// Initgroups
|
||||
// Posix_spawn
|
||||
// Nfsclnt
|
||||
// Fhopen
|
||||
// Minherit
|
||||
// Semsys
|
||||
// Msgsys
|
||||
// Shmsys
|
||||
// Semctl
|
||||
// Semget
|
||||
// Semop
|
||||
// Msgctl
|
||||
// Msgget
|
||||
// Msgsnd
|
||||
// Msgrcv
|
||||
// Shm_open
|
||||
// Shm_unlink
|
||||
// Sem_open
|
||||
// Sem_close
|
||||
// Sem_unlink
|
||||
// Sem_wait
|
||||
// Sem_trywait
|
||||
// Sem_post
|
||||
// Sem_getvalue
|
||||
// Sem_init
|
||||
// Sem_destroy
|
||||
// Open_extended
|
||||
// Umask_extended
|
||||
// Stat_extended
|
||||
// Lstat_extended
|
||||
// Fstat_extended
|
||||
// Chmod_extended
|
||||
// Fchmod_extended
|
||||
// Access_extended
|
||||
// Settid
|
||||
// Gettid
|
||||
// Setsgroups
|
||||
// Getsgroups
|
||||
// Setwgroups
|
||||
// Getwgroups
|
||||
// Mkfifo_extended
|
||||
// Mkdir_extended
|
||||
// Identitysvc
|
||||
// Shared_region_check_np
|
||||
// Shared_region_map_np
|
||||
// __pthread_mutex_destroy
|
||||
// __pthread_mutex_init
|
||||
// __pthread_mutex_lock
|
||||
// __pthread_mutex_trylock
|
||||
// __pthread_mutex_unlock
|
||||
// __pthread_cond_init
|
||||
// __pthread_cond_destroy
|
||||
// __pthread_cond_broadcast
|
||||
// __pthread_cond_signal
|
||||
// Setsid_with_pid
|
||||
// __pthread_cond_timedwait
|
||||
// Aio_fsync
|
||||
// Aio_return
|
||||
// Aio_suspend
|
||||
// Aio_cancel
|
||||
// Aio_error
|
||||
// Aio_read
|
||||
// Aio_write
|
||||
// Lio_listio
|
||||
// __pthread_cond_wait
|
||||
// Iopolicysys
|
||||
// __pthread_kill
|
||||
// __pthread_sigmask
|
||||
// __sigwait
|
||||
// __disable_threadsignal
|
||||
// __pthread_markcancel
|
||||
// __pthread_canceled
|
||||
// __semwait_signal
|
||||
// Proc_info
|
||||
// sendfile
|
||||
// Stat64_extended
|
||||
// Lstat64_extended
|
||||
// Fstat64_extended
|
||||
// __pthread_chdir
|
||||
// __pthread_fchdir
|
||||
// Audit
|
||||
// Auditon
|
||||
// Getauid
|
||||
// Setauid
|
||||
// Getaudit
|
||||
// Setaudit
|
||||
// Getaudit_addr
|
||||
// Setaudit_addr
|
||||
// Auditctl
|
||||
// Bsdthread_create
|
||||
// Bsdthread_terminate
|
||||
// Stack_snapshot
|
||||
// Bsdthread_register
|
||||
// Workq_open
|
||||
// Workq_ops
|
||||
// __mac_execve
|
||||
// __mac_syscall
|
||||
// __mac_get_file
|
||||
// __mac_set_file
|
||||
// __mac_get_link
|
||||
// __mac_set_link
|
||||
// __mac_get_proc
|
||||
// __mac_set_proc
|
||||
// __mac_get_fd
|
||||
// __mac_set_fd
|
||||
// __mac_get_pid
|
||||
// __mac_get_lcid
|
||||
// __mac_get_lctx
|
||||
// __mac_set_lctx
|
||||
// Setlcid
|
||||
// Read_nocancel
|
||||
// Write_nocancel
|
||||
// Open_nocancel
|
||||
// Close_nocancel
|
||||
// Wait4_nocancel
|
||||
// Recvmsg_nocancel
|
||||
// Sendmsg_nocancel
|
||||
// Recvfrom_nocancel
|
||||
// Accept_nocancel
|
||||
// Fcntl_nocancel
|
||||
// Select_nocancel
|
||||
// Fsync_nocancel
|
||||
// Connect_nocancel
|
||||
// Sigsuspend_nocancel
|
||||
// Readv_nocancel
|
||||
// Writev_nocancel
|
||||
// Sendto_nocancel
|
||||
// Pread_nocancel
|
||||
// Pwrite_nocancel
|
||||
// Waitid_nocancel
|
||||
// Poll_nocancel
|
||||
// Msgsnd_nocancel
|
||||
// Msgrcv_nocancel
|
||||
// Sem_wait_nocancel
|
||||
// Aio_suspend_nocancel
|
||||
// __sigwait_nocancel
|
||||
// __semwait_signal_nocancel
|
||||
// __mac_mount
|
||||
// __mac_get_mount
|
||||
// __mac_getfsstat
|
||||
|
|
|
@ -47,6 +47,5 @@ func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr,
|
|||
//sys getfsstat(buf unsafe.Pointer, size uintptr, flags int) (n int, err error) = SYS_GETFSSTAT64
|
||||
//sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
|
||||
//sys ptrace1(request int, pid int, addr uintptr, data uintptr) (err error) = SYS_ptrace
|
||||
//sys ptrace1Ptr(request int, pid int, addr unsafe.Pointer, data uintptr) (err error) = SYS_ptrace
|
||||
//sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
|
||||
//sys Statfs(path string, stat *Statfs_t) (err error) = SYS_STATFS64
|
||||
|
|
|
@ -47,6 +47,5 @@ func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr,
|
|||
//sys getfsstat(buf unsafe.Pointer, size uintptr, flags int) (n int, err error) = SYS_GETFSSTAT
|
||||
//sys Lstat(path string, stat *Stat_t) (err error)
|
||||
//sys ptrace1(request int, pid int, addr uintptr, data uintptr) (err error) = SYS_ptrace
|
||||
//sys ptrace1Ptr(request int, pid int, addr unsafe.Pointer, data uintptr) (err error) = SYS_ptrace
|
||||
//sys Stat(path string, stat *Stat_t) (err error)
|
||||
//sys Statfs(path string, stat *Statfs_t) (err error)
|
||||
|
|
|
@ -343,203 +343,5 @@ func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
|
|||
//sys write(fd int, p []byte) (n int, err error)
|
||||
//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
|
||||
//sys munmap(addr uintptr, length uintptr) (err error)
|
||||
//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
|
||||
//sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
|
||||
//sys accept4(fd int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (nfd int, err error)
|
||||
//sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
|
||||
|
||||
/*
|
||||
* Unimplemented
|
||||
* TODO(jsing): Update this list for DragonFly.
|
||||
*/
|
||||
// Profil
|
||||
// Sigaction
|
||||
// Sigprocmask
|
||||
// Getlogin
|
||||
// Sigpending
|
||||
// Sigaltstack
|
||||
// Reboot
|
||||
// Execve
|
||||
// Vfork
|
||||
// Sbrk
|
||||
// Sstk
|
||||
// Ovadvise
|
||||
// Mincore
|
||||
// Setitimer
|
||||
// Swapon
|
||||
// Select
|
||||
// Sigsuspend
|
||||
// Readv
|
||||
// Writev
|
||||
// Nfssvc
|
||||
// Getfh
|
||||
// Quotactl
|
||||
// Mount
|
||||
// Csops
|
||||
// Waitid
|
||||
// Add_profil
|
||||
// Kdebug_trace
|
||||
// Sigreturn
|
||||
// Atsocket
|
||||
// Kqueue_from_portset_np
|
||||
// Kqueue_portset
|
||||
// Getattrlist
|
||||
// Setattrlist
|
||||
// Getdirentriesattr
|
||||
// Searchfs
|
||||
// Delete
|
||||
// Copyfile
|
||||
// Watchevent
|
||||
// Waitevent
|
||||
// Modwatch
|
||||
// Getxattr
|
||||
// Fgetxattr
|
||||
// Setxattr
|
||||
// Fsetxattr
|
||||
// Removexattr
|
||||
// Fremovexattr
|
||||
// Listxattr
|
||||
// Flistxattr
|
||||
// Fsctl
|
||||
// Initgroups
|
||||
// Posix_spawn
|
||||
// Nfsclnt
|
||||
// Fhopen
|
||||
// Minherit
|
||||
// Semsys
|
||||
// Msgsys
|
||||
// Shmsys
|
||||
// Semctl
|
||||
// Semget
|
||||
// Semop
|
||||
// Msgctl
|
||||
// Msgget
|
||||
// Msgsnd
|
||||
// Msgrcv
|
||||
// Shmat
|
||||
// Shmctl
|
||||
// Shmdt
|
||||
// Shmget
|
||||
// Shm_open
|
||||
// Shm_unlink
|
||||
// Sem_open
|
||||
// Sem_close
|
||||
// Sem_unlink
|
||||
// Sem_wait
|
||||
// Sem_trywait
|
||||
// Sem_post
|
||||
// Sem_getvalue
|
||||
// Sem_init
|
||||
// Sem_destroy
|
||||
// Open_extended
|
||||
// Umask_extended
|
||||
// Stat_extended
|
||||
// Lstat_extended
|
||||
// Fstat_extended
|
||||
// Chmod_extended
|
||||
// Fchmod_extended
|
||||
// Access_extended
|
||||
// Settid
|
||||
// Gettid
|
||||
// Setsgroups
|
||||
// Getsgroups
|
||||
// Setwgroups
|
||||
// Getwgroups
|
||||
// Mkfifo_extended
|
||||
// Mkdir_extended
|
||||
// Identitysvc
|
||||
// Shared_region_check_np
|
||||
// Shared_region_map_np
|
||||
// __pthread_mutex_destroy
|
||||
// __pthread_mutex_init
|
||||
// __pthread_mutex_lock
|
||||
// __pthread_mutex_trylock
|
||||
// __pthread_mutex_unlock
|
||||
// __pthread_cond_init
|
||||
// __pthread_cond_destroy
|
||||
// __pthread_cond_broadcast
|
||||
// __pthread_cond_signal
|
||||
// Setsid_with_pid
|
||||
// __pthread_cond_timedwait
|
||||
// Aio_fsync
|
||||
// Aio_return
|
||||
// Aio_suspend
|
||||
// Aio_cancel
|
||||
// Aio_error
|
||||
// Aio_read
|
||||
// Aio_write
|
||||
// Lio_listio
|
||||
// __pthread_cond_wait
|
||||
// Iopolicysys
|
||||
// __pthread_kill
|
||||
// __pthread_sigmask
|
||||
// __sigwait
|
||||
// __disable_threadsignal
|
||||
// __pthread_markcancel
|
||||
// __pthread_canceled
|
||||
// __semwait_signal
|
||||
// Proc_info
|
||||
// Stat64_extended
|
||||
// Lstat64_extended
|
||||
// Fstat64_extended
|
||||
// __pthread_chdir
|
||||
// __pthread_fchdir
|
||||
// Audit
|
||||
// Auditon
|
||||
// Getauid
|
||||
// Setauid
|
||||
// Getaudit
|
||||
// Setaudit
|
||||
// Getaudit_addr
|
||||
// Setaudit_addr
|
||||
// Auditctl
|
||||
// Bsdthread_create
|
||||
// Bsdthread_terminate
|
||||
// Stack_snapshot
|
||||
// Bsdthread_register
|
||||
// Workq_open
|
||||
// Workq_ops
|
||||
// __mac_execve
|
||||
// __mac_syscall
|
||||
// __mac_get_file
|
||||
// __mac_set_file
|
||||
// __mac_get_link
|
||||
// __mac_set_link
|
||||
// __mac_get_proc
|
||||
// __mac_set_proc
|
||||
// __mac_get_fd
|
||||
// __mac_set_fd
|
||||
// __mac_get_pid
|
||||
// __mac_get_lcid
|
||||
// __mac_get_lctx
|
||||
// __mac_set_lctx
|
||||
// Setlcid
|
||||
// Read_nocancel
|
||||
// Write_nocancel
|
||||
// Open_nocancel
|
||||
// Close_nocancel
|
||||
// Wait4_nocancel
|
||||
// Recvmsg_nocancel
|
||||
// Sendmsg_nocancel
|
||||
// Recvfrom_nocancel
|
||||
// Accept_nocancel
|
||||
// Fcntl_nocancel
|
||||
// Select_nocancel
|
||||
// Fsync_nocancel
|
||||
// Connect_nocancel
|
||||
// Sigsuspend_nocancel
|
||||
// Readv_nocancel
|
||||
// Writev_nocancel
|
||||
// Sendto_nocancel
|
||||
// Pread_nocancel
|
||||
// Pwrite_nocancel
|
||||
// Waitid_nocancel
|
||||
// Msgsnd_nocancel
|
||||
// Msgrcv_nocancel
|
||||
// Sem_wait_nocancel
|
||||
// Aio_suspend_nocancel
|
||||
// __sigwait_nocancel
|
||||
// __semwait_signal_nocancel
|
||||
// __mac_mount
|
||||
// __mac_get_mount
|
||||
// __mac_getfsstat
|
||||
|
|
|
@ -449,197 +449,5 @@ func Dup3(oldfd, newfd, flags int) error {
|
|||
//sys write(fd int, p []byte) (n int, err error)
|
||||
//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
|
||||
//sys munmap(addr uintptr, length uintptr) (err error)
|
||||
//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
|
||||
//sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
|
||||
//sys accept4(fd int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (nfd int, err error)
|
||||
//sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
|
||||
|
||||
/*
|
||||
* Unimplemented
|
||||
*/
|
||||
// Profil
|
||||
// Sigaction
|
||||
// Sigprocmask
|
||||
// Getlogin
|
||||
// Sigpending
|
||||
// Sigaltstack
|
||||
// Ioctl
|
||||
// Reboot
|
||||
// Execve
|
||||
// Vfork
|
||||
// Sbrk
|
||||
// Sstk
|
||||
// Ovadvise
|
||||
// Mincore
|
||||
// Setitimer
|
||||
// Swapon
|
||||
// Select
|
||||
// Sigsuspend
|
||||
// Readv
|
||||
// Writev
|
||||
// Nfssvc
|
||||
// Getfh
|
||||
// Quotactl
|
||||
// Mount
|
||||
// Csops
|
||||
// Waitid
|
||||
// Add_profil
|
||||
// Kdebug_trace
|
||||
// Sigreturn
|
||||
// Atsocket
|
||||
// Kqueue_from_portset_np
|
||||
// Kqueue_portset
|
||||
// Getattrlist
|
||||
// Setattrlist
|
||||
// Getdents
|
||||
// Getdirentriesattr
|
||||
// Searchfs
|
||||
// Delete
|
||||
// Copyfile
|
||||
// Watchevent
|
||||
// Waitevent
|
||||
// Modwatch
|
||||
// Fsctl
|
||||
// Initgroups
|
||||
// Posix_spawn
|
||||
// Nfsclnt
|
||||
// Fhopen
|
||||
// Minherit
|
||||
// Semsys
|
||||
// Msgsys
|
||||
// Shmsys
|
||||
// Semctl
|
||||
// Semget
|
||||
// Semop
|
||||
// Msgctl
|
||||
// Msgget
|
||||
// Msgsnd
|
||||
// Msgrcv
|
||||
// Shmat
|
||||
// Shmctl
|
||||
// Shmdt
|
||||
// Shmget
|
||||
// Shm_open
|
||||
// Shm_unlink
|
||||
// Sem_open
|
||||
// Sem_close
|
||||
// Sem_unlink
|
||||
// Sem_wait
|
||||
// Sem_trywait
|
||||
// Sem_post
|
||||
// Sem_getvalue
|
||||
// Sem_init
|
||||
// Sem_destroy
|
||||
// Open_extended
|
||||
// Umask_extended
|
||||
// Stat_extended
|
||||
// Lstat_extended
|
||||
// Fstat_extended
|
||||
// Chmod_extended
|
||||
// Fchmod_extended
|
||||
// Access_extended
|
||||
// Settid
|
||||
// Gettid
|
||||
// Setsgroups
|
||||
// Getsgroups
|
||||
// Setwgroups
|
||||
// Getwgroups
|
||||
// Mkfifo_extended
|
||||
// Mkdir_extended
|
||||
// Identitysvc
|
||||
// Shared_region_check_np
|
||||
// Shared_region_map_np
|
||||
// __pthread_mutex_destroy
|
||||
// __pthread_mutex_init
|
||||
// __pthread_mutex_lock
|
||||
// __pthread_mutex_trylock
|
||||
// __pthread_mutex_unlock
|
||||
// __pthread_cond_init
|
||||
// __pthread_cond_destroy
|
||||
// __pthread_cond_broadcast
|
||||
// __pthread_cond_signal
|
||||
// Setsid_with_pid
|
||||
// __pthread_cond_timedwait
|
||||
// Aio_fsync
|
||||
// Aio_return
|
||||
// Aio_suspend
|
||||
// Aio_cancel
|
||||
// Aio_error
|
||||
// Aio_read
|
||||
// Aio_write
|
||||
// Lio_listio
|
||||
// __pthread_cond_wait
|
||||
// Iopolicysys
|
||||
// __pthread_kill
|
||||
// __pthread_sigmask
|
||||
// __sigwait
|
||||
// __disable_threadsignal
|
||||
// __pthread_markcancel
|
||||
// __pthread_canceled
|
||||
// __semwait_signal
|
||||
// Proc_info
|
||||
// Stat64_extended
|
||||
// Lstat64_extended
|
||||
// Fstat64_extended
|
||||
// __pthread_chdir
|
||||
// __pthread_fchdir
|
||||
// Audit
|
||||
// Auditon
|
||||
// Getauid
|
||||
// Setauid
|
||||
// Getaudit
|
||||
// Setaudit
|
||||
// Getaudit_addr
|
||||
// Setaudit_addr
|
||||
// Auditctl
|
||||
// Bsdthread_create
|
||||
// Bsdthread_terminate
|
||||
// Stack_snapshot
|
||||
// Bsdthread_register
|
||||
// Workq_open
|
||||
// Workq_ops
|
||||
// __mac_execve
|
||||
// __mac_syscall
|
||||
// __mac_get_file
|
||||
// __mac_set_file
|
||||
// __mac_get_link
|
||||
// __mac_set_link
|
||||
// __mac_get_proc
|
||||
// __mac_set_proc
|
||||
// __mac_get_fd
|
||||
// __mac_set_fd
|
||||
// __mac_get_pid
|
||||
// __mac_get_lcid
|
||||
// __mac_get_lctx
|
||||
// __mac_set_lctx
|
||||
// Setlcid
|
||||
// Read_nocancel
|
||||
// Write_nocancel
|
||||
// Open_nocancel
|
||||
// Close_nocancel
|
||||
// Wait4_nocancel
|
||||
// Recvmsg_nocancel
|
||||
// Sendmsg_nocancel
|
||||
// Recvfrom_nocancel
|
||||
// Accept_nocancel
|
||||
// Fcntl_nocancel
|
||||
// Select_nocancel
|
||||
// Fsync_nocancel
|
||||
// Connect_nocancel
|
||||
// Sigsuspend_nocancel
|
||||
// Readv_nocancel
|
||||
// Writev_nocancel
|
||||
// Sendto_nocancel
|
||||
// Pread_nocancel
|
||||
// Pwrite_nocancel
|
||||
// Waitid_nocancel
|
||||
// Poll_nocancel
|
||||
// Msgsnd_nocancel
|
||||
// Msgrcv_nocancel
|
||||
// Sem_wait_nocancel
|
||||
// Aio_suspend_nocancel
|
||||
// __sigwait_nocancel
|
||||
// __semwait_signal_nocancel
|
||||
// __mac_mount
|
||||
// __mac_get_mount
|
||||
// __mac_getfsstat
|
||||
|
|
|
@ -693,10 +693,10 @@ type SockaddrALG struct {
|
|||
|
||||
func (sa *SockaddrALG) sockaddr() (unsafe.Pointer, _Socklen, error) {
|
||||
// Leave room for NUL byte terminator.
|
||||
if len(sa.Type) > 13 {
|
||||
if len(sa.Type) > len(sa.raw.Type)-1 {
|
||||
return nil, 0, EINVAL
|
||||
}
|
||||
if len(sa.Name) > 63 {
|
||||
if len(sa.Name) > len(sa.raw.Name)-1 {
|
||||
return nil, 0, EINVAL
|
||||
}
|
||||
|
||||
|
@ -704,17 +704,8 @@ func (sa *SockaddrALG) sockaddr() (unsafe.Pointer, _Socklen, error) {
|
|||
sa.raw.Feat = sa.Feature
|
||||
sa.raw.Mask = sa.Mask
|
||||
|
||||
typ, err := ByteSliceFromString(sa.Type)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
name, err := ByteSliceFromString(sa.Name)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
copy(sa.raw.Type[:], typ)
|
||||
copy(sa.raw.Name[:], name)
|
||||
copy(sa.raw.Type[:], sa.Type)
|
||||
copy(sa.raw.Name[:], sa.Name)
|
||||
|
||||
return unsafe.Pointer(&sa.raw), SizeofSockaddrALG, nil
|
||||
}
|
||||
|
@ -1988,8 +1979,6 @@ func Signalfd(fd int, sigmask *Sigset_t, flags int) (newfd int, err error) {
|
|||
//sys Unshare(flags int) (err error)
|
||||
//sys write(fd int, p []byte) (n int, err error)
|
||||
//sys exitThread(code int) (err error) = SYS_EXIT
|
||||
//sys readlen(fd int, p *byte, np int) (n int, err error) = SYS_READ
|
||||
//sys writelen(fd int, p *byte, np int) (n int, err error) = SYS_WRITE
|
||||
//sys readv(fd int, iovs []Iovec) (n int, err error) = SYS_READV
|
||||
//sys writev(fd int, iovs []Iovec) (n int, err error) = SYS_WRITEV
|
||||
//sys preadv(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr) (n int, err error) = SYS_PREADV
|
||||
|
@ -2471,98 +2460,25 @@ func Pselect(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timespec, sigmask *
|
|||
return pselect6(nfd, r, w, e, mutableTimeout, kernelMask)
|
||||
}
|
||||
|
||||
/*
|
||||
* Unimplemented
|
||||
*/
|
||||
// AfsSyscall
|
||||
// ArchPrctl
|
||||
// Brk
|
||||
// ClockNanosleep
|
||||
// ClockSettime
|
||||
// Clone
|
||||
// EpollCtlOld
|
||||
// EpollPwait
|
||||
// EpollWaitOld
|
||||
// Execve
|
||||
// Fork
|
||||
// Futex
|
||||
// GetKernelSyms
|
||||
// GetMempolicy
|
||||
// GetRobustList
|
||||
// GetThreadArea
|
||||
// Getpmsg
|
||||
// IoCancel
|
||||
// IoDestroy
|
||||
// IoGetevents
|
||||
// IoSetup
|
||||
// IoSubmit
|
||||
// IoprioGet
|
||||
// IoprioSet
|
||||
// KexecLoad
|
||||
// LookupDcookie
|
||||
// Mbind
|
||||
// MigratePages
|
||||
// Mincore
|
||||
// ModifyLdt
|
||||
// Mount
|
||||
// MovePages
|
||||
// MqGetsetattr
|
||||
// MqNotify
|
||||
// MqOpen
|
||||
// MqTimedreceive
|
||||
// MqTimedsend
|
||||
// MqUnlink
|
||||
// Msgctl
|
||||
// Msgget
|
||||
// Msgrcv
|
||||
// Msgsnd
|
||||
// Nfsservctl
|
||||
// Personality
|
||||
// Pselect6
|
||||
// Ptrace
|
||||
// Putpmsg
|
||||
// Quotactl
|
||||
// Readahead
|
||||
// Readv
|
||||
// RemapFilePages
|
||||
// RestartSyscall
|
||||
// RtSigaction
|
||||
// RtSigpending
|
||||
// RtSigqueueinfo
|
||||
// RtSigreturn
|
||||
// RtSigsuspend
|
||||
// RtSigtimedwait
|
||||
// SchedGetPriorityMax
|
||||
// SchedGetPriorityMin
|
||||
// SchedGetparam
|
||||
// SchedGetscheduler
|
||||
// SchedRrGetInterval
|
||||
// SchedSetparam
|
||||
// SchedYield
|
||||
// Security
|
||||
// Semctl
|
||||
// Semget
|
||||
// Semop
|
||||
// Semtimedop
|
||||
// SetMempolicy
|
||||
// SetRobustList
|
||||
// SetThreadArea
|
||||
// SetTidAddress
|
||||
// Sigaltstack
|
||||
// Swapoff
|
||||
// Swapon
|
||||
// Sysfs
|
||||
// TimerCreate
|
||||
// TimerDelete
|
||||
// TimerGetoverrun
|
||||
// TimerGettime
|
||||
// TimerSettime
|
||||
// Tkill (obsolete)
|
||||
// Tuxcall
|
||||
// Umount2
|
||||
// Uselib
|
||||
// Utimensat
|
||||
// Vfork
|
||||
// Vhangup
|
||||
// Vserver
|
||||
// _Sysctl
|
||||
//sys schedSetattr(pid int, attr *SchedAttr, flags uint) (err error)
|
||||
//sys schedGetattr(pid int, attr *SchedAttr, size uint, flags uint) (err error)
|
||||
|
||||
// SchedSetAttr is a wrapper for sched_setattr(2) syscall.
|
||||
// https://man7.org/linux/man-pages/man2/sched_setattr.2.html
|
||||
func SchedSetAttr(pid int, attr *SchedAttr, flags uint) error {
|
||||
if attr == nil {
|
||||
return EINVAL
|
||||
}
|
||||
attr.Size = SizeofSchedAttr
|
||||
return schedSetattr(pid, attr, flags)
|
||||
}
|
||||
|
||||
// SchedGetAttr is a wrapper for sched_getattr(2) syscall.
|
||||
// https://man7.org/linux/man-pages/man2/sched_getattr.2.html
|
||||
func SchedGetAttr(pid int, flags uint) (*SchedAttr, error) {
|
||||
attr := &SchedAttr{}
|
||||
if err := schedGetattr(pid, attr, SizeofSchedAttr, flags); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return attr, nil
|
||||
}
|
||||
|
|
|
@ -356,8 +356,6 @@ func Statvfs(path string, buf *Statvfs_t) (err error) {
|
|||
//sys write(fd int, p []byte) (n int, err error)
|
||||
//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
|
||||
//sys munmap(addr uintptr, length uintptr) (err error)
|
||||
//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
|
||||
//sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
|
||||
//sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
|
||||
|
||||
const (
|
||||
|
@ -371,262 +369,3 @@ const (
|
|||
func mremap(oldaddr uintptr, oldlength uintptr, newlength uintptr, flags int, newaddr uintptr) (uintptr, error) {
|
||||
return mremapNetBSD(oldaddr, oldlength, newaddr, newlength, flags)
|
||||
}
|
||||
|
||||
/*
|
||||
* Unimplemented
|
||||
*/
|
||||
// ____semctl13
|
||||
// __clone
|
||||
// __fhopen40
|
||||
// __fhstat40
|
||||
// __fhstatvfs140
|
||||
// __fstat30
|
||||
// __getcwd
|
||||
// __getfh30
|
||||
// __getlogin
|
||||
// __lstat30
|
||||
// __mount50
|
||||
// __msgctl13
|
||||
// __msync13
|
||||
// __ntp_gettime30
|
||||
// __posix_chown
|
||||
// __posix_fchown
|
||||
// __posix_lchown
|
||||
// __posix_rename
|
||||
// __setlogin
|
||||
// __shmctl13
|
||||
// __sigaction_sigtramp
|
||||
// __sigaltstack14
|
||||
// __sigpending14
|
||||
// __sigprocmask14
|
||||
// __sigsuspend14
|
||||
// __sigtimedwait
|
||||
// __stat30
|
||||
// __syscall
|
||||
// __vfork14
|
||||
// _ksem_close
|
||||
// _ksem_destroy
|
||||
// _ksem_getvalue
|
||||
// _ksem_init
|
||||
// _ksem_open
|
||||
// _ksem_post
|
||||
// _ksem_trywait
|
||||
// _ksem_unlink
|
||||
// _ksem_wait
|
||||
// _lwp_continue
|
||||
// _lwp_create
|
||||
// _lwp_ctl
|
||||
// _lwp_detach
|
||||
// _lwp_exit
|
||||
// _lwp_getname
|
||||
// _lwp_getprivate
|
||||
// _lwp_kill
|
||||
// _lwp_park
|
||||
// _lwp_self
|
||||
// _lwp_setname
|
||||
// _lwp_setprivate
|
||||
// _lwp_suspend
|
||||
// _lwp_unpark
|
||||
// _lwp_unpark_all
|
||||
// _lwp_wait
|
||||
// _lwp_wakeup
|
||||
// _pset_bind
|
||||
// _sched_getaffinity
|
||||
// _sched_getparam
|
||||
// _sched_setaffinity
|
||||
// _sched_setparam
|
||||
// acct
|
||||
// aio_cancel
|
||||
// aio_error
|
||||
// aio_fsync
|
||||
// aio_read
|
||||
// aio_return
|
||||
// aio_suspend
|
||||
// aio_write
|
||||
// break
|
||||
// clock_getres
|
||||
// clock_gettime
|
||||
// clock_settime
|
||||
// compat_09_ogetdomainname
|
||||
// compat_09_osetdomainname
|
||||
// compat_09_ouname
|
||||
// compat_10_omsgsys
|
||||
// compat_10_osemsys
|
||||
// compat_10_oshmsys
|
||||
// compat_12_fstat12
|
||||
// compat_12_getdirentries
|
||||
// compat_12_lstat12
|
||||
// compat_12_msync
|
||||
// compat_12_oreboot
|
||||
// compat_12_oswapon
|
||||
// compat_12_stat12
|
||||
// compat_13_sigaction13
|
||||
// compat_13_sigaltstack13
|
||||
// compat_13_sigpending13
|
||||
// compat_13_sigprocmask13
|
||||
// compat_13_sigreturn13
|
||||
// compat_13_sigsuspend13
|
||||
// compat_14___semctl
|
||||
// compat_14_msgctl
|
||||
// compat_14_shmctl
|
||||
// compat_16___sigaction14
|
||||
// compat_16___sigreturn14
|
||||
// compat_20_fhstatfs
|
||||
// compat_20_fstatfs
|
||||
// compat_20_getfsstat
|
||||
// compat_20_statfs
|
||||
// compat_30___fhstat30
|
||||
// compat_30___fstat13
|
||||
// compat_30___lstat13
|
||||
// compat_30___stat13
|
||||
// compat_30_fhopen
|
||||
// compat_30_fhstat
|
||||
// compat_30_fhstatvfs1
|
||||
// compat_30_getdents
|
||||
// compat_30_getfh
|
||||
// compat_30_ntp_gettime
|
||||
// compat_30_socket
|
||||
// compat_40_mount
|
||||
// compat_43_fstat43
|
||||
// compat_43_lstat43
|
||||
// compat_43_oaccept
|
||||
// compat_43_ocreat
|
||||
// compat_43_oftruncate
|
||||
// compat_43_ogetdirentries
|
||||
// compat_43_ogetdtablesize
|
||||
// compat_43_ogethostid
|
||||
// compat_43_ogethostname
|
||||
// compat_43_ogetkerninfo
|
||||
// compat_43_ogetpagesize
|
||||
// compat_43_ogetpeername
|
||||
// compat_43_ogetrlimit
|
||||
// compat_43_ogetsockname
|
||||
// compat_43_okillpg
|
||||
// compat_43_olseek
|
||||
// compat_43_ommap
|
||||
// compat_43_oquota
|
||||
// compat_43_orecv
|
||||
// compat_43_orecvfrom
|
||||
// compat_43_orecvmsg
|
||||
// compat_43_osend
|
||||
// compat_43_osendmsg
|
||||
// compat_43_osethostid
|
||||
// compat_43_osethostname
|
||||
// compat_43_osigblock
|
||||
// compat_43_osigsetmask
|
||||
// compat_43_osigstack
|
||||
// compat_43_osigvec
|
||||
// compat_43_otruncate
|
||||
// compat_43_owait
|
||||
// compat_43_stat43
|
||||
// execve
|
||||
// extattr_delete_fd
|
||||
// extattr_delete_file
|
||||
// extattr_delete_link
|
||||
// extattr_get_fd
|
||||
// extattr_get_file
|
||||
// extattr_get_link
|
||||
// extattr_list_fd
|
||||
// extattr_list_file
|
||||
// extattr_list_link
|
||||
// extattr_set_fd
|
||||
// extattr_set_file
|
||||
// extattr_set_link
|
||||
// extattrctl
|
||||
// fchroot
|
||||
// fdatasync
|
||||
// fgetxattr
|
||||
// fktrace
|
||||
// flistxattr
|
||||
// fork
|
||||
// fremovexattr
|
||||
// fsetxattr
|
||||
// fstatvfs1
|
||||
// fsync_range
|
||||
// getcontext
|
||||
// getitimer
|
||||
// getvfsstat
|
||||
// getxattr
|
||||
// ktrace
|
||||
// lchflags
|
||||
// lchmod
|
||||
// lfs_bmapv
|
||||
// lfs_markv
|
||||
// lfs_segclean
|
||||
// lfs_segwait
|
||||
// lgetxattr
|
||||
// lio_listio
|
||||
// listxattr
|
||||
// llistxattr
|
||||
// lremovexattr
|
||||
// lseek
|
||||
// lsetxattr
|
||||
// lutimes
|
||||
// madvise
|
||||
// mincore
|
||||
// minherit
|
||||
// modctl
|
||||
// mq_close
|
||||
// mq_getattr
|
||||
// mq_notify
|
||||
// mq_open
|
||||
// mq_receive
|
||||
// mq_send
|
||||
// mq_setattr
|
||||
// mq_timedreceive
|
||||
// mq_timedsend
|
||||
// mq_unlink
|
||||
// msgget
|
||||
// msgrcv
|
||||
// msgsnd
|
||||
// nfssvc
|
||||
// ntp_adjtime
|
||||
// pmc_control
|
||||
// pmc_get_info
|
||||
// pollts
|
||||
// preadv
|
||||
// profil
|
||||
// pselect
|
||||
// pset_assign
|
||||
// pset_create
|
||||
// pset_destroy
|
||||
// ptrace
|
||||
// pwritev
|
||||
// quotactl
|
||||
// rasctl
|
||||
// readv
|
||||
// reboot
|
||||
// removexattr
|
||||
// sa_enable
|
||||
// sa_preempt
|
||||
// sa_register
|
||||
// sa_setconcurrency
|
||||
// sa_stacks
|
||||
// sa_yield
|
||||
// sbrk
|
||||
// sched_yield
|
||||
// semconfig
|
||||
// semget
|
||||
// semop
|
||||
// setcontext
|
||||
// setitimer
|
||||
// setxattr
|
||||
// shmat
|
||||
// shmdt
|
||||
// shmget
|
||||
// sstk
|
||||
// statvfs1
|
||||
// swapctl
|
||||
// sysarch
|
||||
// syscall
|
||||
// timer_create
|
||||
// timer_delete
|
||||
// timer_getoverrun
|
||||
// timer_gettime
|
||||
// timer_settime
|
||||
// undelete
|
||||
// utrace
|
||||
// uuidgen
|
||||
// vadvise
|
||||
// vfork
|
||||
// writev
|
||||
|
|
|
@ -326,78 +326,4 @@ func Uname(uname *Utsname) error {
|
|||
//sys write(fd int, p []byte) (n int, err error)
|
||||
//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
|
||||
//sys munmap(addr uintptr, length uintptr) (err error)
|
||||
//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
|
||||
//sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
|
||||
//sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
|
||||
|
||||
/*
|
||||
* Unimplemented
|
||||
*/
|
||||
// __getcwd
|
||||
// __semctl
|
||||
// __syscall
|
||||
// __sysctl
|
||||
// adjfreq
|
||||
// break
|
||||
// clock_getres
|
||||
// clock_gettime
|
||||
// clock_settime
|
||||
// closefrom
|
||||
// execve
|
||||
// fhopen
|
||||
// fhstat
|
||||
// fhstatfs
|
||||
// fork
|
||||
// futimens
|
||||
// getfh
|
||||
// getgid
|
||||
// getitimer
|
||||
// getlogin
|
||||
// getthrid
|
||||
// ktrace
|
||||
// lfs_bmapv
|
||||
// lfs_markv
|
||||
// lfs_segclean
|
||||
// lfs_segwait
|
||||
// mincore
|
||||
// minherit
|
||||
// mount
|
||||
// mquery
|
||||
// msgctl
|
||||
// msgget
|
||||
// msgrcv
|
||||
// msgsnd
|
||||
// nfssvc
|
||||
// nnpfspioctl
|
||||
// preadv
|
||||
// profil
|
||||
// pwritev
|
||||
// quotactl
|
||||
// readv
|
||||
// reboot
|
||||
// renameat
|
||||
// rfork
|
||||
// sched_yield
|
||||
// semget
|
||||
// semop
|
||||
// setgroups
|
||||
// setitimer
|
||||
// setsockopt
|
||||
// shmat
|
||||
// shmctl
|
||||
// shmdt
|
||||
// shmget
|
||||
// sigaction
|
||||
// sigaltstack
|
||||
// sigpending
|
||||
// sigprocmask
|
||||
// sigreturn
|
||||
// sigsuspend
|
||||
// sysarch
|
||||
// syscall
|
||||
// threxit
|
||||
// thrsigdivert
|
||||
// thrsleep
|
||||
// thrwakeup
|
||||
// vfork
|
||||
// writev
|
||||
|
|
|
@ -698,24 +698,6 @@ func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
|
|||
//sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) = libsocket.setsockopt
|
||||
//sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) = libsocket.recvfrom
|
||||
|
||||
func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
|
||||
r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procread)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
|
||||
n = int(r0)
|
||||
if e1 != 0 {
|
||||
err = e1
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func writelen(fd int, buf *byte, nbuf int) (n int, err error) {
|
||||
r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procwrite)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
|
||||
n = int(r0)
|
||||
if e1 != 0 {
|
||||
err = e1
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Event Ports
|
||||
|
||||
type fileObjCookie struct {
|
||||
|
|
|
@ -549,6 +549,9 @@ func SetNonblock(fd int, nonblocking bool) (err error) {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if (flag&O_NONBLOCK != 0) == nonblocking {
|
||||
return nil
|
||||
}
|
||||
if nonblocking {
|
||||
flag |= O_NONBLOCK
|
||||
} else {
|
||||
|
|
|
@ -192,7 +192,6 @@ func (cmsg *Cmsghdr) SetLen(length int) {
|
|||
|
||||
//sys fcntl(fd int, cmd int, arg int) (val int, err error)
|
||||
//sys read(fd int, p []byte) (n int, err error)
|
||||
//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
|
||||
//sys write(fd int, p []byte) (n int, err error)
|
||||
|
||||
//sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) = SYS___ACCEPT_A
|
||||
|
|
|
@ -2421,6 +2421,15 @@ const (
|
|||
PR_PAC_GET_ENABLED_KEYS = 0x3d
|
||||
PR_PAC_RESET_KEYS = 0x36
|
||||
PR_PAC_SET_ENABLED_KEYS = 0x3c
|
||||
PR_RISCV_V_GET_CONTROL = 0x46
|
||||
PR_RISCV_V_SET_CONTROL = 0x45
|
||||
PR_RISCV_V_VSTATE_CTRL_CUR_MASK = 0x3
|
||||
PR_RISCV_V_VSTATE_CTRL_DEFAULT = 0x0
|
||||
PR_RISCV_V_VSTATE_CTRL_INHERIT = 0x10
|
||||
PR_RISCV_V_VSTATE_CTRL_MASK = 0x1f
|
||||
PR_RISCV_V_VSTATE_CTRL_NEXT_MASK = 0xc
|
||||
PR_RISCV_V_VSTATE_CTRL_OFF = 0x1
|
||||
PR_RISCV_V_VSTATE_CTRL_ON = 0x2
|
||||
PR_SCHED_CORE = 0x3e
|
||||
PR_SCHED_CORE_CREATE = 0x1
|
||||
PR_SCHED_CORE_GET = 0x0
|
||||
|
@ -2821,6 +2830,23 @@ const (
|
|||
RWF_SUPPORTED = 0x1f
|
||||
RWF_SYNC = 0x4
|
||||
RWF_WRITE_LIFE_NOT_SET = 0x0
|
||||
SCHED_BATCH = 0x3
|
||||
SCHED_DEADLINE = 0x6
|
||||
SCHED_FIFO = 0x1
|
||||
SCHED_FLAG_ALL = 0x7f
|
||||
SCHED_FLAG_DL_OVERRUN = 0x4
|
||||
SCHED_FLAG_KEEP_ALL = 0x18
|
||||
SCHED_FLAG_KEEP_PARAMS = 0x10
|
||||
SCHED_FLAG_KEEP_POLICY = 0x8
|
||||
SCHED_FLAG_RECLAIM = 0x2
|
||||
SCHED_FLAG_RESET_ON_FORK = 0x1
|
||||
SCHED_FLAG_UTIL_CLAMP = 0x60
|
||||
SCHED_FLAG_UTIL_CLAMP_MAX = 0x40
|
||||
SCHED_FLAG_UTIL_CLAMP_MIN = 0x20
|
||||
SCHED_IDLE = 0x5
|
||||
SCHED_NORMAL = 0x0
|
||||
SCHED_RESET_ON_FORK = 0x40000000
|
||||
SCHED_RR = 0x2
|
||||
SCM_CREDENTIALS = 0x2
|
||||
SCM_RIGHTS = 0x1
|
||||
SCM_TIMESTAMP = 0x1d
|
||||
|
|
|
@ -326,10 +326,12 @@ const (
|
|||
SO_NOFCS = 0x2b
|
||||
SO_OOBINLINE = 0xa
|
||||
SO_PASSCRED = 0x10
|
||||
SO_PASSPIDFD = 0x4c
|
||||
SO_PASSSEC = 0x22
|
||||
SO_PEEK_OFF = 0x2a
|
||||
SO_PEERCRED = 0x11
|
||||
SO_PEERGROUPS = 0x3b
|
||||
SO_PEERPIDFD = 0x4d
|
||||
SO_PEERSEC = 0x1f
|
||||
SO_PREFER_BUSY_POLL = 0x45
|
||||
SO_PROTOCOL = 0x26
|
||||
|
|
|
@ -327,10 +327,12 @@ const (
|
|||
SO_NOFCS = 0x2b
|
||||
SO_OOBINLINE = 0xa
|
||||
SO_PASSCRED = 0x10
|
||||
SO_PASSPIDFD = 0x4c
|
||||
SO_PASSSEC = 0x22
|
||||
SO_PEEK_OFF = 0x2a
|
||||
SO_PEERCRED = 0x11
|
||||
SO_PEERGROUPS = 0x3b
|
||||
SO_PEERPIDFD = 0x4d
|
||||
SO_PEERSEC = 0x1f
|
||||
SO_PREFER_BUSY_POLL = 0x45
|
||||
SO_PROTOCOL = 0x26
|
||||
|
|
|
@ -333,10 +333,12 @@ const (
|
|||
SO_NOFCS = 0x2b
|
||||
SO_OOBINLINE = 0xa
|
||||
SO_PASSCRED = 0x10
|
||||
SO_PASSPIDFD = 0x4c
|
||||
SO_PASSSEC = 0x22
|
||||
SO_PEEK_OFF = 0x2a
|
||||
SO_PEERCRED = 0x11
|
||||
SO_PEERGROUPS = 0x3b
|
||||
SO_PEERPIDFD = 0x4d
|
||||
SO_PEERSEC = 0x1f
|
||||
SO_PREFER_BUSY_POLL = 0x45
|
||||
SO_PROTOCOL = 0x26
|
||||
|
|
|
@ -323,10 +323,12 @@ const (
|
|||
SO_NOFCS = 0x2b
|
||||
SO_OOBINLINE = 0xa
|
||||
SO_PASSCRED = 0x10
|
||||
SO_PASSPIDFD = 0x4c
|
||||
SO_PASSSEC = 0x22
|
||||
SO_PEEK_OFF = 0x2a
|
||||
SO_PEERCRED = 0x11
|
||||
SO_PEERGROUPS = 0x3b
|
||||
SO_PEERPIDFD = 0x4d
|
||||
SO_PEERSEC = 0x1f
|
||||
SO_PREFER_BUSY_POLL = 0x45
|
||||
SO_PROTOCOL = 0x26
|
||||
|
|
|
@ -118,6 +118,8 @@ const (
|
|||
IUCLC = 0x200
|
||||
IXOFF = 0x1000
|
||||
IXON = 0x400
|
||||
LASX_CTX_MAGIC = 0x41535801
|
||||
LSX_CTX_MAGIC = 0x53580001
|
||||
MAP_ANON = 0x20
|
||||
MAP_ANONYMOUS = 0x20
|
||||
MAP_DENYWRITE = 0x800
|
||||
|
@ -317,10 +319,12 @@ const (
|
|||
SO_NOFCS = 0x2b
|
||||
SO_OOBINLINE = 0xa
|
||||
SO_PASSCRED = 0x10
|
||||
SO_PASSPIDFD = 0x4c
|
||||
SO_PASSSEC = 0x22
|
||||
SO_PEEK_OFF = 0x2a
|
||||
SO_PEERCRED = 0x11
|
||||
SO_PEERGROUPS = 0x3b
|
||||
SO_PEERPIDFD = 0x4d
|
||||
SO_PEERSEC = 0x1f
|
||||
SO_PREFER_BUSY_POLL = 0x45
|
||||
SO_PROTOCOL = 0x26
|
||||
|
|
|
@ -326,10 +326,12 @@ const (
|
|||
SO_NOFCS = 0x2b
|
||||
SO_OOBINLINE = 0x100
|
||||
SO_PASSCRED = 0x11
|
||||
SO_PASSPIDFD = 0x4c
|
||||
SO_PASSSEC = 0x22
|
||||
SO_PEEK_OFF = 0x2a
|
||||
SO_PEERCRED = 0x12
|
||||
SO_PEERGROUPS = 0x3b
|
||||
SO_PEERPIDFD = 0x4d
|
||||
SO_PEERSEC = 0x1e
|
||||
SO_PREFER_BUSY_POLL = 0x45
|
||||
SO_PROTOCOL = 0x1028
|
||||
|
|
|
@ -326,10 +326,12 @@ const (
|
|||
SO_NOFCS = 0x2b
|
||||
SO_OOBINLINE = 0x100
|
||||
SO_PASSCRED = 0x11
|
||||
SO_PASSPIDFD = 0x4c
|
||||
SO_PASSSEC = 0x22
|
||||
SO_PEEK_OFF = 0x2a
|
||||
SO_PEERCRED = 0x12
|
||||
SO_PEERGROUPS = 0x3b
|
||||
SO_PEERPIDFD = 0x4d
|
||||
SO_PEERSEC = 0x1e
|
||||
SO_PREFER_BUSY_POLL = 0x45
|
||||
SO_PROTOCOL = 0x1028
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue