Merge pull request #3913 from thaJeztah/bump_opa
vendor: github.com/open-policy-agent/opa v1.14.1
This commit is contained in:
@@ -38,7 +38,7 @@ require (
|
||||
github.com/moby/sys/atomicwriter v0.1.0
|
||||
github.com/moby/sys/mountinfo v0.7.2
|
||||
github.com/morikuni/aec v1.1.0
|
||||
github.com/open-policy-agent/opa v1.10.1
|
||||
github.com/open-policy-agent/opa v1.14.1
|
||||
github.com/opencontainers/go-digest v1.0.0
|
||||
github.com/opencontainers/image-spec v1.1.1
|
||||
github.com/pelletier/go-toml/v2 v2.3.1
|
||||
@@ -97,7 +97,6 @@ require (
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.36.6 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.43.3 // indirect
|
||||
github.com/aws/smithy-go v1.27.2 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/blang/semver v3.5.1+incompatible // indirect
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
@@ -118,7 +117,6 @@ require (
|
||||
github.com/felixge/httpsnoop v1.0.4 // indirect
|
||||
github.com/fvbommel/sortorder v1.1.0 // indirect
|
||||
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/go-logr/logr v1.4.3 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/go-openapi/analysis v0.25.2 // indirect
|
||||
@@ -162,9 +160,8 @@ require (
|
||||
github.com/lestrrat-go/dsig v1.0.0 // indirect
|
||||
github.com/lestrrat-go/dsig-secp256k1 v1.0.0 // indirect
|
||||
github.com/lestrrat-go/httpcc v1.0.1 // indirect
|
||||
github.com/lestrrat-go/httprc/v3 v3.0.1 // indirect
|
||||
github.com/lestrrat-go/jwx/v3 v3.0.11 // indirect
|
||||
github.com/lestrrat-go/option v1.0.1 // indirect
|
||||
github.com/lestrrat-go/httprc/v3 v3.0.2 // indirect
|
||||
github.com/lestrrat-go/jwx/v3 v3.0.13 // indirect
|
||||
github.com/lestrrat-go/option/v2 v2.0.0 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.23 // indirect
|
||||
github.com/mattn/go-shellwords v1.0.12 // indirect
|
||||
@@ -185,15 +182,11 @@ require (
|
||||
github.com/oklog/ulid/v2 v2.1.1 // indirect
|
||||
github.com/package-url/packageurl-go v0.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/prometheus/client_golang v1.23.2 // indirect
|
||||
github.com/prometheus/client_model v0.6.2 // indirect
|
||||
github.com/prometheus/common v0.67.5 // indirect
|
||||
github.com/prometheus/procfs v0.20.1 // indirect
|
||||
github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 // indirect
|
||||
github.com/secure-systems-lab/go-securesystemslib v0.11.0 // indirect
|
||||
github.com/segmentio/asm v1.2.0 // indirect
|
||||
github.com/segmentio/asm v1.2.1 // indirect
|
||||
github.com/shibumi/go-pathspec v1.3.0 // indirect
|
||||
github.com/sigstore/protobuf-specs v0.5.1 // indirect
|
||||
github.com/sigstore/rekor v1.5.2 // indirect
|
||||
@@ -207,8 +200,8 @@ require (
|
||||
github.com/tonistiigi/vt100 v0.0.0-20240514184818-90bafcd6abab // indirect
|
||||
github.com/transparency-dev/formats v0.1.1 // indirect
|
||||
github.com/transparency-dev/merkle v0.0.2 // indirect
|
||||
github.com/valyala/fastjson v1.6.4 // indirect
|
||||
github.com/vektah/gqlparser/v2 v2.5.30 // indirect
|
||||
github.com/valyala/fastjson v1.6.7 // indirect
|
||||
github.com/vektah/gqlparser/v2 v2.5.32 // indirect
|
||||
github.com/x448/float16 v0.8.4 // indirect
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
|
||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
|
||||
|
||||
@@ -98,8 +98,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
|
||||
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
|
||||
github.com/bytecodealliance/wasmtime-go/v37 v37.0.0 h1:DPjdn2V3JhXHMoZ2ymRqGK+y1bDyr9wgpyYCvhjMky8=
|
||||
github.com/bytecodealliance/wasmtime-go/v37 v37.0.0/go.mod h1:Pf1l2JCTUFMnOqDIwkjzx1qfVJ09xbaXETKgRVE4jZ0=
|
||||
github.com/bytecodealliance/wasmtime-go/v39 v39.0.1 h1:RibaT47yiyCRxMOj/l2cvL8cWiWBSqDXHyqsa9sGcCE=
|
||||
github.com/bytecodealliance/wasmtime-go/v39 v39.0.1/go.mod h1:miR4NYIEBXeDNamZIzpskhJ0z/p8al+lwMWylQ/ZJb4=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
|
||||
@@ -203,16 +203,14 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
|
||||
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
|
||||
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
|
||||
github.com/foxcpp/go-mockdns v1.1.0 h1:jI0rD8M0wuYAxL7r/ynTrCQQq0BVqfB99Vgk7DlmewI=
|
||||
github.com/foxcpp/go-mockdns v1.1.0/go.mod h1:IhLeSFGed3mJIAXPH2aiRQB+kqz7oqu8ld2qVbOu7Wk=
|
||||
github.com/foxcpp/go-mockdns v1.2.0 h1:omK3OrHRD1IWJz1FuFBCFquhXslXoF17OvBS6JPzZF0=
|
||||
github.com/foxcpp/go-mockdns v1.2.0/go.mod h1:IhLeSFGed3mJIAXPH2aiRQB+kqz7oqu8ld2qVbOu7Wk=
|
||||
github.com/fvbommel/sortorder v1.1.0 h1:fUmoe+HLsBTctBDoaBwpQo5N+nrCp8g/BjKb/6ZQmYw=
|
||||
github.com/fvbommel/sortorder v1.1.0/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
|
||||
github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sapM=
|
||||
github.com/fxamacker/cbor/v2 v2.9.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
|
||||
github.com/go-chi/chi/v5 v5.3.0 h1:halUjDxhshgXHMrao5bB8eNBXo/rnzwr8m5m36glehM=
|
||||
github.com/go-chi/chi/v5 v5.3.0/go.mod h1:R+tYY2hNuVUUjxoPtqUdgBqevM9s9njzkTLutVsOCto=
|
||||
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/go-jose/go-jose/v4 v4.1.4 h1:moDMcTHmvE6Groj34emNPLs/qtYXRVcd6S7NHbHz3kA=
|
||||
github.com/go-jose/go-jose/v4 v4.1.4/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
@@ -379,12 +377,10 @@ github.com/lestrrat-go/dsig-secp256k1 v1.0.0 h1:JpDe4Aybfl0soBvoVwjqDbp+9S1Y2OM7
|
||||
github.com/lestrrat-go/dsig-secp256k1 v1.0.0/go.mod h1:CxUgAhssb8FToqbL8NjSPoGQlnO4w3LG1P0qPWQm/NU=
|
||||
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/v3 v3.0.1 h1:3n7Es68YYGZb2Jf+k//llA4FTZMl3yCwIjFIk4ubevI=
|
||||
github.com/lestrrat-go/httprc/v3 v3.0.1/go.mod h1:2uAvmbXE4Xq8kAUjVrZOq1tZVYYYs5iP62Cmtru00xk=
|
||||
github.com/lestrrat-go/jwx/v3 v3.0.11 h1:yEeUGNUuNjcez/Voxvr7XPTYNraSQTENJgtVTfwvG/w=
|
||||
github.com/lestrrat-go/jwx/v3 v3.0.11/go.mod h1:XSOAh2SiXm0QgRe3DulLZLyt+wUuEdFo81zuKTLcvgQ=
|
||||
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/lestrrat-go/httprc/v3 v3.0.2 h1:7u4HUaD0NQbf2/n5+fyp+T10hNCsAnwKfqn4A4Baif0=
|
||||
github.com/lestrrat-go/httprc/v3 v3.0.2/go.mod h1:mSMtkZW92Z98M5YoNNztbRGxbXHql7tSitCvaxvo9l0=
|
||||
github.com/lestrrat-go/jwx/v3 v3.0.13 h1:AdHKiPIYeCSnOJtvdpipPg/0SuFh9rdkN+HF3O0VdSk=
|
||||
github.com/lestrrat-go/jwx/v3 v3.0.13/go.mod h1:2m0PV1A9tM4b/jVLMx8rh6rBl7F6WGb3EG2hufN9OQU=
|
||||
github.com/lestrrat-go/option/v2 v2.0.0 h1:XxrcaJESE1fokHy3FpaQ/cXW8ZsIdWcdFzzLOcID3Ss=
|
||||
github.com/lestrrat-go/option/v2 v2.0.0/go.mod h1:oSySsmzMoR0iRzCDCaUfsCzxQHUEuhOViQObyy7S6Vg=
|
||||
github.com/letsencrypt/boulder v0.20260309.0 h1:kZynrxK3QfqLGx6hhoz+Rfs3hgltJs1p9Mp+4+VwnY0=
|
||||
@@ -451,8 +447,8 @@ github.com/natefinch/atomic v1.0.1 h1:ZPYKxkqQOx3KZ+RsbnP/YsgvxWQPGxjC0oBt2AhwV0
|
||||
github.com/natefinch/atomic v1.0.1/go.mod h1:N/D/ELrljoqDyT3rZrsUmtsuzvHkeB/wWjHV22AZRbM=
|
||||
github.com/oklog/ulid/v2 v2.1.1 h1:suPZ4ARWLOJLegGFiZZ1dFAkqzhMjL3J1TzI+5wHz8s=
|
||||
github.com/oklog/ulid/v2 v2.1.1/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
|
||||
github.com/open-policy-agent/opa v1.10.1 h1:haIvxZSPky8HLjRrvQwWAjCPLg8JDFSZMbbG4yyUHgY=
|
||||
github.com/open-policy-agent/opa v1.10.1/go.mod h1:7uPI3iRpOalJ0BhK6s1JALWPU9HvaV1XeBSSMZnr/PM=
|
||||
github.com/open-policy-agent/opa v1.14.1 h1:MhurLB9mSbXmojYFCmGbiC1Uagu1+aFAV4XVotDA86M=
|
||||
github.com/open-policy-agent/opa v1.14.1/go.mod h1:B5gykwJ2l0g0wZS4ClCcpfSSEx51n4NHpTsWfuPwqnQ=
|
||||
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
||||
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
||||
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
|
||||
@@ -499,8 +495,8 @@ github.com/sassoftware/relic/v7 v7.6.2 h1:rS44Lbv9G9eXsukknS4mSjIAuuX+lMq/FnStgm
|
||||
github.com/sassoftware/relic/v7 v7.6.2/go.mod h1:kjmP0IBVkJZ6gXeAu35/KCEfca//+PKM6vTAsyDPY+k=
|
||||
github.com/secure-systems-lab/go-securesystemslib v0.11.0 h1:iuCR9kcMFD4QurdKrGvPLoKZLv9YvwPYVr0473BdtFs=
|
||||
github.com/secure-systems-lab/go-securesystemslib v0.11.0/go.mod h1:+PMOTjUGwHj2vcZ+TFKlb1tXRbrdWE1LYDT5i9JC80Q=
|
||||
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
|
||||
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
|
||||
github.com/segmentio/asm v1.2.1 h1:DTNbBqs57ioxAD4PrArqftgypG4/qNpXoJx8TVXxPR0=
|
||||
github.com/segmentio/asm v1.2.1/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
|
||||
github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw=
|
||||
github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
|
||||
github.com/serialx/hashring v0.0.0-20200727003509-22c0c7ab6b1b h1:h+3JX2VoWTFuyQEo87pStk/a99dzIO1mM9KxIyLPGTU=
|
||||
@@ -540,7 +536,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
@@ -576,12 +571,12 @@ github.com/transparency-dev/formats v0.1.1 h1:4bVHJc+KdBgpA1OJD1yjI+g0i5Z1graCpp
|
||||
github.com/transparency-dev/formats v0.1.1/go.mod h1:qtZ8goRuJ8FTBG9c9+Bj0rn2rUG7eG/AUTkr+Aw3jFw=
|
||||
github.com/transparency-dev/merkle v0.0.2 h1:Q9nBoQcZcgPamMkGn7ghV8XiTZ/kRxn1yCG81+twTK4=
|
||||
github.com/transparency-dev/merkle v0.0.2/go.mod h1:pqSy+OXefQ1EDUVmAJ8MUhHB9TXGuzVAT58PqBoHz1A=
|
||||
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
|
||||
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
|
||||
github.com/valyala/fastjson v1.6.7 h1:ZE4tRy0CIkh+qDc5McjatheGX2czdn8slQjomexVpBM=
|
||||
github.com/valyala/fastjson v1.6.7/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
|
||||
github.com/vbatts/tar-split v0.12.3 h1:Cd46rkGXI3Td4yrVNwU8ripbxFaQbmesqhjBUUYAJSw=
|
||||
github.com/vbatts/tar-split v0.12.3/go.mod h1:sQOc6OlqGCr7HkGx/IDBeKiTIvqhmj8KffNhEXG4Nq0=
|
||||
github.com/vektah/gqlparser/v2 v2.5.30 h1:EqLwGAFLIzt1wpx1IPpY67DwUujF1OfzgEyDsLrN6kE=
|
||||
github.com/vektah/gqlparser/v2 v2.5.30/go.mod h1:D1/VCZtV3LPnQrcPBeR/q5jkSQIPti0uYCP/RI0gIeo=
|
||||
github.com/vektah/gqlparser/v2 v2.5.32 h1:k9QPJd4sEDTL+qB4ncPLflqTJ3MmjB9SrVzJrawpFSc=
|
||||
github.com/vektah/gqlparser/v2 v2.5.32/go.mod h1:c1I28gSOVNzlfc4WuDlqU7voQnsqI6OG2amkBAFmgts=
|
||||
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
|
||||
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
|
||||
|
||||
@@ -55,6 +55,7 @@ func builtins() []*ast.Builtin {
|
||||
|
||||
// Arrays
|
||||
ast.ArrayConcat,
|
||||
ast.ArrayFlatten,
|
||||
ast.ArraySlice,
|
||||
ast.ArrayReverse,
|
||||
|
||||
@@ -196,6 +197,9 @@ func builtins() []*ast.Builtin {
|
||||
// Printing
|
||||
ast.Print,
|
||||
ast.InternalPrint,
|
||||
|
||||
// Internal implementation for template strings.
|
||||
ast.InternalTemplateString,
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
@@ -917,6 +917,35 @@ decision := {
|
||||
}, caps)
|
||||
}
|
||||
|
||||
func TestPolicyOPA114Builtins(t *testing.T) {
|
||||
p := NewPolicy(Opt{
|
||||
Files: []File{{
|
||||
Filename: "policy.rego",
|
||||
Data: []byte(`
|
||||
package docker
|
||||
|
||||
segments := array.flatten([["exec"], ["proxy"]])
|
||||
|
||||
decision := {
|
||||
"allow": true,
|
||||
"caps": {
|
||||
"exec.proxy": true,
|
||||
},
|
||||
} if {
|
||||
msg := $"execute {segments[0]}.{segments[1]}"
|
||||
msg == "execute exec.proxy"
|
||||
}
|
||||
`),
|
||||
}},
|
||||
})
|
||||
|
||||
caps, err := p.CheckCaps(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, Caps{
|
||||
CapExecProxy: true,
|
||||
}, caps)
|
||||
}
|
||||
|
||||
func TestCheckCapsMalformedCaps(t *testing.T) {
|
||||
p := NewPolicy(Opt{
|
||||
Files: []File{{
|
||||
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
Copyright (C) 2013 Blake Mizerany
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
-2388
File diff suppressed because it is too large
Load Diff
-316
@@ -1,316 +0,0 @@
|
||||
// Package quantile computes approximate quantiles over an unbounded data
|
||||
// stream within low memory and CPU bounds.
|
||||
//
|
||||
// A small amount of accuracy is traded to achieve the above properties.
|
||||
//
|
||||
// Multiple streams can be merged before calling Query to generate a single set
|
||||
// of results. This is meaningful when the streams represent the same type of
|
||||
// data. See Merge and Samples.
|
||||
//
|
||||
// For more detailed information about the algorithm used, see:
|
||||
//
|
||||
// Effective Computation of Biased Quantiles over Data Streams
|
||||
//
|
||||
// http://www.cs.rutgers.edu/~muthu/bquant.pdf
|
||||
package quantile
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Sample holds an observed value and meta information for compression. JSON
|
||||
// tags have been added for convenience.
|
||||
type Sample struct {
|
||||
Value float64 `json:",string"`
|
||||
Width float64 `json:",string"`
|
||||
Delta float64 `json:",string"`
|
||||
}
|
||||
|
||||
// Samples represents a slice of samples. It implements sort.Interface.
|
||||
type Samples []Sample
|
||||
|
||||
func (a Samples) Len() int { return len(a) }
|
||||
func (a Samples) Less(i, j int) bool { return a[i].Value < a[j].Value }
|
||||
func (a Samples) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
|
||||
type invariant func(s *stream, r float64) float64
|
||||
|
||||
// NewLowBiased returns an initialized Stream for low-biased quantiles
|
||||
// (e.g. 0.01, 0.1, 0.5) where the needed quantiles are not known a priori, but
|
||||
// error guarantees can still be given even for the lower ranks of the data
|
||||
// distribution.
|
||||
//
|
||||
// The provided epsilon is a relative error, i.e. the true quantile of a value
|
||||
// returned by a query is guaranteed to be within (1±Epsilon)*Quantile.
|
||||
//
|
||||
// See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error
|
||||
// properties.
|
||||
func NewLowBiased(epsilon float64) *Stream {
|
||||
ƒ := func(s *stream, r float64) float64 {
|
||||
return 2 * epsilon * r
|
||||
}
|
||||
return newStream(ƒ)
|
||||
}
|
||||
|
||||
// NewHighBiased returns an initialized Stream for high-biased quantiles
|
||||
// (e.g. 0.01, 0.1, 0.5) where the needed quantiles are not known a priori, but
|
||||
// error guarantees can still be given even for the higher ranks of the data
|
||||
// distribution.
|
||||
//
|
||||
// The provided epsilon is a relative error, i.e. the true quantile of a value
|
||||
// returned by a query is guaranteed to be within 1-(1±Epsilon)*(1-Quantile).
|
||||
//
|
||||
// See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error
|
||||
// properties.
|
||||
func NewHighBiased(epsilon float64) *Stream {
|
||||
ƒ := func(s *stream, r float64) float64 {
|
||||
return 2 * epsilon * (s.n - r)
|
||||
}
|
||||
return newStream(ƒ)
|
||||
}
|
||||
|
||||
// NewTargeted returns an initialized Stream concerned with a particular set of
|
||||
// quantile values that are supplied a priori. Knowing these a priori reduces
|
||||
// space and computation time. The targets map maps the desired quantiles to
|
||||
// their absolute errors, i.e. the true quantile of a value returned by a query
|
||||
// is guaranteed to be within (Quantile±Epsilon).
|
||||
//
|
||||
// See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error properties.
|
||||
func NewTargeted(targetMap map[float64]float64) *Stream {
|
||||
// Convert map to slice to avoid slow iterations on a map.
|
||||
// ƒ is called on the hot path, so converting the map to a slice
|
||||
// beforehand results in significant CPU savings.
|
||||
targets := targetMapToSlice(targetMap)
|
||||
|
||||
ƒ := func(s *stream, r float64) float64 {
|
||||
var m = math.MaxFloat64
|
||||
var f float64
|
||||
for _, t := range targets {
|
||||
if t.quantile*s.n <= r {
|
||||
f = (2 * t.epsilon * r) / t.quantile
|
||||
} else {
|
||||
f = (2 * t.epsilon * (s.n - r)) / (1 - t.quantile)
|
||||
}
|
||||
if f < m {
|
||||
m = f
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
return newStream(ƒ)
|
||||
}
|
||||
|
||||
type target struct {
|
||||
quantile float64
|
||||
epsilon float64
|
||||
}
|
||||
|
||||
func targetMapToSlice(targetMap map[float64]float64) []target {
|
||||
targets := make([]target, 0, len(targetMap))
|
||||
|
||||
for quantile, epsilon := range targetMap {
|
||||
t := target{
|
||||
quantile: quantile,
|
||||
epsilon: epsilon,
|
||||
}
|
||||
targets = append(targets, t)
|
||||
}
|
||||
|
||||
return targets
|
||||
}
|
||||
|
||||
// Stream computes quantiles for a stream of float64s. It is not thread-safe by
|
||||
// design. Take care when using across multiple goroutines.
|
||||
type Stream struct {
|
||||
*stream
|
||||
b Samples
|
||||
sorted bool
|
||||
}
|
||||
|
||||
func newStream(ƒ invariant) *Stream {
|
||||
x := &stream{ƒ: ƒ}
|
||||
return &Stream{x, make(Samples, 0, 500), true}
|
||||
}
|
||||
|
||||
// Insert inserts v into the stream.
|
||||
func (s *Stream) Insert(v float64) {
|
||||
s.insert(Sample{Value: v, Width: 1})
|
||||
}
|
||||
|
||||
func (s *Stream) insert(sample Sample) {
|
||||
s.b = append(s.b, sample)
|
||||
s.sorted = false
|
||||
if len(s.b) == cap(s.b) {
|
||||
s.flush()
|
||||
}
|
||||
}
|
||||
|
||||
// Query returns the computed qth percentiles value. If s was created with
|
||||
// NewTargeted, and q is not in the set of quantiles provided a priori, Query
|
||||
// will return an unspecified result.
|
||||
func (s *Stream) Query(q float64) float64 {
|
||||
if !s.flushed() {
|
||||
// Fast path when there hasn't been enough data for a flush;
|
||||
// this also yields better accuracy for small sets of data.
|
||||
l := len(s.b)
|
||||
if l == 0 {
|
||||
return 0
|
||||
}
|
||||
i := int(math.Ceil(float64(l) * q))
|
||||
if i > 0 {
|
||||
i -= 1
|
||||
}
|
||||
s.maybeSort()
|
||||
return s.b[i].Value
|
||||
}
|
||||
s.flush()
|
||||
return s.stream.query(q)
|
||||
}
|
||||
|
||||
// Merge merges samples into the underlying streams samples. This is handy when
|
||||
// merging multiple streams from separate threads, database shards, etc.
|
||||
//
|
||||
// ATTENTION: This method is broken and does not yield correct results. The
|
||||
// underlying algorithm is not capable of merging streams correctly.
|
||||
func (s *Stream) Merge(samples Samples) {
|
||||
sort.Sort(samples)
|
||||
s.stream.merge(samples)
|
||||
}
|
||||
|
||||
// Reset reinitializes and clears the list reusing the samples buffer memory.
|
||||
func (s *Stream) Reset() {
|
||||
s.stream.reset()
|
||||
s.b = s.b[:0]
|
||||
}
|
||||
|
||||
// Samples returns stream samples held by s.
|
||||
func (s *Stream) Samples() Samples {
|
||||
if !s.flushed() {
|
||||
return s.b
|
||||
}
|
||||
s.flush()
|
||||
return s.stream.samples()
|
||||
}
|
||||
|
||||
// Count returns the total number of samples observed in the stream
|
||||
// since initialization.
|
||||
func (s *Stream) Count() int {
|
||||
return len(s.b) + s.stream.count()
|
||||
}
|
||||
|
||||
func (s *Stream) flush() {
|
||||
s.maybeSort()
|
||||
s.stream.merge(s.b)
|
||||
s.b = s.b[:0]
|
||||
}
|
||||
|
||||
func (s *Stream) maybeSort() {
|
||||
if !s.sorted {
|
||||
s.sorted = true
|
||||
sort.Sort(s.b)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stream) flushed() bool {
|
||||
return len(s.stream.l) > 0
|
||||
}
|
||||
|
||||
type stream struct {
|
||||
n float64
|
||||
l []Sample
|
||||
ƒ invariant
|
||||
}
|
||||
|
||||
func (s *stream) reset() {
|
||||
s.l = s.l[:0]
|
||||
s.n = 0
|
||||
}
|
||||
|
||||
func (s *stream) insert(v float64) {
|
||||
s.merge(Samples{{v, 1, 0}})
|
||||
}
|
||||
|
||||
func (s *stream) merge(samples Samples) {
|
||||
// TODO(beorn7): This tries to merge not only individual samples, but
|
||||
// whole summaries. The paper doesn't mention merging summaries at
|
||||
// all. Unittests show that the merging is inaccurate. Find out how to
|
||||
// do merges properly.
|
||||
var r float64
|
||||
i := 0
|
||||
for _, sample := range samples {
|
||||
for ; i < len(s.l); i++ {
|
||||
c := s.l[i]
|
||||
if c.Value > sample.Value {
|
||||
// Insert at position i.
|
||||
s.l = append(s.l, Sample{})
|
||||
copy(s.l[i+1:], s.l[i:])
|
||||
s.l[i] = Sample{
|
||||
sample.Value,
|
||||
sample.Width,
|
||||
math.Max(sample.Delta, math.Floor(s.ƒ(s, r))-1),
|
||||
// TODO(beorn7): How to calculate delta correctly?
|
||||
}
|
||||
i++
|
||||
goto inserted
|
||||
}
|
||||
r += c.Width
|
||||
}
|
||||
s.l = append(s.l, Sample{sample.Value, sample.Width, 0})
|
||||
i++
|
||||
inserted:
|
||||
s.n += sample.Width
|
||||
r += sample.Width
|
||||
}
|
||||
s.compress()
|
||||
}
|
||||
|
||||
func (s *stream) count() int {
|
||||
return int(s.n)
|
||||
}
|
||||
|
||||
func (s *stream) query(q float64) float64 {
|
||||
t := math.Ceil(q * s.n)
|
||||
t += math.Ceil(s.ƒ(s, t) / 2)
|
||||
p := s.l[0]
|
||||
var r float64
|
||||
for _, c := range s.l[1:] {
|
||||
r += p.Width
|
||||
if r+c.Width+c.Delta > t {
|
||||
return p.Value
|
||||
}
|
||||
p = c
|
||||
}
|
||||
return p.Value
|
||||
}
|
||||
|
||||
func (s *stream) compress() {
|
||||
if len(s.l) < 2 {
|
||||
return
|
||||
}
|
||||
x := s.l[len(s.l)-1]
|
||||
xi := len(s.l) - 1
|
||||
r := s.n - 1 - x.Width
|
||||
|
||||
for i := len(s.l) - 2; i >= 0; i-- {
|
||||
c := s.l[i]
|
||||
if c.Width+x.Width+x.Delta <= s.ƒ(s, r) {
|
||||
x.Width += c.Width
|
||||
s.l[xi] = x
|
||||
// Remove element at i.
|
||||
copy(s.l[i:], s.l[i+1:])
|
||||
s.l = s.l[:len(s.l)-1]
|
||||
xi -= 1
|
||||
} else {
|
||||
x = c
|
||||
xi = i
|
||||
}
|
||||
r -= c.Width
|
||||
}
|
||||
}
|
||||
|
||||
func (s *stream) samples() Samples {
|
||||
samples := make(Samples, len(s.l))
|
||||
copy(samples, s.l)
|
||||
return samples
|
||||
}
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
# http://editorconfig.org
|
||||
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*_test.go]
|
||||
trim_trailing_whitespace = false
|
||||
Generated
-7
@@ -1,7 +0,0 @@
|
||||
testdata/conf_out.ini
|
||||
ini.sublime-project
|
||||
ini.sublime-workspace
|
||||
testdata/conf_reflect.ini
|
||||
.idea
|
||||
/.vscode
|
||||
.DS_Store
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
linters-settings:
|
||||
staticcheck:
|
||||
checks: [
|
||||
"all",
|
||||
"-SA1019" # There are valid use cases of strings.Title
|
||||
]
|
||||
nakedret:
|
||||
max-func-lines: 0 # Disallow any unnamed return statement
|
||||
|
||||
linters:
|
||||
enable:
|
||||
- deadcode
|
||||
- errcheck
|
||||
- gosimple
|
||||
- govet
|
||||
- ineffassign
|
||||
- staticcheck
|
||||
- structcheck
|
||||
- typecheck
|
||||
- unused
|
||||
- varcheck
|
||||
- nakedret
|
||||
- gofmt
|
||||
- rowserrcheck
|
||||
- unconvert
|
||||
- goimports
|
||||
- unparam
|
||||
-191
@@ -1,191 +0,0 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and
|
||||
distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
||||
owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities
|
||||
that control, are controlled by, or are under common control with that entity.
|
||||
For the purposes of this definition, "control" means (i) the power, direct or
|
||||
indirect, to cause the direction or management of such entity, whether by
|
||||
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
||||
permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including
|
||||
but not limited to software source code, documentation source, and configuration
|
||||
files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or
|
||||
translation of a Source form, including but not limited to compiled object code,
|
||||
generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made
|
||||
available under the License, as indicated by a copyright notice that is included
|
||||
in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
||||
is based on (or derived from) the Work and for which the editorial revisions,
|
||||
annotations, elaborations, or other modifications represent, as a whole, an
|
||||
original work of authorship. For the purposes of this License, Derivative Works
|
||||
shall not include works that remain separable from, or merely link (or bind by
|
||||
name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version
|
||||
of the Work and any modifications or additions to that Work or Derivative Works
|
||||
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
||||
by the copyright owner or by an individual or Legal Entity authorized to submit
|
||||
on behalf of the copyright owner. For the purposes of this definition,
|
||||
"submitted" means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems, and
|
||||
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
||||
the purpose of discussing and improving the Work, but excluding communication
|
||||
that is conspicuously marked or otherwise designated in writing by the copyright
|
||||
owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
||||
of whom a Contribution has been received by Licensor and subsequently
|
||||
incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License.
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the Work and such
|
||||
Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License.
|
||||
|
||||
Subject to the terms and conditions of this License, each Contributor hereby
|
||||
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||
irrevocable (except as stated in this section) patent license to make, have
|
||||
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
|
||||
such license applies only to those patent claims licensable by such Contributor
|
||||
that are necessarily infringed by their Contribution(s) alone or by combination
|
||||
of their Contribution(s) with the Work to which such Contribution(s) was
|
||||
submitted. If You institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
||||
Contribution incorporated within the Work constitutes direct or contributory
|
||||
patent infringement, then any patent licenses granted to You under this License
|
||||
for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution.
|
||||
|
||||
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
||||
in any medium, with or without modifications, and in Source or Object form,
|
||||
provided that You meet the following conditions:
|
||||
|
||||
You must give any other recipients of the Work or Derivative Works a copy of
|
||||
this License; and
|
||||
You must cause any modified files to carry prominent notices stating that You
|
||||
changed the files; and
|
||||
You must retain, in the Source form of any Derivative Works that You distribute,
|
||||
all copyright, patent, trademark, and attribution notices from the Source form
|
||||
of the Work, excluding those notices that do not pertain to any part of the
|
||||
Derivative Works; and
|
||||
If the Work includes a "NOTICE" text file as part of its distribution, then any
|
||||
Derivative Works that You distribute must include a readable copy of the
|
||||
attribution notices contained within such NOTICE file, excluding those notices
|
||||
that do not pertain to any part of the Derivative Works, in at least one of the
|
||||
following places: within a NOTICE text file distributed as part of the
|
||||
Derivative Works; within the Source form or documentation, if provided along
|
||||
with the Derivative Works; or, within a display generated by the Derivative
|
||||
Works, if and wherever such third-party notices normally appear. The contents of
|
||||
the NOTICE file are for informational purposes only and do not modify the
|
||||
License. You may add Your own attribution notices within Derivative Works that
|
||||
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
||||
provided that such additional attribution notices cannot be construed as
|
||||
modifying the License.
|
||||
You may add Your own copyright statement to Your modifications and may provide
|
||||
additional or different license terms and conditions for use, reproduction, or
|
||||
distribution of Your modifications, or for any such Derivative Works as a whole,
|
||||
provided Your use, reproduction, and distribution of the Work otherwise complies
|
||||
with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions.
|
||||
|
||||
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
||||
for inclusion in the Work by You to the Licensor shall be under the terms and
|
||||
conditions of this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify the terms of
|
||||
any separate license agreement you may have executed with Licensor regarding
|
||||
such Contributions.
|
||||
|
||||
6. Trademarks.
|
||||
|
||||
This License does not grant permission to use the trade names, trademarks,
|
||||
service marks, or product names of the Licensor, except as required for
|
||||
reasonable and customary use in describing the origin of the Work and
|
||||
reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty.
|
||||
|
||||
Unless required by applicable law or agreed to in writing, Licensor provides the
|
||||
Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
||||
including, without limitation, any warranties or conditions of TITLE,
|
||||
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
|
||||
solely responsible for determining the appropriateness of using or
|
||||
redistributing the Work and assume any risks associated with Your exercise of
|
||||
permissions under this License.
|
||||
|
||||
8. Limitation of Liability.
|
||||
|
||||
In no event and under no legal theory, whether in tort (including negligence),
|
||||
contract, or otherwise, unless required by applicable law (such as deliberate
|
||||
and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special, incidental,
|
||||
or consequential damages of any character arising as a result of this License or
|
||||
out of the use or inability to use the Work (including but not limited to
|
||||
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
|
||||
any and all other commercial damages or losses), even if such Contributor has
|
||||
been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability.
|
||||
|
||||
While redistributing the Work or Derivative Works thereof, You may choose to
|
||||
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
||||
other liability obligations and/or rights consistent with this License. However,
|
||||
in accepting such obligations, You may act only on Your own behalf and on Your
|
||||
sole responsibility, not on behalf of any other Contributor, and only if You
|
||||
agree to indemnify, defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason of your
|
||||
accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate
|
||||
notice, with the fields enclosed by brackets "[]" replaced with your own
|
||||
identifying information. (Don't include the brackets!) The text should be
|
||||
enclosed in the appropriate comment syntax for the file format. We also
|
||||
recommend that a file or class name and description of purpose be included on
|
||||
the same "printed page" as the copyright notice for easier identification within
|
||||
third-party archives.
|
||||
|
||||
Copyright 2014 Unknwon
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
.PHONY: build test bench vet coverage
|
||||
|
||||
build: vet bench
|
||||
|
||||
test:
|
||||
go test -v -cover -race
|
||||
|
||||
bench:
|
||||
go test -v -cover -test.bench=. -test.benchmem
|
||||
|
||||
vet:
|
||||
go vet
|
||||
|
||||
coverage:
|
||||
go test -coverprofile=c.out && go tool cover -html=c.out && rm c.out
|
||||
-43
@@ -1,43 +0,0 @@
|
||||
# INI
|
||||
|
||||
[](https://github.com/go-ini/ini/actions?query=branch%3Amain)
|
||||
[](https://codecov.io/gh/go-ini/ini)
|
||||
[](https://pkg.go.dev/github.com/go-ini/ini?tab=doc)
|
||||
[](https://sourcegraph.com/github.com/go-ini/ini)
|
||||
|
||||

|
||||
|
||||
Package ini provides INI file read and write functionality in Go.
|
||||
|
||||
## Features
|
||||
|
||||
- Load from multiple data sources(file, `[]byte`, `io.Reader` and `io.ReadCloser`) with overwrites.
|
||||
- Read with recursion values.
|
||||
- Read with parent-child sections.
|
||||
- Read with auto-increment key names.
|
||||
- Read with multiple-line values.
|
||||
- Read with tons of helper methods.
|
||||
- Read and convert values to Go types.
|
||||
- Read and **WRITE** comments of sections and keys.
|
||||
- Manipulate sections, keys and comments with ease.
|
||||
- Keep sections and keys in order as you parse and save.
|
||||
|
||||
## Installation
|
||||
|
||||
The minimum requirement of Go is **1.13**.
|
||||
|
||||
```sh
|
||||
$ go get gopkg.in/ini.v1
|
||||
```
|
||||
|
||||
Please add `-u` flag to update in the future.
|
||||
|
||||
## Getting Help
|
||||
|
||||
- [Getting Started](https://ini.unknwon.io/docs/intro/getting_started)
|
||||
- [API Documentation](https://gowalker.org/gopkg.in/ini.v1)
|
||||
- 中国大陆镜像:https://ini.unknwon.cn
|
||||
|
||||
## License
|
||||
|
||||
This project is under Apache v2 License. See the [LICENSE](LICENSE) file for the full license text.
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
coverage:
|
||||
range: "60...95"
|
||||
status:
|
||||
project:
|
||||
default:
|
||||
threshold: 1%
|
||||
informational: true
|
||||
patch:
|
||||
defualt:
|
||||
only_pulls: true
|
||||
informational: true
|
||||
|
||||
comment:
|
||||
layout: 'diff'
|
||||
|
||||
github_checks: false
|
||||
-76
@@ -1,76 +0,0 @@
|
||||
// Copyright 2019 Unknwon
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package ini
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
_ dataSource = (*sourceFile)(nil)
|
||||
_ dataSource = (*sourceData)(nil)
|
||||
_ dataSource = (*sourceReadCloser)(nil)
|
||||
)
|
||||
|
||||
// dataSource is an interface that returns object which can be read and closed.
|
||||
type dataSource interface {
|
||||
ReadCloser() (io.ReadCloser, error)
|
||||
}
|
||||
|
||||
// sourceFile represents an object that contains content on the local file system.
|
||||
type sourceFile struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (s sourceFile) ReadCloser() (_ io.ReadCloser, err error) {
|
||||
return os.Open(s.name)
|
||||
}
|
||||
|
||||
// sourceData represents an object that contains content in memory.
|
||||
type sourceData struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
func (s *sourceData) ReadCloser() (io.ReadCloser, error) {
|
||||
return ioutil.NopCloser(bytes.NewReader(s.data)), nil
|
||||
}
|
||||
|
||||
// sourceReadCloser represents an input stream with Close method.
|
||||
type sourceReadCloser struct {
|
||||
reader io.ReadCloser
|
||||
}
|
||||
|
||||
func (s *sourceReadCloser) ReadCloser() (io.ReadCloser, error) {
|
||||
return s.reader, nil
|
||||
}
|
||||
|
||||
func parseDataSource(source interface{}) (dataSource, error) {
|
||||
switch s := source.(type) {
|
||||
case string:
|
||||
return sourceFile{s}, nil
|
||||
case []byte:
|
||||
return &sourceData{s}, nil
|
||||
case io.ReadCloser:
|
||||
return &sourceReadCloser{s}, nil
|
||||
case io.Reader:
|
||||
return &sourceReadCloser{ioutil.NopCloser(s)}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("error parsing data source: unknown type %q", s)
|
||||
}
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
// Copyright 2019 Unknwon
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package ini
|
||||
|
||||
var (
|
||||
// Deprecated: Use "DefaultSection" instead.
|
||||
DEFAULT_SECTION = DefaultSection
|
||||
// Deprecated: AllCapsUnderscore converts to format ALL_CAPS_UNDERSCORE.
|
||||
AllCapsUnderscore = SnackCase
|
||||
)
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
// Copyright 2016 Unknwon
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package ini
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ErrDelimiterNotFound indicates the error type of no delimiter is found which there should be one.
|
||||
type ErrDelimiterNotFound struct {
|
||||
Line string
|
||||
}
|
||||
|
||||
// IsErrDelimiterNotFound returns true if the given error is an instance of ErrDelimiterNotFound.
|
||||
func IsErrDelimiterNotFound(err error) bool {
|
||||
_, ok := err.(ErrDelimiterNotFound)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrDelimiterNotFound) Error() string {
|
||||
return fmt.Sprintf("key-value delimiter not found: %s", err.Line)
|
||||
}
|
||||
|
||||
// ErrEmptyKeyName indicates the error type of no key name is found which there should be one.
|
||||
type ErrEmptyKeyName struct {
|
||||
Line string
|
||||
}
|
||||
|
||||
// IsErrEmptyKeyName returns true if the given error is an instance of ErrEmptyKeyName.
|
||||
func IsErrEmptyKeyName(err error) bool {
|
||||
_, ok := err.(ErrEmptyKeyName)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrEmptyKeyName) Error() string {
|
||||
return fmt.Sprintf("empty key name: %s", err.Line)
|
||||
}
|
||||
-541
@@ -1,541 +0,0 @@
|
||||
// Copyright 2017 Unknwon
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package ini
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// File represents a combination of one or more INI files in memory.
|
||||
type File struct {
|
||||
options LoadOptions
|
||||
dataSources []dataSource
|
||||
|
||||
// Should make things safe, but sometimes doesn't matter.
|
||||
BlockMode bool
|
||||
lock sync.RWMutex
|
||||
|
||||
// To keep data in order.
|
||||
sectionList []string
|
||||
// To keep track of the index of a section with same name.
|
||||
// This meta list is only used with non-unique section names are allowed.
|
||||
sectionIndexes []int
|
||||
|
||||
// Actual data is stored here.
|
||||
sections map[string][]*Section
|
||||
|
||||
NameMapper
|
||||
ValueMapper
|
||||
}
|
||||
|
||||
// newFile initializes File object with given data sources.
|
||||
func newFile(dataSources []dataSource, opts LoadOptions) *File {
|
||||
if len(opts.KeyValueDelimiters) == 0 {
|
||||
opts.KeyValueDelimiters = "=:"
|
||||
}
|
||||
if len(opts.KeyValueDelimiterOnWrite) == 0 {
|
||||
opts.KeyValueDelimiterOnWrite = "="
|
||||
}
|
||||
if len(opts.ChildSectionDelimiter) == 0 {
|
||||
opts.ChildSectionDelimiter = "."
|
||||
}
|
||||
|
||||
return &File{
|
||||
BlockMode: true,
|
||||
dataSources: dataSources,
|
||||
sections: make(map[string][]*Section),
|
||||
options: opts,
|
||||
}
|
||||
}
|
||||
|
||||
// Empty returns an empty file object.
|
||||
func Empty(opts ...LoadOptions) *File {
|
||||
var opt LoadOptions
|
||||
if len(opts) > 0 {
|
||||
opt = opts[0]
|
||||
}
|
||||
|
||||
// Ignore error here, we are sure our data is good.
|
||||
f, _ := LoadSources(opt, []byte(""))
|
||||
return f
|
||||
}
|
||||
|
||||
// NewSection creates a new section.
|
||||
func (f *File) NewSection(name string) (*Section, error) {
|
||||
if len(name) == 0 {
|
||||
return nil, errors.New("empty section name")
|
||||
}
|
||||
|
||||
if (f.options.Insensitive || f.options.InsensitiveSections) && name != DefaultSection {
|
||||
name = strings.ToLower(name)
|
||||
}
|
||||
|
||||
if f.BlockMode {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
}
|
||||
|
||||
if !f.options.AllowNonUniqueSections && inSlice(name, f.sectionList) {
|
||||
return f.sections[name][0], nil
|
||||
}
|
||||
|
||||
f.sectionList = append(f.sectionList, name)
|
||||
|
||||
// NOTE: Append to indexes must happen before appending to sections,
|
||||
// otherwise index will have off-by-one problem.
|
||||
f.sectionIndexes = append(f.sectionIndexes, len(f.sections[name]))
|
||||
|
||||
sec := newSection(f, name)
|
||||
f.sections[name] = append(f.sections[name], sec)
|
||||
|
||||
return sec, nil
|
||||
}
|
||||
|
||||
// NewRawSection creates a new section with an unparseable body.
|
||||
func (f *File) NewRawSection(name, body string) (*Section, error) {
|
||||
section, err := f.NewSection(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
section.isRawSection = true
|
||||
section.rawBody = body
|
||||
return section, nil
|
||||
}
|
||||
|
||||
// NewSections creates a list of sections.
|
||||
func (f *File) NewSections(names ...string) (err error) {
|
||||
for _, name := range names {
|
||||
if _, err = f.NewSection(name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSection returns section by given name.
|
||||
func (f *File) GetSection(name string) (*Section, error) {
|
||||
secs, err := f.SectionsByName(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return secs[0], err
|
||||
}
|
||||
|
||||
// HasSection returns true if the file contains a section with given name.
|
||||
func (f *File) HasSection(name string) bool {
|
||||
section, _ := f.GetSection(name)
|
||||
return section != nil
|
||||
}
|
||||
|
||||
// SectionsByName returns all sections with given name.
|
||||
func (f *File) SectionsByName(name string) ([]*Section, error) {
|
||||
if len(name) == 0 {
|
||||
name = DefaultSection
|
||||
}
|
||||
if f.options.Insensitive || f.options.InsensitiveSections {
|
||||
name = strings.ToLower(name)
|
||||
}
|
||||
|
||||
if f.BlockMode {
|
||||
f.lock.RLock()
|
||||
defer f.lock.RUnlock()
|
||||
}
|
||||
|
||||
secs := f.sections[name]
|
||||
if len(secs) == 0 {
|
||||
return nil, fmt.Errorf("section %q does not exist", name)
|
||||
}
|
||||
|
||||
return secs, nil
|
||||
}
|
||||
|
||||
// Section assumes named section exists and returns a zero-value when not.
|
||||
func (f *File) Section(name string) *Section {
|
||||
sec, err := f.GetSection(name)
|
||||
if err != nil {
|
||||
if name == "" {
|
||||
name = DefaultSection
|
||||
}
|
||||
sec, _ = f.NewSection(name)
|
||||
return sec
|
||||
}
|
||||
return sec
|
||||
}
|
||||
|
||||
// SectionWithIndex assumes named section exists and returns a new section when not.
|
||||
func (f *File) SectionWithIndex(name string, index int) *Section {
|
||||
secs, err := f.SectionsByName(name)
|
||||
if err != nil || len(secs) <= index {
|
||||
// NOTE: It's OK here because the only possible error is empty section name,
|
||||
// but if it's empty, this piece of code won't be executed.
|
||||
newSec, _ := f.NewSection(name)
|
||||
return newSec
|
||||
}
|
||||
|
||||
return secs[index]
|
||||
}
|
||||
|
||||
// Sections returns a list of Section stored in the current instance.
|
||||
func (f *File) Sections() []*Section {
|
||||
if f.BlockMode {
|
||||
f.lock.RLock()
|
||||
defer f.lock.RUnlock()
|
||||
}
|
||||
|
||||
sections := make([]*Section, len(f.sectionList))
|
||||
for i, name := range f.sectionList {
|
||||
sections[i] = f.sections[name][f.sectionIndexes[i]]
|
||||
}
|
||||
return sections
|
||||
}
|
||||
|
||||
// ChildSections returns a list of child sections of given section name.
|
||||
func (f *File) ChildSections(name string) []*Section {
|
||||
return f.Section(name).ChildSections()
|
||||
}
|
||||
|
||||
// SectionStrings returns list of section names.
|
||||
func (f *File) SectionStrings() []string {
|
||||
list := make([]string, len(f.sectionList))
|
||||
copy(list, f.sectionList)
|
||||
return list
|
||||
}
|
||||
|
||||
// DeleteSection deletes a section or all sections with given name.
|
||||
func (f *File) DeleteSection(name string) {
|
||||
secs, err := f.SectionsByName(name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < len(secs); i++ {
|
||||
// For non-unique sections, it is always needed to remove the first one so
|
||||
// in the next iteration, the subsequent section continue having index 0.
|
||||
// Ignoring the error as index 0 never returns an error.
|
||||
_ = f.DeleteSectionWithIndex(name, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteSectionWithIndex deletes a section with given name and index.
|
||||
func (f *File) DeleteSectionWithIndex(name string, index int) error {
|
||||
if !f.options.AllowNonUniqueSections && index != 0 {
|
||||
return fmt.Errorf("delete section with non-zero index is only allowed when non-unique sections is enabled")
|
||||
}
|
||||
|
||||
if len(name) == 0 {
|
||||
name = DefaultSection
|
||||
}
|
||||
if f.options.Insensitive || f.options.InsensitiveSections {
|
||||
name = strings.ToLower(name)
|
||||
}
|
||||
|
||||
if f.BlockMode {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
}
|
||||
|
||||
// Count occurrences of the sections
|
||||
occurrences := 0
|
||||
|
||||
sectionListCopy := make([]string, len(f.sectionList))
|
||||
copy(sectionListCopy, f.sectionList)
|
||||
|
||||
for i, s := range sectionListCopy {
|
||||
if s != name {
|
||||
continue
|
||||
}
|
||||
|
||||
if occurrences == index {
|
||||
if len(f.sections[name]) <= 1 {
|
||||
delete(f.sections, name) // The last one in the map
|
||||
} else {
|
||||
f.sections[name] = append(f.sections[name][:index], f.sections[name][index+1:]...)
|
||||
}
|
||||
|
||||
// Fix section lists
|
||||
f.sectionList = append(f.sectionList[:i], f.sectionList[i+1:]...)
|
||||
f.sectionIndexes = append(f.sectionIndexes[:i], f.sectionIndexes[i+1:]...)
|
||||
|
||||
} else if occurrences > index {
|
||||
// Fix the indices of all following sections with this name.
|
||||
f.sectionIndexes[i-1]--
|
||||
}
|
||||
|
||||
occurrences++
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *File) reload(s dataSource) error {
|
||||
r, err := s.ReadCloser()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
return f.parse(r)
|
||||
}
|
||||
|
||||
// Reload reloads and parses all data sources.
|
||||
func (f *File) Reload() (err error) {
|
||||
for _, s := range f.dataSources {
|
||||
if err = f.reload(s); err != nil {
|
||||
// In loose mode, we create an empty default section for nonexistent files.
|
||||
if os.IsNotExist(err) && f.options.Loose {
|
||||
_ = f.parse(bytes.NewBuffer(nil))
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
if f.options.ShortCircuit {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Append appends one or more data sources and reloads automatically.
|
||||
func (f *File) Append(source interface{}, others ...interface{}) error {
|
||||
ds, err := parseDataSource(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.dataSources = append(f.dataSources, ds)
|
||||
for _, s := range others {
|
||||
ds, err = parseDataSource(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.dataSources = append(f.dataSources, ds)
|
||||
}
|
||||
return f.Reload()
|
||||
}
|
||||
|
||||
func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
|
||||
equalSign := DefaultFormatLeft + f.options.KeyValueDelimiterOnWrite + DefaultFormatRight
|
||||
|
||||
if PrettyFormat || PrettyEqual {
|
||||
equalSign = fmt.Sprintf(" %s ", f.options.KeyValueDelimiterOnWrite)
|
||||
}
|
||||
|
||||
// Use buffer to make sure target is safe until finish encoding.
|
||||
buf := bytes.NewBuffer(nil)
|
||||
lastSectionIdx := len(f.sectionList) - 1
|
||||
for i, sname := range f.sectionList {
|
||||
sec := f.SectionWithIndex(sname, f.sectionIndexes[i])
|
||||
if len(sec.Comment) > 0 {
|
||||
// Support multiline comments
|
||||
lines := strings.Split(sec.Comment, LineBreak)
|
||||
for i := range lines {
|
||||
if lines[i][0] != '#' && lines[i][0] != ';' {
|
||||
lines[i] = "; " + lines[i]
|
||||
} else {
|
||||
lines[i] = lines[i][:1] + " " + strings.TrimSpace(lines[i][1:])
|
||||
}
|
||||
|
||||
if _, err := buf.WriteString(lines[i] + LineBreak); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if i > 0 || DefaultHeader || (i == 0 && strings.ToUpper(sec.name) != DefaultSection) {
|
||||
if _, err := buf.WriteString("[" + sname + "]" + LineBreak); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// Write nothing if default section is empty
|
||||
if len(sec.keyList) == 0 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
isLastSection := i == lastSectionIdx
|
||||
if sec.isRawSection {
|
||||
if _, err := buf.WriteString(sec.rawBody); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if PrettySection && !isLastSection {
|
||||
// Put a line between sections
|
||||
if _, err := buf.WriteString(LineBreak); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Count and generate alignment length and buffer spaces using the
|
||||
// longest key. Keys may be modified if they contain certain characters so
|
||||
// we need to take that into account in our calculation.
|
||||
alignLength := 0
|
||||
if PrettyFormat {
|
||||
for _, kname := range sec.keyList {
|
||||
keyLength := len(kname)
|
||||
// First case will surround key by ` and second by """
|
||||
if strings.Contains(kname, "\"") || strings.ContainsAny(kname, f.options.KeyValueDelimiters) {
|
||||
keyLength += 2
|
||||
} else if strings.Contains(kname, "`") {
|
||||
keyLength += 6
|
||||
}
|
||||
|
||||
if keyLength > alignLength {
|
||||
alignLength = keyLength
|
||||
}
|
||||
}
|
||||
}
|
||||
alignSpaces := bytes.Repeat([]byte(" "), alignLength)
|
||||
|
||||
KeyList:
|
||||
for _, kname := range sec.keyList {
|
||||
key := sec.Key(kname)
|
||||
if len(key.Comment) > 0 {
|
||||
if len(indent) > 0 && sname != DefaultSection {
|
||||
buf.WriteString(indent)
|
||||
}
|
||||
|
||||
// Support multiline comments
|
||||
lines := strings.Split(key.Comment, LineBreak)
|
||||
for i := range lines {
|
||||
if lines[i][0] != '#' && lines[i][0] != ';' {
|
||||
lines[i] = "; " + strings.TrimSpace(lines[i])
|
||||
} else {
|
||||
lines[i] = lines[i][:1] + " " + strings.TrimSpace(lines[i][1:])
|
||||
}
|
||||
|
||||
if _, err := buf.WriteString(lines[i] + LineBreak); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(indent) > 0 && sname != DefaultSection {
|
||||
buf.WriteString(indent)
|
||||
}
|
||||
|
||||
switch {
|
||||
case key.isAutoIncrement:
|
||||
kname = "-"
|
||||
case strings.Contains(kname, "\"") || strings.ContainsAny(kname, f.options.KeyValueDelimiters):
|
||||
kname = "`" + kname + "`"
|
||||
case strings.Contains(kname, "`"):
|
||||
kname = `"""` + kname + `"""`
|
||||
}
|
||||
|
||||
writeKeyValue := func(val string) (bool, error) {
|
||||
if _, err := buf.WriteString(kname); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if key.isBooleanType {
|
||||
buf.WriteString(LineBreak)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Write out alignment spaces before "=" sign
|
||||
if PrettyFormat {
|
||||
buf.Write(alignSpaces[:alignLength-len(kname)])
|
||||
}
|
||||
|
||||
// In case key value contains "\n", "`", "\"", "#" or ";"
|
||||
if strings.ContainsAny(val, "\n`") {
|
||||
val = `"""` + val + `"""`
|
||||
} else if !f.options.IgnoreInlineComment && strings.ContainsAny(val, "#;") {
|
||||
val = "`" + val + "`"
|
||||
} else if len(strings.TrimSpace(val)) != len(val) {
|
||||
val = `"` + val + `"`
|
||||
}
|
||||
if _, err := buf.WriteString(equalSign + val + LineBreak); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
shadows := key.ValueWithShadows()
|
||||
if len(shadows) == 0 {
|
||||
if _, err := writeKeyValue(""); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
for _, val := range shadows {
|
||||
exitLoop, err := writeKeyValue(val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if exitLoop {
|
||||
continue KeyList
|
||||
}
|
||||
}
|
||||
|
||||
for _, val := range key.nestedValues {
|
||||
if _, err := buf.WriteString(indent + " " + val + LineBreak); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if PrettySection && !isLastSection {
|
||||
// Put a line between sections
|
||||
if _, err := buf.WriteString(LineBreak); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
// WriteToIndent writes content into io.Writer with given indention.
|
||||
// If PrettyFormat has been set to be true,
|
||||
// it will align "=" sign with spaces under each section.
|
||||
func (f *File) WriteToIndent(w io.Writer, indent string) (int64, error) {
|
||||
buf, err := f.writeToBuffer(indent)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return buf.WriteTo(w)
|
||||
}
|
||||
|
||||
// WriteTo writes file content into io.Writer.
|
||||
func (f *File) WriteTo(w io.Writer) (int64, error) {
|
||||
return f.WriteToIndent(w, "")
|
||||
}
|
||||
|
||||
// SaveToIndent writes content to file system with given value indention.
|
||||
func (f *File) SaveToIndent(filename, indent string) error {
|
||||
// Note: Because we are truncating with os.Create,
|
||||
// so it's safer to save to a temporary file location and rename after done.
|
||||
buf, err := f.writeToBuffer(indent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(filename, buf.Bytes(), 0666)
|
||||
}
|
||||
|
||||
// SaveTo writes content to file system.
|
||||
func (f *File) SaveTo(filename string) error {
|
||||
return f.SaveToIndent(filename, "")
|
||||
}
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
// Copyright 2019 Unknwon
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package ini
|
||||
|
||||
func inSlice(str string, s []string) bool {
|
||||
for _, v := range s {
|
||||
if str == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
-176
@@ -1,176 +0,0 @@
|
||||
// Copyright 2014 Unknwon
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
// Package ini provides INI file read and write functionality in Go.
|
||||
package ini
|
||||
|
||||
import (
|
||||
"os"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// Maximum allowed depth when recursively substituing variable names.
|
||||
depthValues = 99
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultSection is the name of default section. You can use this var or the string literal.
|
||||
// In most of cases, an empty string is all you need to access the section.
|
||||
DefaultSection = "DEFAULT"
|
||||
|
||||
// LineBreak is the delimiter to determine or compose a new line.
|
||||
// This variable will be changed to "\r\n" automatically on Windows at package init time.
|
||||
LineBreak = "\n"
|
||||
|
||||
// Variable regexp pattern: %(variable)s
|
||||
varPattern = regexp.MustCompile(`%\(([^)]+)\)s`)
|
||||
|
||||
// DefaultHeader explicitly writes default section header.
|
||||
DefaultHeader = false
|
||||
|
||||
// PrettySection indicates whether to put a line between sections.
|
||||
PrettySection = true
|
||||
// PrettyFormat indicates whether to align "=" sign with spaces to produce pretty output
|
||||
// or reduce all possible spaces for compact format.
|
||||
PrettyFormat = true
|
||||
// PrettyEqual places spaces around "=" sign even when PrettyFormat is false.
|
||||
PrettyEqual = false
|
||||
// DefaultFormatLeft places custom spaces on the left when PrettyFormat and PrettyEqual are both disabled.
|
||||
DefaultFormatLeft = ""
|
||||
// DefaultFormatRight places custom spaces on the right when PrettyFormat and PrettyEqual are both disabled.
|
||||
DefaultFormatRight = ""
|
||||
)
|
||||
|
||||
var inTest = len(os.Args) > 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test")
|
||||
|
||||
func init() {
|
||||
if runtime.GOOS == "windows" && !inTest {
|
||||
LineBreak = "\r\n"
|
||||
}
|
||||
}
|
||||
|
||||
// LoadOptions contains all customized options used for load data source(s).
|
||||
type LoadOptions struct {
|
||||
// Loose indicates whether the parser should ignore nonexistent files or return error.
|
||||
Loose bool
|
||||
// Insensitive indicates whether the parser forces all section and key names to lowercase.
|
||||
Insensitive bool
|
||||
// InsensitiveSections indicates whether the parser forces all section to lowercase.
|
||||
InsensitiveSections bool
|
||||
// InsensitiveKeys indicates whether the parser forces all key names to lowercase.
|
||||
InsensitiveKeys bool
|
||||
// IgnoreContinuation indicates whether to ignore continuation lines while parsing.
|
||||
IgnoreContinuation bool
|
||||
// IgnoreInlineComment indicates whether to ignore comments at the end of value and treat it as part of value.
|
||||
IgnoreInlineComment bool
|
||||
// SkipUnrecognizableLines indicates whether to skip unrecognizable lines that do not conform to key/value pairs.
|
||||
SkipUnrecognizableLines bool
|
||||
// ShortCircuit indicates whether to ignore other configuration sources after loaded the first available configuration source.
|
||||
ShortCircuit bool
|
||||
// AllowBooleanKeys indicates whether to allow boolean type keys or treat as value is missing.
|
||||
// This type of keys are mostly used in my.cnf.
|
||||
AllowBooleanKeys bool
|
||||
// AllowShadows indicates whether to keep track of keys with same name under same section.
|
||||
AllowShadows bool
|
||||
// AllowNestedValues indicates whether to allow AWS-like nested values.
|
||||
// Docs: http://docs.aws.amazon.com/cli/latest/topic/config-vars.html#nested-values
|
||||
AllowNestedValues bool
|
||||
// AllowPythonMultilineValues indicates whether to allow Python-like multi-line values.
|
||||
// Docs: https://docs.python.org/3/library/configparser.html#supported-ini-file-structure
|
||||
// Relevant quote: Values can also span multiple lines, as long as they are indented deeper
|
||||
// than the first line of the value.
|
||||
AllowPythonMultilineValues bool
|
||||
// SpaceBeforeInlineComment indicates whether to allow comment symbols (\# and \;) inside value.
|
||||
// Docs: https://docs.python.org/2/library/configparser.html
|
||||
// Quote: Comments may appear on their own in an otherwise empty line, or may be entered in lines holding values or section names.
|
||||
// In the latter case, they need to be preceded by a whitespace character to be recognized as a comment.
|
||||
SpaceBeforeInlineComment bool
|
||||
// UnescapeValueDoubleQuotes indicates whether to unescape double quotes inside value to regular format
|
||||
// when value is surrounded by double quotes, e.g. key="a \"value\"" => key=a "value"
|
||||
UnescapeValueDoubleQuotes bool
|
||||
// UnescapeValueCommentSymbols indicates to unescape comment symbols (\# and \;) inside value to regular format
|
||||
// when value is NOT surrounded by any quotes.
|
||||
// Note: UNSTABLE, behavior might change to only unescape inside double quotes but may noy necessary at all.
|
||||
UnescapeValueCommentSymbols bool
|
||||
// UnparseableSections stores a list of blocks that are allowed with raw content which do not otherwise
|
||||
// conform to key/value pairs. Specify the names of those blocks here.
|
||||
UnparseableSections []string
|
||||
// KeyValueDelimiters is the sequence of delimiters that are used to separate key and value. By default, it is "=:".
|
||||
KeyValueDelimiters string
|
||||
// KeyValueDelimiterOnWrite is the delimiter that are used to separate key and value output. By default, it is "=".
|
||||
KeyValueDelimiterOnWrite string
|
||||
// ChildSectionDelimiter is the delimiter that is used to separate child sections. By default, it is ".".
|
||||
ChildSectionDelimiter string
|
||||
// PreserveSurroundedQuote indicates whether to preserve surrounded quote (single and double quotes).
|
||||
PreserveSurroundedQuote bool
|
||||
// DebugFunc is called to collect debug information (currently only useful to debug parsing Python-style multiline values).
|
||||
DebugFunc DebugFunc
|
||||
// ReaderBufferSize is the buffer size of the reader in bytes.
|
||||
ReaderBufferSize int
|
||||
// AllowNonUniqueSections indicates whether to allow sections with the same name multiple times.
|
||||
AllowNonUniqueSections bool
|
||||
// AllowDuplicateShadowValues indicates whether values for shadowed keys should be deduplicated.
|
||||
AllowDuplicateShadowValues bool
|
||||
}
|
||||
|
||||
// DebugFunc is the type of function called to log parse events.
|
||||
type DebugFunc func(message string)
|
||||
|
||||
// LoadSources allows caller to apply customized options for loading from data source(s).
|
||||
func LoadSources(opts LoadOptions, source interface{}, others ...interface{}) (_ *File, err error) {
|
||||
sources := make([]dataSource, len(others)+1)
|
||||
sources[0], err = parseDataSource(source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range others {
|
||||
sources[i+1], err = parseDataSource(others[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
f := newFile(sources, opts)
|
||||
if err = f.Reload(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// Load loads and parses from INI data sources.
|
||||
// Arguments can be mixed of file name with string type, or raw data in []byte.
|
||||
// It will return error if list contains nonexistent files.
|
||||
func Load(source interface{}, others ...interface{}) (*File, error) {
|
||||
return LoadSources(LoadOptions{}, source, others...)
|
||||
}
|
||||
|
||||
// LooseLoad has exactly same functionality as Load function
|
||||
// except it ignores nonexistent files instead of returning error.
|
||||
func LooseLoad(source interface{}, others ...interface{}) (*File, error) {
|
||||
return LoadSources(LoadOptions{Loose: true}, source, others...)
|
||||
}
|
||||
|
||||
// InsensitiveLoad has exactly same functionality as Load function
|
||||
// except it forces all section and key names to be lowercased.
|
||||
func InsensitiveLoad(source interface{}, others ...interface{}) (*File, error) {
|
||||
return LoadSources(LoadOptions{Insensitive: true}, source, others...)
|
||||
}
|
||||
|
||||
// ShadowLoad has exactly same functionality as Load function
|
||||
// except it allows have shadow keys.
|
||||
func ShadowLoad(source interface{}, others ...interface{}) (*File, error) {
|
||||
return LoadSources(LoadOptions{AllowShadows: true}, source, others...)
|
||||
}
|
||||
-837
@@ -1,837 +0,0 @@
|
||||
// Copyright 2014 Unknwon
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package ini
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Key represents a key under a section.
|
||||
type Key struct {
|
||||
s *Section
|
||||
Comment string
|
||||
name string
|
||||
value string
|
||||
isAutoIncrement bool
|
||||
isBooleanType bool
|
||||
|
||||
isShadow bool
|
||||
shadows []*Key
|
||||
|
||||
nestedValues []string
|
||||
}
|
||||
|
||||
// newKey simply return a key object with given values.
|
||||
func newKey(s *Section, name, val string) *Key {
|
||||
return &Key{
|
||||
s: s,
|
||||
name: name,
|
||||
value: val,
|
||||
}
|
||||
}
|
||||
|
||||
func (k *Key) addShadow(val string) error {
|
||||
if k.isShadow {
|
||||
return errors.New("cannot add shadow to another shadow key")
|
||||
} else if k.isAutoIncrement || k.isBooleanType {
|
||||
return errors.New("cannot add shadow to auto-increment or boolean key")
|
||||
}
|
||||
|
||||
if !k.s.f.options.AllowDuplicateShadowValues {
|
||||
// Deduplicate shadows based on their values.
|
||||
if k.value == val {
|
||||
return nil
|
||||
}
|
||||
for i := range k.shadows {
|
||||
if k.shadows[i].value == val {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shadow := newKey(k.s, k.name, val)
|
||||
shadow.isShadow = true
|
||||
k.shadows = append(k.shadows, shadow)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddShadow adds a new shadow key to itself.
|
||||
func (k *Key) AddShadow(val string) error {
|
||||
if !k.s.f.options.AllowShadows {
|
||||
return errors.New("shadow key is not allowed")
|
||||
}
|
||||
return k.addShadow(val)
|
||||
}
|
||||
|
||||
func (k *Key) addNestedValue(val string) error {
|
||||
if k.isAutoIncrement || k.isBooleanType {
|
||||
return errors.New("cannot add nested value to auto-increment or boolean key")
|
||||
}
|
||||
|
||||
k.nestedValues = append(k.nestedValues, val)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddNestedValue adds a nested value to the key.
|
||||
func (k *Key) AddNestedValue(val string) error {
|
||||
if !k.s.f.options.AllowNestedValues {
|
||||
return errors.New("nested value is not allowed")
|
||||
}
|
||||
return k.addNestedValue(val)
|
||||
}
|
||||
|
||||
// ValueMapper represents a mapping function for values, e.g. os.ExpandEnv
|
||||
type ValueMapper func(string) string
|
||||
|
||||
// Name returns name of key.
|
||||
func (k *Key) Name() string {
|
||||
return k.name
|
||||
}
|
||||
|
||||
// Value returns raw value of key for performance purpose.
|
||||
func (k *Key) Value() string {
|
||||
return k.value
|
||||
}
|
||||
|
||||
// ValueWithShadows returns raw values of key and its shadows if any. Shadow
|
||||
// keys with empty values are ignored from the returned list.
|
||||
func (k *Key) ValueWithShadows() []string {
|
||||
if len(k.shadows) == 0 {
|
||||
if k.value == "" {
|
||||
return []string{}
|
||||
}
|
||||
return []string{k.value}
|
||||
}
|
||||
|
||||
vals := make([]string, 0, len(k.shadows)+1)
|
||||
if k.value != "" {
|
||||
vals = append(vals, k.value)
|
||||
}
|
||||
for _, s := range k.shadows {
|
||||
if s.value != "" {
|
||||
vals = append(vals, s.value)
|
||||
}
|
||||
}
|
||||
return vals
|
||||
}
|
||||
|
||||
// NestedValues returns nested values stored in the key.
|
||||
// It is possible returned value is nil if no nested values stored in the key.
|
||||
func (k *Key) NestedValues() []string {
|
||||
return k.nestedValues
|
||||
}
|
||||
|
||||
// transformValue takes a raw value and transforms to its final string.
|
||||
func (k *Key) transformValue(val string) string {
|
||||
if k.s.f.ValueMapper != nil {
|
||||
val = k.s.f.ValueMapper(val)
|
||||
}
|
||||
|
||||
// Fail-fast if no indicate char found for recursive value
|
||||
if !strings.Contains(val, "%") {
|
||||
return val
|
||||
}
|
||||
for i := 0; i < depthValues; i++ {
|
||||
vr := varPattern.FindString(val)
|
||||
if len(vr) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
// Take off leading '%(' and trailing ')s'.
|
||||
noption := vr[2 : len(vr)-2]
|
||||
|
||||
// Search in the same section.
|
||||
// If not found or found the key itself, then search again in default section.
|
||||
nk, err := k.s.GetKey(noption)
|
||||
if err != nil || k == nk {
|
||||
nk, _ = k.s.f.Section("").GetKey(noption)
|
||||
if nk == nil {
|
||||
// Stop when no results found in the default section,
|
||||
// and returns the value as-is.
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Substitute by new value and take off leading '%(' and trailing ')s'.
|
||||
val = strings.Replace(val, vr, nk.value, -1)
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// String returns string representation of value.
|
||||
func (k *Key) String() string {
|
||||
return k.transformValue(k.value)
|
||||
}
|
||||
|
||||
// Validate accepts a validate function which can
|
||||
// return modifed result as key value.
|
||||
func (k *Key) Validate(fn func(string) string) string {
|
||||
return fn(k.String())
|
||||
}
|
||||
|
||||
// parseBool returns the boolean value represented by the string.
|
||||
//
|
||||
// It accepts 1, t, T, TRUE, true, True, YES, yes, Yes, y, ON, on, On,
|
||||
// 0, f, F, FALSE, false, False, NO, no, No, n, OFF, off, Off.
|
||||
// Any other value returns an error.
|
||||
func parseBool(str string) (value bool, err error) {
|
||||
switch str {
|
||||
case "1", "t", "T", "true", "TRUE", "True", "YES", "yes", "Yes", "y", "ON", "on", "On":
|
||||
return true, nil
|
||||
case "0", "f", "F", "false", "FALSE", "False", "NO", "no", "No", "n", "OFF", "off", "Off":
|
||||
return false, nil
|
||||
}
|
||||
return false, fmt.Errorf("parsing \"%s\": invalid syntax", str)
|
||||
}
|
||||
|
||||
// Bool returns bool type value.
|
||||
func (k *Key) Bool() (bool, error) {
|
||||
return parseBool(k.String())
|
||||
}
|
||||
|
||||
// Float64 returns float64 type value.
|
||||
func (k *Key) Float64() (float64, error) {
|
||||
return strconv.ParseFloat(k.String(), 64)
|
||||
}
|
||||
|
||||
// Int returns int type value.
|
||||
func (k *Key) Int() (int, error) {
|
||||
v, err := strconv.ParseInt(k.String(), 0, 64)
|
||||
return int(v), err
|
||||
}
|
||||
|
||||
// Int64 returns int64 type value.
|
||||
func (k *Key) Int64() (int64, error) {
|
||||
return strconv.ParseInt(k.String(), 0, 64)
|
||||
}
|
||||
|
||||
// Uint returns uint type valued.
|
||||
func (k *Key) Uint() (uint, error) {
|
||||
u, e := strconv.ParseUint(k.String(), 0, 64)
|
||||
return uint(u), e
|
||||
}
|
||||
|
||||
// Uint64 returns uint64 type value.
|
||||
func (k *Key) Uint64() (uint64, error) {
|
||||
return strconv.ParseUint(k.String(), 0, 64)
|
||||
}
|
||||
|
||||
// Duration returns time.Duration type value.
|
||||
func (k *Key) Duration() (time.Duration, error) {
|
||||
return time.ParseDuration(k.String())
|
||||
}
|
||||
|
||||
// TimeFormat parses with given format and returns time.Time type value.
|
||||
func (k *Key) TimeFormat(format string) (time.Time, error) {
|
||||
return time.Parse(format, k.String())
|
||||
}
|
||||
|
||||
// Time parses with RFC3339 format and returns time.Time type value.
|
||||
func (k *Key) Time() (time.Time, error) {
|
||||
return k.TimeFormat(time.RFC3339)
|
||||
}
|
||||
|
||||
// MustString returns default value if key value is empty.
|
||||
func (k *Key) MustString(defaultVal string) string {
|
||||
val := k.String()
|
||||
if len(val) == 0 {
|
||||
k.value = defaultVal
|
||||
return defaultVal
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// MustBool always returns value without error,
|
||||
// it returns false if error occurs.
|
||||
func (k *Key) MustBool(defaultVal ...bool) bool {
|
||||
val, err := k.Bool()
|
||||
if len(defaultVal) > 0 && err != nil {
|
||||
k.value = strconv.FormatBool(defaultVal[0])
|
||||
return defaultVal[0]
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// MustFloat64 always returns value without error,
|
||||
// it returns 0.0 if error occurs.
|
||||
func (k *Key) MustFloat64(defaultVal ...float64) float64 {
|
||||
val, err := k.Float64()
|
||||
if len(defaultVal) > 0 && err != nil {
|
||||
k.value = strconv.FormatFloat(defaultVal[0], 'f', -1, 64)
|
||||
return defaultVal[0]
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// MustInt always returns value without error,
|
||||
// it returns 0 if error occurs.
|
||||
func (k *Key) MustInt(defaultVal ...int) int {
|
||||
val, err := k.Int()
|
||||
if len(defaultVal) > 0 && err != nil {
|
||||
k.value = strconv.FormatInt(int64(defaultVal[0]), 10)
|
||||
return defaultVal[0]
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// MustInt64 always returns value without error,
|
||||
// it returns 0 if error occurs.
|
||||
func (k *Key) MustInt64(defaultVal ...int64) int64 {
|
||||
val, err := k.Int64()
|
||||
if len(defaultVal) > 0 && err != nil {
|
||||
k.value = strconv.FormatInt(defaultVal[0], 10)
|
||||
return defaultVal[0]
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// MustUint always returns value without error,
|
||||
// it returns 0 if error occurs.
|
||||
func (k *Key) MustUint(defaultVal ...uint) uint {
|
||||
val, err := k.Uint()
|
||||
if len(defaultVal) > 0 && err != nil {
|
||||
k.value = strconv.FormatUint(uint64(defaultVal[0]), 10)
|
||||
return defaultVal[0]
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// MustUint64 always returns value without error,
|
||||
// it returns 0 if error occurs.
|
||||
func (k *Key) MustUint64(defaultVal ...uint64) uint64 {
|
||||
val, err := k.Uint64()
|
||||
if len(defaultVal) > 0 && err != nil {
|
||||
k.value = strconv.FormatUint(defaultVal[0], 10)
|
||||
return defaultVal[0]
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// MustDuration always returns value without error,
|
||||
// it returns zero value if error occurs.
|
||||
func (k *Key) MustDuration(defaultVal ...time.Duration) time.Duration {
|
||||
val, err := k.Duration()
|
||||
if len(defaultVal) > 0 && err != nil {
|
||||
k.value = defaultVal[0].String()
|
||||
return defaultVal[0]
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// MustTimeFormat always parses with given format and returns value without error,
|
||||
// it returns zero value if error occurs.
|
||||
func (k *Key) MustTimeFormat(format string, defaultVal ...time.Time) time.Time {
|
||||
val, err := k.TimeFormat(format)
|
||||
if len(defaultVal) > 0 && err != nil {
|
||||
k.value = defaultVal[0].Format(format)
|
||||
return defaultVal[0]
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// MustTime always parses with RFC3339 format and returns value without error,
|
||||
// it returns zero value if error occurs.
|
||||
func (k *Key) MustTime(defaultVal ...time.Time) time.Time {
|
||||
return k.MustTimeFormat(time.RFC3339, defaultVal...)
|
||||
}
|
||||
|
||||
// In always returns value without error,
|
||||
// it returns default value if error occurs or doesn't fit into candidates.
|
||||
func (k *Key) In(defaultVal string, candidates []string) string {
|
||||
val := k.String()
|
||||
for _, cand := range candidates {
|
||||
if val == cand {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// InFloat64 always returns value without error,
|
||||
// it returns default value if error occurs or doesn't fit into candidates.
|
||||
func (k *Key) InFloat64(defaultVal float64, candidates []float64) float64 {
|
||||
val := k.MustFloat64()
|
||||
for _, cand := range candidates {
|
||||
if val == cand {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// InInt always returns value without error,
|
||||
// it returns default value if error occurs or doesn't fit into candidates.
|
||||
func (k *Key) InInt(defaultVal int, candidates []int) int {
|
||||
val := k.MustInt()
|
||||
for _, cand := range candidates {
|
||||
if val == cand {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// InInt64 always returns value without error,
|
||||
// it returns default value if error occurs or doesn't fit into candidates.
|
||||
func (k *Key) InInt64(defaultVal int64, candidates []int64) int64 {
|
||||
val := k.MustInt64()
|
||||
for _, cand := range candidates {
|
||||
if val == cand {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// InUint always returns value without error,
|
||||
// it returns default value if error occurs or doesn't fit into candidates.
|
||||
func (k *Key) InUint(defaultVal uint, candidates []uint) uint {
|
||||
val := k.MustUint()
|
||||
for _, cand := range candidates {
|
||||
if val == cand {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// InUint64 always returns value without error,
|
||||
// it returns default value if error occurs or doesn't fit into candidates.
|
||||
func (k *Key) InUint64(defaultVal uint64, candidates []uint64) uint64 {
|
||||
val := k.MustUint64()
|
||||
for _, cand := range candidates {
|
||||
if val == cand {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// InTimeFormat always parses with given format and returns value without error,
|
||||
// it returns default value if error occurs or doesn't fit into candidates.
|
||||
func (k *Key) InTimeFormat(format string, defaultVal time.Time, candidates []time.Time) time.Time {
|
||||
val := k.MustTimeFormat(format)
|
||||
for _, cand := range candidates {
|
||||
if val == cand {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// InTime always parses with RFC3339 format and returns value without error,
|
||||
// it returns default value if error occurs or doesn't fit into candidates.
|
||||
func (k *Key) InTime(defaultVal time.Time, candidates []time.Time) time.Time {
|
||||
return k.InTimeFormat(time.RFC3339, defaultVal, candidates)
|
||||
}
|
||||
|
||||
// RangeFloat64 checks if value is in given range inclusively,
|
||||
// and returns default value if it's not.
|
||||
func (k *Key) RangeFloat64(defaultVal, min, max float64) float64 {
|
||||
val := k.MustFloat64()
|
||||
if val < min || val > max {
|
||||
return defaultVal
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// RangeInt checks if value is in given range inclusively,
|
||||
// and returns default value if it's not.
|
||||
func (k *Key) RangeInt(defaultVal, min, max int) int {
|
||||
val := k.MustInt()
|
||||
if val < min || val > max {
|
||||
return defaultVal
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// RangeInt64 checks if value is in given range inclusively,
|
||||
// and returns default value if it's not.
|
||||
func (k *Key) RangeInt64(defaultVal, min, max int64) int64 {
|
||||
val := k.MustInt64()
|
||||
if val < min || val > max {
|
||||
return defaultVal
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// RangeTimeFormat checks if value with given format is in given range inclusively,
|
||||
// and returns default value if it's not.
|
||||
func (k *Key) RangeTimeFormat(format string, defaultVal, min, max time.Time) time.Time {
|
||||
val := k.MustTimeFormat(format)
|
||||
if val.Unix() < min.Unix() || val.Unix() > max.Unix() {
|
||||
return defaultVal
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// RangeTime checks if value with RFC3339 format is in given range inclusively,
|
||||
// and returns default value if it's not.
|
||||
func (k *Key) RangeTime(defaultVal, min, max time.Time) time.Time {
|
||||
return k.RangeTimeFormat(time.RFC3339, defaultVal, min, max)
|
||||
}
|
||||
|
||||
// Strings returns list of string divided by given delimiter.
|
||||
func (k *Key) Strings(delim string) []string {
|
||||
str := k.String()
|
||||
if len(str) == 0 {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
runes := []rune(str)
|
||||
vals := make([]string, 0, 2)
|
||||
var buf bytes.Buffer
|
||||
escape := false
|
||||
idx := 0
|
||||
for {
|
||||
if escape {
|
||||
escape = false
|
||||
if runes[idx] != '\\' && !strings.HasPrefix(string(runes[idx:]), delim) {
|
||||
buf.WriteRune('\\')
|
||||
}
|
||||
buf.WriteRune(runes[idx])
|
||||
} else {
|
||||
if runes[idx] == '\\' {
|
||||
escape = true
|
||||
} else if strings.HasPrefix(string(runes[idx:]), delim) {
|
||||
idx += len(delim) - 1
|
||||
vals = append(vals, strings.TrimSpace(buf.String()))
|
||||
buf.Reset()
|
||||
} else {
|
||||
buf.WriteRune(runes[idx])
|
||||
}
|
||||
}
|
||||
idx++
|
||||
if idx == len(runes) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if buf.Len() > 0 {
|
||||
vals = append(vals, strings.TrimSpace(buf.String()))
|
||||
}
|
||||
|
||||
return vals
|
||||
}
|
||||
|
||||
// StringsWithShadows returns list of string divided by given delimiter.
|
||||
// Shadows will also be appended if any.
|
||||
func (k *Key) StringsWithShadows(delim string) []string {
|
||||
vals := k.ValueWithShadows()
|
||||
results := make([]string, 0, len(vals)*2)
|
||||
for i := range vals {
|
||||
if len(vals) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
results = append(results, strings.Split(vals[i], delim)...)
|
||||
}
|
||||
|
||||
for i := range results {
|
||||
results[i] = k.transformValue(strings.TrimSpace(results[i]))
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// Float64s returns list of float64 divided by given delimiter. Any invalid input will be treated as zero value.
|
||||
func (k *Key) Float64s(delim string) []float64 {
|
||||
vals, _ := k.parseFloat64s(k.Strings(delim), true, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// Ints returns list of int divided by given delimiter. Any invalid input will be treated as zero value.
|
||||
func (k *Key) Ints(delim string) []int {
|
||||
vals, _ := k.parseInts(k.Strings(delim), true, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// Int64s returns list of int64 divided by given delimiter. Any invalid input will be treated as zero value.
|
||||
func (k *Key) Int64s(delim string) []int64 {
|
||||
vals, _ := k.parseInt64s(k.Strings(delim), true, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// Uints returns list of uint divided by given delimiter. Any invalid input will be treated as zero value.
|
||||
func (k *Key) Uints(delim string) []uint {
|
||||
vals, _ := k.parseUints(k.Strings(delim), true, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// Uint64s returns list of uint64 divided by given delimiter. Any invalid input will be treated as zero value.
|
||||
func (k *Key) Uint64s(delim string) []uint64 {
|
||||
vals, _ := k.parseUint64s(k.Strings(delim), true, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// Bools returns list of bool divided by given delimiter. Any invalid input will be treated as zero value.
|
||||
func (k *Key) Bools(delim string) []bool {
|
||||
vals, _ := k.parseBools(k.Strings(delim), true, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// TimesFormat parses with given format and returns list of time.Time divided by given delimiter.
|
||||
// Any invalid input will be treated as zero value (0001-01-01 00:00:00 +0000 UTC).
|
||||
func (k *Key) TimesFormat(format, delim string) []time.Time {
|
||||
vals, _ := k.parseTimesFormat(format, k.Strings(delim), true, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// Times parses with RFC3339 format and returns list of time.Time divided by given delimiter.
|
||||
// Any invalid input will be treated as zero value (0001-01-01 00:00:00 +0000 UTC).
|
||||
func (k *Key) Times(delim string) []time.Time {
|
||||
return k.TimesFormat(time.RFC3339, delim)
|
||||
}
|
||||
|
||||
// ValidFloat64s returns list of float64 divided by given delimiter. If some value is not float, then
|
||||
// it will not be included to result list.
|
||||
func (k *Key) ValidFloat64s(delim string) []float64 {
|
||||
vals, _ := k.parseFloat64s(k.Strings(delim), false, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// ValidInts returns list of int divided by given delimiter. If some value is not integer, then it will
|
||||
// not be included to result list.
|
||||
func (k *Key) ValidInts(delim string) []int {
|
||||
vals, _ := k.parseInts(k.Strings(delim), false, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// ValidInt64s returns list of int64 divided by given delimiter. If some value is not 64-bit integer,
|
||||
// then it will not be included to result list.
|
||||
func (k *Key) ValidInt64s(delim string) []int64 {
|
||||
vals, _ := k.parseInt64s(k.Strings(delim), false, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// ValidUints returns list of uint divided by given delimiter. If some value is not unsigned integer,
|
||||
// then it will not be included to result list.
|
||||
func (k *Key) ValidUints(delim string) []uint {
|
||||
vals, _ := k.parseUints(k.Strings(delim), false, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// ValidUint64s returns list of uint64 divided by given delimiter. If some value is not 64-bit unsigned
|
||||
// integer, then it will not be included to result list.
|
||||
func (k *Key) ValidUint64s(delim string) []uint64 {
|
||||
vals, _ := k.parseUint64s(k.Strings(delim), false, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// ValidBools returns list of bool divided by given delimiter. If some value is not 64-bit unsigned
|
||||
// integer, then it will not be included to result list.
|
||||
func (k *Key) ValidBools(delim string) []bool {
|
||||
vals, _ := k.parseBools(k.Strings(delim), false, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// ValidTimesFormat parses with given format and returns list of time.Time divided by given delimiter.
|
||||
func (k *Key) ValidTimesFormat(format, delim string) []time.Time {
|
||||
vals, _ := k.parseTimesFormat(format, k.Strings(delim), false, false)
|
||||
return vals
|
||||
}
|
||||
|
||||
// ValidTimes parses with RFC3339 format and returns list of time.Time divided by given delimiter.
|
||||
func (k *Key) ValidTimes(delim string) []time.Time {
|
||||
return k.ValidTimesFormat(time.RFC3339, delim)
|
||||
}
|
||||
|
||||
// StrictFloat64s returns list of float64 divided by given delimiter or error on first invalid input.
|
||||
func (k *Key) StrictFloat64s(delim string) ([]float64, error) {
|
||||
return k.parseFloat64s(k.Strings(delim), false, true)
|
||||
}
|
||||
|
||||
// StrictInts returns list of int divided by given delimiter or error on first invalid input.
|
||||
func (k *Key) StrictInts(delim string) ([]int, error) {
|
||||
return k.parseInts(k.Strings(delim), false, true)
|
||||
}
|
||||
|
||||
// StrictInt64s returns list of int64 divided by given delimiter or error on first invalid input.
|
||||
func (k *Key) StrictInt64s(delim string) ([]int64, error) {
|
||||
return k.parseInt64s(k.Strings(delim), false, true)
|
||||
}
|
||||
|
||||
// StrictUints returns list of uint divided by given delimiter or error on first invalid input.
|
||||
func (k *Key) StrictUints(delim string) ([]uint, error) {
|
||||
return k.parseUints(k.Strings(delim), false, true)
|
||||
}
|
||||
|
||||
// StrictUint64s returns list of uint64 divided by given delimiter or error on first invalid input.
|
||||
func (k *Key) StrictUint64s(delim string) ([]uint64, error) {
|
||||
return k.parseUint64s(k.Strings(delim), false, true)
|
||||
}
|
||||
|
||||
// StrictBools returns list of bool divided by given delimiter or error on first invalid input.
|
||||
func (k *Key) StrictBools(delim string) ([]bool, error) {
|
||||
return k.parseBools(k.Strings(delim), false, true)
|
||||
}
|
||||
|
||||
// StrictTimesFormat parses with given format and returns list of time.Time divided by given delimiter
|
||||
// or error on first invalid input.
|
||||
func (k *Key) StrictTimesFormat(format, delim string) ([]time.Time, error) {
|
||||
return k.parseTimesFormat(format, k.Strings(delim), false, true)
|
||||
}
|
||||
|
||||
// StrictTimes parses with RFC3339 format and returns list of time.Time divided by given delimiter
|
||||
// or error on first invalid input.
|
||||
func (k *Key) StrictTimes(delim string) ([]time.Time, error) {
|
||||
return k.StrictTimesFormat(time.RFC3339, delim)
|
||||
}
|
||||
|
||||
// parseBools transforms strings to bools.
|
||||
func (k *Key) parseBools(strs []string, addInvalid, returnOnInvalid bool) ([]bool, error) {
|
||||
vals := make([]bool, 0, len(strs))
|
||||
parser := func(str string) (interface{}, error) {
|
||||
val, err := parseBool(str)
|
||||
return val, err
|
||||
}
|
||||
rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
|
||||
if err == nil {
|
||||
for _, val := range rawVals {
|
||||
vals = append(vals, val.(bool))
|
||||
}
|
||||
}
|
||||
return vals, err
|
||||
}
|
||||
|
||||
// parseFloat64s transforms strings to float64s.
|
||||
func (k *Key) parseFloat64s(strs []string, addInvalid, returnOnInvalid bool) ([]float64, error) {
|
||||
vals := make([]float64, 0, len(strs))
|
||||
parser := func(str string) (interface{}, error) {
|
||||
val, err := strconv.ParseFloat(str, 64)
|
||||
return val, err
|
||||
}
|
||||
rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
|
||||
if err == nil {
|
||||
for _, val := range rawVals {
|
||||
vals = append(vals, val.(float64))
|
||||
}
|
||||
}
|
||||
return vals, err
|
||||
}
|
||||
|
||||
// parseInts transforms strings to ints.
|
||||
func (k *Key) parseInts(strs []string, addInvalid, returnOnInvalid bool) ([]int, error) {
|
||||
vals := make([]int, 0, len(strs))
|
||||
parser := func(str string) (interface{}, error) {
|
||||
val, err := strconv.ParseInt(str, 0, 64)
|
||||
return val, err
|
||||
}
|
||||
rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
|
||||
if err == nil {
|
||||
for _, val := range rawVals {
|
||||
vals = append(vals, int(val.(int64)))
|
||||
}
|
||||
}
|
||||
return vals, err
|
||||
}
|
||||
|
||||
// parseInt64s transforms strings to int64s.
|
||||
func (k *Key) parseInt64s(strs []string, addInvalid, returnOnInvalid bool) ([]int64, error) {
|
||||
vals := make([]int64, 0, len(strs))
|
||||
parser := func(str string) (interface{}, error) {
|
||||
val, err := strconv.ParseInt(str, 0, 64)
|
||||
return val, err
|
||||
}
|
||||
|
||||
rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
|
||||
if err == nil {
|
||||
for _, val := range rawVals {
|
||||
vals = append(vals, val.(int64))
|
||||
}
|
||||
}
|
||||
return vals, err
|
||||
}
|
||||
|
||||
// parseUints transforms strings to uints.
|
||||
func (k *Key) parseUints(strs []string, addInvalid, returnOnInvalid bool) ([]uint, error) {
|
||||
vals := make([]uint, 0, len(strs))
|
||||
parser := func(str string) (interface{}, error) {
|
||||
val, err := strconv.ParseUint(str, 0, 64)
|
||||
return val, err
|
||||
}
|
||||
|
||||
rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
|
||||
if err == nil {
|
||||
for _, val := range rawVals {
|
||||
vals = append(vals, uint(val.(uint64)))
|
||||
}
|
||||
}
|
||||
return vals, err
|
||||
}
|
||||
|
||||
// parseUint64s transforms strings to uint64s.
|
||||
func (k *Key) parseUint64s(strs []string, addInvalid, returnOnInvalid bool) ([]uint64, error) {
|
||||
vals := make([]uint64, 0, len(strs))
|
||||
parser := func(str string) (interface{}, error) {
|
||||
val, err := strconv.ParseUint(str, 0, 64)
|
||||
return val, err
|
||||
}
|
||||
rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
|
||||
if err == nil {
|
||||
for _, val := range rawVals {
|
||||
vals = append(vals, val.(uint64))
|
||||
}
|
||||
}
|
||||
return vals, err
|
||||
}
|
||||
|
||||
type Parser func(str string) (interface{}, error)
|
||||
|
||||
// parseTimesFormat transforms strings to times in given format.
|
||||
func (k *Key) parseTimesFormat(format string, strs []string, addInvalid, returnOnInvalid bool) ([]time.Time, error) {
|
||||
vals := make([]time.Time, 0, len(strs))
|
||||
parser := func(str string) (interface{}, error) {
|
||||
val, err := time.Parse(format, str)
|
||||
return val, err
|
||||
}
|
||||
rawVals, err := k.doParse(strs, addInvalid, returnOnInvalid, parser)
|
||||
if err == nil {
|
||||
for _, val := range rawVals {
|
||||
vals = append(vals, val.(time.Time))
|
||||
}
|
||||
}
|
||||
return vals, err
|
||||
}
|
||||
|
||||
// doParse transforms strings to different types
|
||||
func (k *Key) doParse(strs []string, addInvalid, returnOnInvalid bool, parser Parser) ([]interface{}, error) {
|
||||
vals := make([]interface{}, 0, len(strs))
|
||||
for _, str := range strs {
|
||||
val, err := parser(str)
|
||||
if err != nil && returnOnInvalid {
|
||||
return nil, err
|
||||
}
|
||||
if err == nil || addInvalid {
|
||||
vals = append(vals, val)
|
||||
}
|
||||
}
|
||||
return vals, nil
|
||||
}
|
||||
|
||||
// SetValue changes key value.
|
||||
func (k *Key) SetValue(v string) {
|
||||
if k.s.f.BlockMode {
|
||||
k.s.f.lock.Lock()
|
||||
defer k.s.f.lock.Unlock()
|
||||
}
|
||||
|
||||
k.value = v
|
||||
k.s.keysHash[k.name] = v
|
||||
}
|
||||
-520
@@ -1,520 +0,0 @@
|
||||
// Copyright 2015 Unknwon
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package ini
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const minReaderBufferSize = 4096
|
||||
|
||||
var pythonMultiline = regexp.MustCompile(`^([\t\f ]+)(.*)`)
|
||||
|
||||
type parserOptions struct {
|
||||
IgnoreContinuation bool
|
||||
IgnoreInlineComment bool
|
||||
AllowPythonMultilineValues bool
|
||||
SpaceBeforeInlineComment bool
|
||||
UnescapeValueDoubleQuotes bool
|
||||
UnescapeValueCommentSymbols bool
|
||||
PreserveSurroundedQuote bool
|
||||
DebugFunc DebugFunc
|
||||
ReaderBufferSize int
|
||||
}
|
||||
|
||||
type parser struct {
|
||||
buf *bufio.Reader
|
||||
options parserOptions
|
||||
|
||||
isEOF bool
|
||||
count int
|
||||
comment *bytes.Buffer
|
||||
}
|
||||
|
||||
func (p *parser) debug(format string, args ...interface{}) {
|
||||
if p.options.DebugFunc != nil {
|
||||
p.options.DebugFunc(fmt.Sprintf(format, args...))
|
||||
}
|
||||
}
|
||||
|
||||
func newParser(r io.Reader, opts parserOptions) *parser {
|
||||
size := opts.ReaderBufferSize
|
||||
if size < minReaderBufferSize {
|
||||
size = minReaderBufferSize
|
||||
}
|
||||
|
||||
return &parser{
|
||||
buf: bufio.NewReaderSize(r, size),
|
||||
options: opts,
|
||||
count: 1,
|
||||
comment: &bytes.Buffer{},
|
||||
}
|
||||
}
|
||||
|
||||
// BOM handles header of UTF-8, UTF-16 LE and UTF-16 BE's BOM format.
|
||||
// http://en.wikipedia.org/wiki/Byte_order_mark#Representations_of_byte_order_marks_by_encoding
|
||||
func (p *parser) BOM() error {
|
||||
mask, err := p.buf.Peek(2)
|
||||
if err != nil && err != io.EOF {
|
||||
return err
|
||||
} else if len(mask) < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch {
|
||||
case mask[0] == 254 && mask[1] == 255:
|
||||
fallthrough
|
||||
case mask[0] == 255 && mask[1] == 254:
|
||||
_, err = p.buf.Read(mask)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case mask[0] == 239 && mask[1] == 187:
|
||||
mask, err := p.buf.Peek(3)
|
||||
if err != nil && err != io.EOF {
|
||||
return err
|
||||
} else if len(mask) < 3 {
|
||||
return nil
|
||||
}
|
||||
if mask[2] == 191 {
|
||||
_, err = p.buf.Read(mask)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *parser) readUntil(delim byte) ([]byte, error) {
|
||||
data, err := p.buf.ReadBytes(delim)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
p.isEOF = true
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func cleanComment(in []byte) ([]byte, bool) {
|
||||
i := bytes.IndexAny(in, "#;")
|
||||
if i == -1 {
|
||||
return nil, false
|
||||
}
|
||||
return in[i:], true
|
||||
}
|
||||
|
||||
func readKeyName(delimiters string, in []byte) (string, int, error) {
|
||||
line := string(in)
|
||||
|
||||
// Check if key name surrounded by quotes.
|
||||
var keyQuote string
|
||||
if line[0] == '"' {
|
||||
if len(line) > 6 && line[0:3] == `"""` {
|
||||
keyQuote = `"""`
|
||||
} else {
|
||||
keyQuote = `"`
|
||||
}
|
||||
} else if line[0] == '`' {
|
||||
keyQuote = "`"
|
||||
}
|
||||
|
||||
// Get out key name
|
||||
var endIdx int
|
||||
if len(keyQuote) > 0 {
|
||||
startIdx := len(keyQuote)
|
||||
// FIXME: fail case -> """"""name"""=value
|
||||
pos := strings.Index(line[startIdx:], keyQuote)
|
||||
if pos == -1 {
|
||||
return "", -1, fmt.Errorf("missing closing key quote: %s", line)
|
||||
}
|
||||
pos += startIdx
|
||||
|
||||
// Find key-value delimiter
|
||||
i := strings.IndexAny(line[pos+startIdx:], delimiters)
|
||||
if i < 0 {
|
||||
return "", -1, ErrDelimiterNotFound{line}
|
||||
}
|
||||
endIdx = pos + i
|
||||
return strings.TrimSpace(line[startIdx:pos]), endIdx + startIdx + 1, nil
|
||||
}
|
||||
|
||||
endIdx = strings.IndexAny(line, delimiters)
|
||||
if endIdx < 0 {
|
||||
return "", -1, ErrDelimiterNotFound{line}
|
||||
}
|
||||
if endIdx == 0 {
|
||||
return "", -1, ErrEmptyKeyName{line}
|
||||
}
|
||||
|
||||
return strings.TrimSpace(line[0:endIdx]), endIdx + 1, nil
|
||||
}
|
||||
|
||||
func (p *parser) readMultilines(line, val, valQuote string) (string, error) {
|
||||
for {
|
||||
data, err := p.readUntil('\n')
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
next := string(data)
|
||||
|
||||
pos := strings.LastIndex(next, valQuote)
|
||||
if pos > -1 {
|
||||
val += next[:pos]
|
||||
|
||||
comment, has := cleanComment([]byte(next[pos:]))
|
||||
if has {
|
||||
p.comment.Write(bytes.TrimSpace(comment))
|
||||
}
|
||||
break
|
||||
}
|
||||
val += next
|
||||
if p.isEOF {
|
||||
return "", fmt.Errorf("missing closing key quote from %q to %q", line, next)
|
||||
}
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func (p *parser) readContinuationLines(val string) (string, error) {
|
||||
for {
|
||||
data, err := p.readUntil('\n')
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
next := strings.TrimSpace(string(data))
|
||||
|
||||
if len(next) == 0 {
|
||||
break
|
||||
}
|
||||
val += next
|
||||
if val[len(val)-1] != '\\' {
|
||||
break
|
||||
}
|
||||
val = val[:len(val)-1]
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
|
||||
// hasSurroundedQuote check if and only if the first and last characters
|
||||
// are quotes \" or \'.
|
||||
// It returns false if any other parts also contain same kind of quotes.
|
||||
func hasSurroundedQuote(in string, quote byte) bool {
|
||||
return len(in) >= 2 && in[0] == quote && in[len(in)-1] == quote &&
|
||||
strings.IndexByte(in[1:], quote) == len(in)-2
|
||||
}
|
||||
|
||||
func (p *parser) readValue(in []byte, bufferSize int) (string, error) {
|
||||
|
||||
line := strings.TrimLeftFunc(string(in), unicode.IsSpace)
|
||||
if len(line) == 0 {
|
||||
if p.options.AllowPythonMultilineValues && len(in) > 0 && in[len(in)-1] == '\n' {
|
||||
return p.readPythonMultilines(line, bufferSize)
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
var valQuote string
|
||||
if len(line) > 3 && line[0:3] == `"""` {
|
||||
valQuote = `"""`
|
||||
} else if line[0] == '`' {
|
||||
valQuote = "`"
|
||||
} else if p.options.UnescapeValueDoubleQuotes && line[0] == '"' {
|
||||
valQuote = `"`
|
||||
}
|
||||
|
||||
if len(valQuote) > 0 {
|
||||
startIdx := len(valQuote)
|
||||
pos := strings.LastIndex(line[startIdx:], valQuote)
|
||||
// Check for multi-line value
|
||||
if pos == -1 {
|
||||
return p.readMultilines(line, line[startIdx:], valQuote)
|
||||
}
|
||||
|
||||
if p.options.UnescapeValueDoubleQuotes && valQuote == `"` {
|
||||
return strings.Replace(line[startIdx:pos+startIdx], `\"`, `"`, -1), nil
|
||||
}
|
||||
return line[startIdx : pos+startIdx], nil
|
||||
}
|
||||
|
||||
lastChar := line[len(line)-1]
|
||||
// Won't be able to reach here if value only contains whitespace
|
||||
line = strings.TrimSpace(line)
|
||||
trimmedLastChar := line[len(line)-1]
|
||||
|
||||
// Check continuation lines when desired
|
||||
if !p.options.IgnoreContinuation && trimmedLastChar == '\\' {
|
||||
return p.readContinuationLines(line[:len(line)-1])
|
||||
}
|
||||
|
||||
// Check if ignore inline comment
|
||||
if !p.options.IgnoreInlineComment {
|
||||
var i int
|
||||
if p.options.SpaceBeforeInlineComment {
|
||||
i = strings.Index(line, " #")
|
||||
if i == -1 {
|
||||
i = strings.Index(line, " ;")
|
||||
}
|
||||
|
||||
} else {
|
||||
i = strings.IndexAny(line, "#;")
|
||||
}
|
||||
|
||||
if i > -1 {
|
||||
p.comment.WriteString(line[i:])
|
||||
line = strings.TrimSpace(line[:i])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Trim single and double quotes
|
||||
if (hasSurroundedQuote(line, '\'') ||
|
||||
hasSurroundedQuote(line, '"')) && !p.options.PreserveSurroundedQuote {
|
||||
line = line[1 : len(line)-1]
|
||||
} else if len(valQuote) == 0 && p.options.UnescapeValueCommentSymbols {
|
||||
line = strings.ReplaceAll(line, `\;`, ";")
|
||||
line = strings.ReplaceAll(line, `\#`, "#")
|
||||
} else if p.options.AllowPythonMultilineValues && lastChar == '\n' {
|
||||
return p.readPythonMultilines(line, bufferSize)
|
||||
}
|
||||
|
||||
return line, nil
|
||||
}
|
||||
|
||||
func (p *parser) readPythonMultilines(line string, bufferSize int) (string, error) {
|
||||
parserBufferPeekResult, _ := p.buf.Peek(bufferSize)
|
||||
peekBuffer := bytes.NewBuffer(parserBufferPeekResult)
|
||||
|
||||
for {
|
||||
peekData, peekErr := peekBuffer.ReadBytes('\n')
|
||||
if peekErr != nil && peekErr != io.EOF {
|
||||
p.debug("readPythonMultilines: failed to peek with error: %v", peekErr)
|
||||
return "", peekErr
|
||||
}
|
||||
|
||||
p.debug("readPythonMultilines: parsing %q", string(peekData))
|
||||
|
||||
peekMatches := pythonMultiline.FindStringSubmatch(string(peekData))
|
||||
p.debug("readPythonMultilines: matched %d parts", len(peekMatches))
|
||||
for n, v := range peekMatches {
|
||||
p.debug(" %d: %q", n, v)
|
||||
}
|
||||
|
||||
// Return if not a Python multiline value.
|
||||
if len(peekMatches) != 3 {
|
||||
p.debug("readPythonMultilines: end of value, got: %q", line)
|
||||
return line, nil
|
||||
}
|
||||
|
||||
// Advance the parser reader (buffer) in-sync with the peek buffer.
|
||||
_, err := p.buf.Discard(len(peekData))
|
||||
if err != nil {
|
||||
p.debug("readPythonMultilines: failed to skip to the end, returning error")
|
||||
return "", err
|
||||
}
|
||||
|
||||
line += "\n" + peekMatches[0]
|
||||
}
|
||||
}
|
||||
|
||||
// parse parses data through an io.Reader.
|
||||
func (f *File) parse(reader io.Reader) (err error) {
|
||||
p := newParser(reader, parserOptions{
|
||||
IgnoreContinuation: f.options.IgnoreContinuation,
|
||||
IgnoreInlineComment: f.options.IgnoreInlineComment,
|
||||
AllowPythonMultilineValues: f.options.AllowPythonMultilineValues,
|
||||
SpaceBeforeInlineComment: f.options.SpaceBeforeInlineComment,
|
||||
UnescapeValueDoubleQuotes: f.options.UnescapeValueDoubleQuotes,
|
||||
UnescapeValueCommentSymbols: f.options.UnescapeValueCommentSymbols,
|
||||
PreserveSurroundedQuote: f.options.PreserveSurroundedQuote,
|
||||
DebugFunc: f.options.DebugFunc,
|
||||
ReaderBufferSize: f.options.ReaderBufferSize,
|
||||
})
|
||||
if err = p.BOM(); err != nil {
|
||||
return fmt.Errorf("BOM: %v", err)
|
||||
}
|
||||
|
||||
// Ignore error because default section name is never empty string.
|
||||
name := DefaultSection
|
||||
if f.options.Insensitive || f.options.InsensitiveSections {
|
||||
name = strings.ToLower(DefaultSection)
|
||||
}
|
||||
section, _ := f.NewSection(name)
|
||||
|
||||
// This "last" is not strictly equivalent to "previous one" if current key is not the first nested key
|
||||
var isLastValueEmpty bool
|
||||
var lastRegularKey *Key
|
||||
|
||||
var line []byte
|
||||
var inUnparseableSection bool
|
||||
|
||||
// NOTE: Iterate and increase `currentPeekSize` until
|
||||
// the size of the parser buffer is found.
|
||||
// TODO(unknwon): When Golang 1.10 is the lowest version supported, replace with `parserBufferSize := p.buf.Size()`.
|
||||
parserBufferSize := 0
|
||||
// NOTE: Peek 4kb at a time.
|
||||
currentPeekSize := minReaderBufferSize
|
||||
|
||||
if f.options.AllowPythonMultilineValues {
|
||||
for {
|
||||
peekBytes, _ := p.buf.Peek(currentPeekSize)
|
||||
peekBytesLength := len(peekBytes)
|
||||
|
||||
if parserBufferSize >= peekBytesLength {
|
||||
break
|
||||
}
|
||||
|
||||
currentPeekSize *= 2
|
||||
parserBufferSize = peekBytesLength
|
||||
}
|
||||
}
|
||||
|
||||
for !p.isEOF {
|
||||
line, err = p.readUntil('\n')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if f.options.AllowNestedValues &&
|
||||
isLastValueEmpty && len(line) > 0 {
|
||||
if line[0] == ' ' || line[0] == '\t' {
|
||||
err = lastRegularKey.addNestedValue(string(bytes.TrimSpace(line)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
line = bytes.TrimLeftFunc(line, unicode.IsSpace)
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Comments
|
||||
if line[0] == '#' || line[0] == ';' {
|
||||
// Note: we do not care ending line break,
|
||||
// it is needed for adding second line,
|
||||
// so just clean it once at the end when set to value.
|
||||
p.comment.Write(line)
|
||||
continue
|
||||
}
|
||||
|
||||
// Section
|
||||
if line[0] == '[' {
|
||||
// Read to the next ']' (TODO: support quoted strings)
|
||||
closeIdx := bytes.LastIndexByte(line, ']')
|
||||
if closeIdx == -1 {
|
||||
return fmt.Errorf("unclosed section: %s", line)
|
||||
}
|
||||
|
||||
name := string(line[1:closeIdx])
|
||||
section, err = f.NewSection(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
comment, has := cleanComment(line[closeIdx+1:])
|
||||
if has {
|
||||
p.comment.Write(comment)
|
||||
}
|
||||
|
||||
section.Comment = strings.TrimSpace(p.comment.String())
|
||||
|
||||
// Reset auto-counter and comments
|
||||
p.comment.Reset()
|
||||
p.count = 1
|
||||
// Nested values can't span sections
|
||||
isLastValueEmpty = false
|
||||
|
||||
inUnparseableSection = false
|
||||
for i := range f.options.UnparseableSections {
|
||||
if f.options.UnparseableSections[i] == name ||
|
||||
((f.options.Insensitive || f.options.InsensitiveSections) && strings.EqualFold(f.options.UnparseableSections[i], name)) {
|
||||
inUnparseableSection = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if inUnparseableSection {
|
||||
section.isRawSection = true
|
||||
section.rawBody += string(line)
|
||||
continue
|
||||
}
|
||||
|
||||
kname, offset, err := readKeyName(f.options.KeyValueDelimiters, line)
|
||||
if err != nil {
|
||||
switch {
|
||||
// Treat as boolean key when desired, and whole line is key name.
|
||||
case IsErrDelimiterNotFound(err):
|
||||
switch {
|
||||
case f.options.AllowBooleanKeys:
|
||||
kname, err := p.readValue(line, parserBufferSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key, err := section.NewBooleanKey(kname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key.Comment = strings.TrimSpace(p.comment.String())
|
||||
p.comment.Reset()
|
||||
continue
|
||||
|
||||
case f.options.SkipUnrecognizableLines:
|
||||
continue
|
||||
}
|
||||
case IsErrEmptyKeyName(err) && f.options.SkipUnrecognizableLines:
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Auto increment.
|
||||
isAutoIncr := false
|
||||
if kname == "-" {
|
||||
isAutoIncr = true
|
||||
kname = "#" + strconv.Itoa(p.count)
|
||||
p.count++
|
||||
}
|
||||
|
||||
value, err := p.readValue(line[offset:], parserBufferSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
isLastValueEmpty = len(value) == 0
|
||||
|
||||
key, err := section.NewKey(kname, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key.isAutoIncrement = isAutoIncr
|
||||
key.Comment = strings.TrimSpace(p.comment.String())
|
||||
p.comment.Reset()
|
||||
lastRegularKey = key
|
||||
}
|
||||
return nil
|
||||
}
|
||||
-256
@@ -1,256 +0,0 @@
|
||||
// Copyright 2014 Unknwon
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package ini
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Section represents a config section.
|
||||
type Section struct {
|
||||
f *File
|
||||
Comment string
|
||||
name string
|
||||
keys map[string]*Key
|
||||
keyList []string
|
||||
keysHash map[string]string
|
||||
|
||||
isRawSection bool
|
||||
rawBody string
|
||||
}
|
||||
|
||||
func newSection(f *File, name string) *Section {
|
||||
return &Section{
|
||||
f: f,
|
||||
name: name,
|
||||
keys: make(map[string]*Key),
|
||||
keyList: make([]string, 0, 10),
|
||||
keysHash: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
// Name returns name of Section.
|
||||
func (s *Section) Name() string {
|
||||
return s.name
|
||||
}
|
||||
|
||||
// Body returns rawBody of Section if the section was marked as unparseable.
|
||||
// It still follows the other rules of the INI format surrounding leading/trailing whitespace.
|
||||
func (s *Section) Body() string {
|
||||
return strings.TrimSpace(s.rawBody)
|
||||
}
|
||||
|
||||
// SetBody updates body content only if section is raw.
|
||||
func (s *Section) SetBody(body string) {
|
||||
if !s.isRawSection {
|
||||
return
|
||||
}
|
||||
s.rawBody = body
|
||||
}
|
||||
|
||||
// NewKey creates a new key to given section.
|
||||
func (s *Section) NewKey(name, val string) (*Key, error) {
|
||||
if len(name) == 0 {
|
||||
return nil, errors.New("error creating new key: empty key name")
|
||||
} else if s.f.options.Insensitive || s.f.options.InsensitiveKeys {
|
||||
name = strings.ToLower(name)
|
||||
}
|
||||
|
||||
if s.f.BlockMode {
|
||||
s.f.lock.Lock()
|
||||
defer s.f.lock.Unlock()
|
||||
}
|
||||
|
||||
if inSlice(name, s.keyList) {
|
||||
if s.f.options.AllowShadows {
|
||||
if err := s.keys[name].addShadow(val); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
s.keys[name].value = val
|
||||
s.keysHash[name] = val
|
||||
}
|
||||
return s.keys[name], nil
|
||||
}
|
||||
|
||||
s.keyList = append(s.keyList, name)
|
||||
s.keys[name] = newKey(s, name, val)
|
||||
s.keysHash[name] = val
|
||||
return s.keys[name], nil
|
||||
}
|
||||
|
||||
// NewBooleanKey creates a new boolean type key to given section.
|
||||
func (s *Section) NewBooleanKey(name string) (*Key, error) {
|
||||
key, err := s.NewKey(name, "true")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
key.isBooleanType = true
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// GetKey returns key in section by given name.
|
||||
func (s *Section) GetKey(name string) (*Key, error) {
|
||||
if s.f.BlockMode {
|
||||
s.f.lock.RLock()
|
||||
}
|
||||
if s.f.options.Insensitive || s.f.options.InsensitiveKeys {
|
||||
name = strings.ToLower(name)
|
||||
}
|
||||
key := s.keys[name]
|
||||
if s.f.BlockMode {
|
||||
s.f.lock.RUnlock()
|
||||
}
|
||||
|
||||
if key == nil {
|
||||
// Check if it is a child-section.
|
||||
sname := s.name
|
||||
for {
|
||||
if i := strings.LastIndex(sname, s.f.options.ChildSectionDelimiter); i > -1 {
|
||||
sname = sname[:i]
|
||||
sec, err := s.f.GetSection(sname)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
return sec.GetKey(name)
|
||||
}
|
||||
break
|
||||
}
|
||||
return nil, fmt.Errorf("error when getting key of section %q: key %q not exists", s.name, name)
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// HasKey returns true if section contains a key with given name.
|
||||
func (s *Section) HasKey(name string) bool {
|
||||
key, _ := s.GetKey(name)
|
||||
return key != nil
|
||||
}
|
||||
|
||||
// Deprecated: Use "HasKey" instead.
|
||||
func (s *Section) Haskey(name string) bool {
|
||||
return s.HasKey(name)
|
||||
}
|
||||
|
||||
// HasValue returns true if section contains given raw value.
|
||||
func (s *Section) HasValue(value string) bool {
|
||||
if s.f.BlockMode {
|
||||
s.f.lock.RLock()
|
||||
defer s.f.lock.RUnlock()
|
||||
}
|
||||
|
||||
for _, k := range s.keys {
|
||||
if value == k.value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Key assumes named Key exists in section and returns a zero-value when not.
|
||||
func (s *Section) Key(name string) *Key {
|
||||
key, err := s.GetKey(name)
|
||||
if err != nil {
|
||||
// It's OK here because the only possible error is empty key name,
|
||||
// but if it's empty, this piece of code won't be executed.
|
||||
key, _ = s.NewKey(name, "")
|
||||
return key
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
// Keys returns list of keys of section.
|
||||
func (s *Section) Keys() []*Key {
|
||||
keys := make([]*Key, len(s.keyList))
|
||||
for i := range s.keyList {
|
||||
keys[i] = s.Key(s.keyList[i])
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
// ParentKeys returns list of keys of parent section.
|
||||
func (s *Section) ParentKeys() []*Key {
|
||||
var parentKeys []*Key
|
||||
sname := s.name
|
||||
for {
|
||||
if i := strings.LastIndex(sname, s.f.options.ChildSectionDelimiter); i > -1 {
|
||||
sname = sname[:i]
|
||||
sec, err := s.f.GetSection(sname)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
parentKeys = append(parentKeys, sec.Keys()...)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
return parentKeys
|
||||
}
|
||||
|
||||
// KeyStrings returns list of key names of section.
|
||||
func (s *Section) KeyStrings() []string {
|
||||
list := make([]string, len(s.keyList))
|
||||
copy(list, s.keyList)
|
||||
return list
|
||||
}
|
||||
|
||||
// KeysHash returns keys hash consisting of names and values.
|
||||
func (s *Section) KeysHash() map[string]string {
|
||||
if s.f.BlockMode {
|
||||
s.f.lock.RLock()
|
||||
defer s.f.lock.RUnlock()
|
||||
}
|
||||
|
||||
hash := make(map[string]string, len(s.keysHash))
|
||||
for key, value := range s.keysHash {
|
||||
hash[key] = value
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
||||
// DeleteKey deletes a key from section.
|
||||
func (s *Section) DeleteKey(name string) {
|
||||
if s.f.BlockMode {
|
||||
s.f.lock.Lock()
|
||||
defer s.f.lock.Unlock()
|
||||
}
|
||||
|
||||
for i, k := range s.keyList {
|
||||
if k == name {
|
||||
s.keyList = append(s.keyList[:i], s.keyList[i+1:]...)
|
||||
delete(s.keys, name)
|
||||
delete(s.keysHash, name)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ChildSections returns a list of child sections of current section.
|
||||
// For example, "[parent.child1]" and "[parent.child12]" are child sections
|
||||
// of section "[parent]".
|
||||
func (s *Section) ChildSections() []*Section {
|
||||
prefix := s.name + s.f.options.ChildSectionDelimiter
|
||||
children := make([]*Section, 0, 3)
|
||||
for _, name := range s.f.sectionList {
|
||||
if strings.HasPrefix(name, prefix) {
|
||||
children = append(children, s.f.sections[name]...)
|
||||
}
|
||||
}
|
||||
return children
|
||||
}
|
||||
-747
@@ -1,747 +0,0 @@
|
||||
// Copyright 2014 Unknwon
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
||||
// not use this file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package ini
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// NameMapper represents a ini tag name mapper.
|
||||
type NameMapper func(string) string
|
||||
|
||||
// Built-in name getters.
|
||||
var (
|
||||
// SnackCase converts to format SNACK_CASE.
|
||||
SnackCase NameMapper = func(raw string) string {
|
||||
newstr := make([]rune, 0, len(raw))
|
||||
for i, chr := range raw {
|
||||
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
|
||||
if i > 0 {
|
||||
newstr = append(newstr, '_')
|
||||
}
|
||||
}
|
||||
newstr = append(newstr, unicode.ToUpper(chr))
|
||||
}
|
||||
return string(newstr)
|
||||
}
|
||||
// TitleUnderscore converts to format title_underscore.
|
||||
TitleUnderscore NameMapper = func(raw string) string {
|
||||
newstr := make([]rune, 0, len(raw))
|
||||
for i, chr := range raw {
|
||||
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
|
||||
if i > 0 {
|
||||
newstr = append(newstr, '_')
|
||||
}
|
||||
chr -= 'A' - 'a'
|
||||
}
|
||||
newstr = append(newstr, chr)
|
||||
}
|
||||
return string(newstr)
|
||||
}
|
||||
)
|
||||
|
||||
func (s *Section) parseFieldName(raw, actual string) string {
|
||||
if len(actual) > 0 {
|
||||
return actual
|
||||
}
|
||||
if s.f.NameMapper != nil {
|
||||
return s.f.NameMapper(raw)
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
func parseDelim(actual string) string {
|
||||
if len(actual) > 0 {
|
||||
return actual
|
||||
}
|
||||
return ","
|
||||
}
|
||||
|
||||
var reflectTime = reflect.TypeOf(time.Now()).Kind()
|
||||
|
||||
// setSliceWithProperType sets proper values to slice based on its type.
|
||||
func setSliceWithProperType(key *Key, field reflect.Value, delim string, allowShadow, isStrict bool) error {
|
||||
var strs []string
|
||||
if allowShadow {
|
||||
strs = key.StringsWithShadows(delim)
|
||||
} else {
|
||||
strs = key.Strings(delim)
|
||||
}
|
||||
|
||||
numVals := len(strs)
|
||||
if numVals == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var vals interface{}
|
||||
var err error
|
||||
|
||||
sliceOf := field.Type().Elem().Kind()
|
||||
switch sliceOf {
|
||||
case reflect.String:
|
||||
vals = strs
|
||||
case reflect.Int:
|
||||
vals, err = key.parseInts(strs, true, false)
|
||||
case reflect.Int64:
|
||||
vals, err = key.parseInt64s(strs, true, false)
|
||||
case reflect.Uint:
|
||||
vals, err = key.parseUints(strs, true, false)
|
||||
case reflect.Uint64:
|
||||
vals, err = key.parseUint64s(strs, true, false)
|
||||
case reflect.Float64:
|
||||
vals, err = key.parseFloat64s(strs, true, false)
|
||||
case reflect.Bool:
|
||||
vals, err = key.parseBools(strs, true, false)
|
||||
case reflectTime:
|
||||
vals, err = key.parseTimesFormat(time.RFC3339, strs, true, false)
|
||||
default:
|
||||
return fmt.Errorf("unsupported type '[]%s'", sliceOf)
|
||||
}
|
||||
if err != nil && isStrict {
|
||||
return err
|
||||
}
|
||||
|
||||
slice := reflect.MakeSlice(field.Type(), numVals, numVals)
|
||||
for i := 0; i < numVals; i++ {
|
||||
switch sliceOf {
|
||||
case reflect.String:
|
||||
slice.Index(i).Set(reflect.ValueOf(vals.([]string)[i]))
|
||||
case reflect.Int:
|
||||
slice.Index(i).Set(reflect.ValueOf(vals.([]int)[i]))
|
||||
case reflect.Int64:
|
||||
slice.Index(i).Set(reflect.ValueOf(vals.([]int64)[i]))
|
||||
case reflect.Uint:
|
||||
slice.Index(i).Set(reflect.ValueOf(vals.([]uint)[i]))
|
||||
case reflect.Uint64:
|
||||
slice.Index(i).Set(reflect.ValueOf(vals.([]uint64)[i]))
|
||||
case reflect.Float64:
|
||||
slice.Index(i).Set(reflect.ValueOf(vals.([]float64)[i]))
|
||||
case reflect.Bool:
|
||||
slice.Index(i).Set(reflect.ValueOf(vals.([]bool)[i]))
|
||||
case reflectTime:
|
||||
slice.Index(i).Set(reflect.ValueOf(vals.([]time.Time)[i]))
|
||||
}
|
||||
}
|
||||
field.Set(slice)
|
||||
return nil
|
||||
}
|
||||
|
||||
func wrapStrictError(err error, isStrict bool) error {
|
||||
if isStrict {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// setWithProperType sets proper value to field based on its type,
|
||||
// but it does not return error for failing parsing,
|
||||
// because we want to use default value that is already assigned to struct.
|
||||
func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim string, allowShadow, isStrict bool) error {
|
||||
vt := t
|
||||
isPtr := t.Kind() == reflect.Ptr
|
||||
if isPtr {
|
||||
vt = t.Elem()
|
||||
}
|
||||
switch vt.Kind() {
|
||||
case reflect.String:
|
||||
stringVal := key.String()
|
||||
if isPtr {
|
||||
field.Set(reflect.ValueOf(&stringVal))
|
||||
} else if len(stringVal) > 0 {
|
||||
field.SetString(key.String())
|
||||
}
|
||||
case reflect.Bool:
|
||||
boolVal, err := key.Bool()
|
||||
if err != nil {
|
||||
return wrapStrictError(err, isStrict)
|
||||
}
|
||||
if isPtr {
|
||||
field.Set(reflect.ValueOf(&boolVal))
|
||||
} else {
|
||||
field.SetBool(boolVal)
|
||||
}
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
// ParseDuration will not return err for `0`, so check the type name
|
||||
if vt.Name() == "Duration" {
|
||||
durationVal, err := key.Duration()
|
||||
if err != nil {
|
||||
if intVal, err := key.Int64(); err == nil {
|
||||
field.SetInt(intVal)
|
||||
return nil
|
||||
}
|
||||
return wrapStrictError(err, isStrict)
|
||||
}
|
||||
if isPtr {
|
||||
field.Set(reflect.ValueOf(&durationVal))
|
||||
} else if int64(durationVal) > 0 {
|
||||
field.Set(reflect.ValueOf(durationVal))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
intVal, err := key.Int64()
|
||||
if err != nil {
|
||||
return wrapStrictError(err, isStrict)
|
||||
}
|
||||
if isPtr {
|
||||
pv := reflect.New(t.Elem())
|
||||
pv.Elem().SetInt(intVal)
|
||||
field.Set(pv)
|
||||
} else {
|
||||
field.SetInt(intVal)
|
||||
}
|
||||
// byte is an alias for uint8, so supporting uint8 breaks support for byte
|
||||
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
durationVal, err := key.Duration()
|
||||
// Skip zero value
|
||||
if err == nil && uint64(durationVal) > 0 {
|
||||
if isPtr {
|
||||
field.Set(reflect.ValueOf(&durationVal))
|
||||
} else {
|
||||
field.Set(reflect.ValueOf(durationVal))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
uintVal, err := key.Uint64()
|
||||
if err != nil {
|
||||
return wrapStrictError(err, isStrict)
|
||||
}
|
||||
if isPtr {
|
||||
pv := reflect.New(t.Elem())
|
||||
pv.Elem().SetUint(uintVal)
|
||||
field.Set(pv)
|
||||
} else {
|
||||
field.SetUint(uintVal)
|
||||
}
|
||||
|
||||
case reflect.Float32, reflect.Float64:
|
||||
floatVal, err := key.Float64()
|
||||
if err != nil {
|
||||
return wrapStrictError(err, isStrict)
|
||||
}
|
||||
if isPtr {
|
||||
pv := reflect.New(t.Elem())
|
||||
pv.Elem().SetFloat(floatVal)
|
||||
field.Set(pv)
|
||||
} else {
|
||||
field.SetFloat(floatVal)
|
||||
}
|
||||
case reflectTime:
|
||||
timeVal, err := key.Time()
|
||||
if err != nil {
|
||||
return wrapStrictError(err, isStrict)
|
||||
}
|
||||
if isPtr {
|
||||
field.Set(reflect.ValueOf(&timeVal))
|
||||
} else {
|
||||
field.Set(reflect.ValueOf(timeVal))
|
||||
}
|
||||
case reflect.Slice:
|
||||
return setSliceWithProperType(key, field, delim, allowShadow, isStrict)
|
||||
default:
|
||||
return fmt.Errorf("unsupported type %q", t)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseTagOptions(tag string) (rawName string, omitEmpty bool, allowShadow bool, allowNonUnique bool, extends bool) {
|
||||
opts := strings.SplitN(tag, ",", 5)
|
||||
rawName = opts[0]
|
||||
for _, opt := range opts[1:] {
|
||||
omitEmpty = omitEmpty || (opt == "omitempty")
|
||||
allowShadow = allowShadow || (opt == "allowshadow")
|
||||
allowNonUnique = allowNonUnique || (opt == "nonunique")
|
||||
extends = extends || (opt == "extends")
|
||||
}
|
||||
return rawName, omitEmpty, allowShadow, allowNonUnique, extends
|
||||
}
|
||||
|
||||
// mapToField maps the given value to the matching field of the given section.
|
||||
// The sectionIndex is the index (if non unique sections are enabled) to which the value should be added.
|
||||
func (s *Section) mapToField(val reflect.Value, isStrict bool, sectionIndex int, sectionName string) error {
|
||||
if val.Kind() == reflect.Ptr {
|
||||
val = val.Elem()
|
||||
}
|
||||
typ := val.Type()
|
||||
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := val.Field(i)
|
||||
tpField := typ.Field(i)
|
||||
|
||||
tag := tpField.Tag.Get("ini")
|
||||
if tag == "-" {
|
||||
continue
|
||||
}
|
||||
|
||||
rawName, _, allowShadow, allowNonUnique, extends := parseTagOptions(tag)
|
||||
fieldName := s.parseFieldName(tpField.Name, rawName)
|
||||
if len(fieldName) == 0 || !field.CanSet() {
|
||||
continue
|
||||
}
|
||||
|
||||
isStruct := tpField.Type.Kind() == reflect.Struct
|
||||
isStructPtr := tpField.Type.Kind() == reflect.Ptr && tpField.Type.Elem().Kind() == reflect.Struct
|
||||
isAnonymousPtr := tpField.Type.Kind() == reflect.Ptr && tpField.Anonymous
|
||||
if isAnonymousPtr {
|
||||
field.Set(reflect.New(tpField.Type.Elem()))
|
||||
}
|
||||
|
||||
if extends && (isAnonymousPtr || (isStruct && tpField.Anonymous)) {
|
||||
if isStructPtr && field.IsNil() {
|
||||
field.Set(reflect.New(tpField.Type.Elem()))
|
||||
}
|
||||
fieldSection := s
|
||||
if rawName != "" {
|
||||
sectionName = s.name + s.f.options.ChildSectionDelimiter + rawName
|
||||
if secs, err := s.f.SectionsByName(sectionName); err == nil && sectionIndex < len(secs) {
|
||||
fieldSection = secs[sectionIndex]
|
||||
}
|
||||
}
|
||||
if err := fieldSection.mapToField(field, isStrict, sectionIndex, sectionName); err != nil {
|
||||
return fmt.Errorf("map to field %q: %v", fieldName, err)
|
||||
}
|
||||
} else if isAnonymousPtr || isStruct || isStructPtr {
|
||||
if secs, err := s.f.SectionsByName(fieldName); err == nil {
|
||||
if len(secs) <= sectionIndex {
|
||||
return fmt.Errorf("there are not enough sections (%d <= %d) for the field %q", len(secs), sectionIndex, fieldName)
|
||||
}
|
||||
// Only set the field to non-nil struct value if we have a section for it.
|
||||
// Otherwise, we end up with a non-nil struct ptr even though there is no data.
|
||||
if isStructPtr && field.IsNil() {
|
||||
field.Set(reflect.New(tpField.Type.Elem()))
|
||||
}
|
||||
if err = secs[sectionIndex].mapToField(field, isStrict, sectionIndex, fieldName); err != nil {
|
||||
return fmt.Errorf("map to field %q: %v", fieldName, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Map non-unique sections
|
||||
if allowNonUnique && tpField.Type.Kind() == reflect.Slice {
|
||||
newField, err := s.mapToSlice(fieldName, field, isStrict)
|
||||
if err != nil {
|
||||
return fmt.Errorf("map to slice %q: %v", fieldName, err)
|
||||
}
|
||||
|
||||
field.Set(newField)
|
||||
continue
|
||||
}
|
||||
|
||||
if key, err := s.GetKey(fieldName); err == nil {
|
||||
delim := parseDelim(tpField.Tag.Get("delim"))
|
||||
if err = setWithProperType(tpField.Type, key, field, delim, allowShadow, isStrict); err != nil {
|
||||
return fmt.Errorf("set field %q: %v", fieldName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// mapToSlice maps all sections with the same name and returns the new value.
|
||||
// The type of the Value must be a slice.
|
||||
func (s *Section) mapToSlice(secName string, val reflect.Value, isStrict bool) (reflect.Value, error) {
|
||||
secs, err := s.f.SectionsByName(secName)
|
||||
if err != nil {
|
||||
return reflect.Value{}, err
|
||||
}
|
||||
|
||||
typ := val.Type().Elem()
|
||||
for i, sec := range secs {
|
||||
elem := reflect.New(typ)
|
||||
if err = sec.mapToField(elem, isStrict, i, sec.name); err != nil {
|
||||
return reflect.Value{}, fmt.Errorf("map to field from section %q: %v", secName, err)
|
||||
}
|
||||
|
||||
val = reflect.Append(val, elem.Elem())
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
|
||||
// mapTo maps a section to object v.
|
||||
func (s *Section) mapTo(v interface{}, isStrict bool) error {
|
||||
typ := reflect.TypeOf(v)
|
||||
val := reflect.ValueOf(v)
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
typ = typ.Elem()
|
||||
val = val.Elem()
|
||||
} else {
|
||||
return errors.New("not a pointer to a struct")
|
||||
}
|
||||
|
||||
if typ.Kind() == reflect.Slice {
|
||||
newField, err := s.mapToSlice(s.name, val, isStrict)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
val.Set(newField)
|
||||
return nil
|
||||
}
|
||||
|
||||
return s.mapToField(val, isStrict, 0, s.name)
|
||||
}
|
||||
|
||||
// MapTo maps section to given struct.
|
||||
func (s *Section) MapTo(v interface{}) error {
|
||||
return s.mapTo(v, false)
|
||||
}
|
||||
|
||||
// StrictMapTo maps section to given struct in strict mode,
|
||||
// which returns all possible error including value parsing error.
|
||||
func (s *Section) StrictMapTo(v interface{}) error {
|
||||
return s.mapTo(v, true)
|
||||
}
|
||||
|
||||
// MapTo maps file to given struct.
|
||||
func (f *File) MapTo(v interface{}) error {
|
||||
return f.Section("").MapTo(v)
|
||||
}
|
||||
|
||||
// StrictMapTo maps file to given struct in strict mode,
|
||||
// which returns all possible error including value parsing error.
|
||||
func (f *File) StrictMapTo(v interface{}) error {
|
||||
return f.Section("").StrictMapTo(v)
|
||||
}
|
||||
|
||||
// MapToWithMapper maps data sources to given struct with name mapper.
|
||||
func MapToWithMapper(v interface{}, mapper NameMapper, source interface{}, others ...interface{}) error {
|
||||
cfg, err := Load(source, others...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg.NameMapper = mapper
|
||||
return cfg.MapTo(v)
|
||||
}
|
||||
|
||||
// StrictMapToWithMapper maps data sources to given struct with name mapper in strict mode,
|
||||
// which returns all possible error including value parsing error.
|
||||
func StrictMapToWithMapper(v interface{}, mapper NameMapper, source interface{}, others ...interface{}) error {
|
||||
cfg, err := Load(source, others...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg.NameMapper = mapper
|
||||
return cfg.StrictMapTo(v)
|
||||
}
|
||||
|
||||
// MapTo maps data sources to given struct.
|
||||
func MapTo(v, source interface{}, others ...interface{}) error {
|
||||
return MapToWithMapper(v, nil, source, others...)
|
||||
}
|
||||
|
||||
// StrictMapTo maps data sources to given struct in strict mode,
|
||||
// which returns all possible error including value parsing error.
|
||||
func StrictMapTo(v, source interface{}, others ...interface{}) error {
|
||||
return StrictMapToWithMapper(v, nil, source, others...)
|
||||
}
|
||||
|
||||
// reflectSliceWithProperType does the opposite thing as setSliceWithProperType.
|
||||
func reflectSliceWithProperType(key *Key, field reflect.Value, delim string, allowShadow bool) error {
|
||||
slice := field.Slice(0, field.Len())
|
||||
if field.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
sliceOf := field.Type().Elem().Kind()
|
||||
|
||||
if allowShadow {
|
||||
var keyWithShadows *Key
|
||||
for i := 0; i < field.Len(); i++ {
|
||||
var val string
|
||||
switch sliceOf {
|
||||
case reflect.String:
|
||||
val = slice.Index(i).String()
|
||||
case reflect.Int, reflect.Int64:
|
||||
val = fmt.Sprint(slice.Index(i).Int())
|
||||
case reflect.Uint, reflect.Uint64:
|
||||
val = fmt.Sprint(slice.Index(i).Uint())
|
||||
case reflect.Float64:
|
||||
val = fmt.Sprint(slice.Index(i).Float())
|
||||
case reflect.Bool:
|
||||
val = fmt.Sprint(slice.Index(i).Bool())
|
||||
case reflectTime:
|
||||
val = slice.Index(i).Interface().(time.Time).Format(time.RFC3339)
|
||||
default:
|
||||
return fmt.Errorf("unsupported type '[]%s'", sliceOf)
|
||||
}
|
||||
|
||||
if i == 0 {
|
||||
keyWithShadows = newKey(key.s, key.name, val)
|
||||
} else {
|
||||
_ = keyWithShadows.AddShadow(val)
|
||||
}
|
||||
}
|
||||
*key = *keyWithShadows
|
||||
return nil
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
for i := 0; i < field.Len(); i++ {
|
||||
switch sliceOf {
|
||||
case reflect.String:
|
||||
buf.WriteString(slice.Index(i).String())
|
||||
case reflect.Int, reflect.Int64:
|
||||
buf.WriteString(fmt.Sprint(slice.Index(i).Int()))
|
||||
case reflect.Uint, reflect.Uint64:
|
||||
buf.WriteString(fmt.Sprint(slice.Index(i).Uint()))
|
||||
case reflect.Float64:
|
||||
buf.WriteString(fmt.Sprint(slice.Index(i).Float()))
|
||||
case reflect.Bool:
|
||||
buf.WriteString(fmt.Sprint(slice.Index(i).Bool()))
|
||||
case reflectTime:
|
||||
buf.WriteString(slice.Index(i).Interface().(time.Time).Format(time.RFC3339))
|
||||
default:
|
||||
return fmt.Errorf("unsupported type '[]%s'", sliceOf)
|
||||
}
|
||||
buf.WriteString(delim)
|
||||
}
|
||||
key.SetValue(buf.String()[:buf.Len()-len(delim)])
|
||||
return nil
|
||||
}
|
||||
|
||||
// reflectWithProperType does the opposite thing as setWithProperType.
|
||||
func reflectWithProperType(t reflect.Type, key *Key, field reflect.Value, delim string, allowShadow bool) error {
|
||||
switch t.Kind() {
|
||||
case reflect.String:
|
||||
key.SetValue(field.String())
|
||||
case reflect.Bool:
|
||||
key.SetValue(fmt.Sprint(field.Bool()))
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
key.SetValue(fmt.Sprint(field.Int()))
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
key.SetValue(fmt.Sprint(field.Uint()))
|
||||
case reflect.Float32, reflect.Float64:
|
||||
key.SetValue(fmt.Sprint(field.Float()))
|
||||
case reflectTime:
|
||||
key.SetValue(fmt.Sprint(field.Interface().(time.Time).Format(time.RFC3339)))
|
||||
case reflect.Slice:
|
||||
return reflectSliceWithProperType(key, field, delim, allowShadow)
|
||||
case reflect.Ptr:
|
||||
if !field.IsNil() {
|
||||
return reflectWithProperType(t.Elem(), key, field.Elem(), delim, allowShadow)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unsupported type %q", t)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CR: copied from encoding/json/encode.go with modifications of time.Time support.
|
||||
// TODO: add more test coverage.
|
||||
func isEmptyValue(v reflect.Value) bool {
|
||||
switch v.Kind() {
|
||||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
||||
return v.Len() == 0
|
||||
case reflect.Bool:
|
||||
return !v.Bool()
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return v.Int() == 0
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return v.Uint() == 0
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return v.Float() == 0
|
||||
case reflect.Interface, reflect.Ptr:
|
||||
return v.IsNil()
|
||||
case reflectTime:
|
||||
t, ok := v.Interface().(time.Time)
|
||||
return ok && t.IsZero()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// StructReflector is the interface implemented by struct types that can extract themselves into INI objects.
|
||||
type StructReflector interface {
|
||||
ReflectINIStruct(*File) error
|
||||
}
|
||||
|
||||
func (s *Section) reflectFrom(val reflect.Value) error {
|
||||
if val.Kind() == reflect.Ptr {
|
||||
val = val.Elem()
|
||||
}
|
||||
typ := val.Type()
|
||||
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
if !val.Field(i).CanInterface() {
|
||||
continue
|
||||
}
|
||||
|
||||
field := val.Field(i)
|
||||
tpField := typ.Field(i)
|
||||
|
||||
tag := tpField.Tag.Get("ini")
|
||||
if tag == "-" {
|
||||
continue
|
||||
}
|
||||
|
||||
rawName, omitEmpty, allowShadow, allowNonUnique, extends := parseTagOptions(tag)
|
||||
if omitEmpty && isEmptyValue(field) {
|
||||
continue
|
||||
}
|
||||
|
||||
if r, ok := field.Interface().(StructReflector); ok {
|
||||
return r.ReflectINIStruct(s.f)
|
||||
}
|
||||
|
||||
fieldName := s.parseFieldName(tpField.Name, rawName)
|
||||
if len(fieldName) == 0 || !field.CanSet() {
|
||||
continue
|
||||
}
|
||||
|
||||
if extends && tpField.Anonymous && (tpField.Type.Kind() == reflect.Ptr || tpField.Type.Kind() == reflect.Struct) {
|
||||
if err := s.reflectFrom(field); err != nil {
|
||||
return fmt.Errorf("reflect from field %q: %v", fieldName, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if (tpField.Type.Kind() == reflect.Ptr && tpField.Type.Elem().Kind() == reflect.Struct) ||
|
||||
(tpField.Type.Kind() == reflect.Struct && tpField.Type.Name() != "Time") {
|
||||
// Note: The only error here is section doesn't exist.
|
||||
sec, err := s.f.GetSection(fieldName)
|
||||
if err != nil {
|
||||
// Note: fieldName can never be empty here, ignore error.
|
||||
sec, _ = s.f.NewSection(fieldName)
|
||||
}
|
||||
|
||||
// Add comment from comment tag
|
||||
if len(sec.Comment) == 0 {
|
||||
sec.Comment = tpField.Tag.Get("comment")
|
||||
}
|
||||
|
||||
if err = sec.reflectFrom(field); err != nil {
|
||||
return fmt.Errorf("reflect from field %q: %v", fieldName, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if allowNonUnique && tpField.Type.Kind() == reflect.Slice {
|
||||
slice := field.Slice(0, field.Len())
|
||||
if field.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
sliceOf := field.Type().Elem().Kind()
|
||||
|
||||
for i := 0; i < field.Len(); i++ {
|
||||
if sliceOf != reflect.Struct && sliceOf != reflect.Ptr {
|
||||
return fmt.Errorf("field %q is not a slice of pointer or struct", fieldName)
|
||||
}
|
||||
|
||||
sec, err := s.f.NewSection(fieldName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add comment from comment tag
|
||||
if len(sec.Comment) == 0 {
|
||||
sec.Comment = tpField.Tag.Get("comment")
|
||||
}
|
||||
|
||||
if err := sec.reflectFrom(slice.Index(i)); err != nil {
|
||||
return fmt.Errorf("reflect from field %q: %v", fieldName, err)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Note: Same reason as section.
|
||||
key, err := s.GetKey(fieldName)
|
||||
if err != nil {
|
||||
key, _ = s.NewKey(fieldName, "")
|
||||
}
|
||||
|
||||
// Add comment from comment tag
|
||||
if len(key.Comment) == 0 {
|
||||
key.Comment = tpField.Tag.Get("comment")
|
||||
}
|
||||
|
||||
delim := parseDelim(tpField.Tag.Get("delim"))
|
||||
if err = reflectWithProperType(tpField.Type, key, field, delim, allowShadow); err != nil {
|
||||
return fmt.Errorf("reflect field %q: %v", fieldName, err)
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReflectFrom reflects section from given struct. It overwrites existing ones.
|
||||
func (s *Section) ReflectFrom(v interface{}) error {
|
||||
typ := reflect.TypeOf(v)
|
||||
val := reflect.ValueOf(v)
|
||||
|
||||
if s.name != DefaultSection && s.f.options.AllowNonUniqueSections &&
|
||||
(typ.Kind() == reflect.Slice || typ.Kind() == reflect.Ptr) {
|
||||
// Clear sections to make sure none exists before adding the new ones
|
||||
s.f.DeleteSection(s.name)
|
||||
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
sec, err := s.f.NewSection(s.name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return sec.reflectFrom(val.Elem())
|
||||
}
|
||||
|
||||
slice := val.Slice(0, val.Len())
|
||||
sliceOf := val.Type().Elem().Kind()
|
||||
if sliceOf != reflect.Ptr {
|
||||
return fmt.Errorf("not a slice of pointers")
|
||||
}
|
||||
|
||||
for i := 0; i < slice.Len(); i++ {
|
||||
sec, err := s.f.NewSection(s.name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = sec.reflectFrom(slice.Index(i))
|
||||
if err != nil {
|
||||
return fmt.Errorf("reflect from %dth field: %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
val = val.Elem()
|
||||
} else {
|
||||
return errors.New("not a pointer to a struct")
|
||||
}
|
||||
|
||||
return s.reflectFrom(val)
|
||||
}
|
||||
|
||||
// ReflectFrom reflects file from given struct.
|
||||
func (f *File) ReflectFrom(v interface{}) error {
|
||||
return f.Section("").ReflectFrom(v)
|
||||
}
|
||||
|
||||
// ReflectFromWithMapper reflects data sources from given struct with name mapper.
|
||||
func ReflectFromWithMapper(cfg *File, v interface{}, mapper NameMapper) error {
|
||||
cfg.NameMapper = mapper
|
||||
return cfg.ReflectFrom(v)
|
||||
}
|
||||
|
||||
// ReflectFrom reflects data sources from given struct.
|
||||
func ReflectFrom(cfg *File, v interface{}) error {
|
||||
return ReflectFromWithMapper(cfg, v, nil)
|
||||
}
|
||||
+4
@@ -1,6 +1,10 @@
|
||||
Changes
|
||||
=======
|
||||
|
||||
v3.0.2 05 Dev 2025
|
||||
* Code changes mainly due to upgraded linter.
|
||||
* github.com/lestrrat-go/option upgraded to v2
|
||||
|
||||
v3.0.1 18 Aug 2025
|
||||
* Refresh() no longer requires the resource to be ready.
|
||||
|
||||
|
||||
+8
-6
@@ -51,6 +51,9 @@ type Client struct {
|
||||
// By default ALL urls are allowed. This may not be suitable for you if
|
||||
// are using this in a production environment. You are encouraged to specify
|
||||
// a whitelist using the `WithWhitelist` option.
|
||||
//
|
||||
// NOTE: In future versions, this function signature should be changed to
|
||||
// return an error to properly handle option parsing failures.
|
||||
func NewClient(options ...NewClientOption) *Client {
|
||||
//nolint:staticcheck
|
||||
var errSink ErrorSink = errsink.NewNop()
|
||||
@@ -63,19 +66,18 @@ func NewClient(options ...NewClientOption) *Client {
|
||||
defaultMaxInterval := DefaultMaxInterval
|
||||
|
||||
numWorkers := DefaultWorkers
|
||||
//nolint:forcetypeassert
|
||||
for _, option := range options {
|
||||
switch option.Ident() {
|
||||
case identHTTPClient{}:
|
||||
httpcl = option.Value().(HTTPClient)
|
||||
_ = option.Value(&httpcl)
|
||||
case identWorkers{}:
|
||||
numWorkers = option.Value().(int)
|
||||
_ = option.Value(&numWorkers)
|
||||
case identErrorSink{}:
|
||||
errSink = option.Value().(ErrorSink)
|
||||
_ = option.Value(&errSink)
|
||||
case identTraceSink{}:
|
||||
traceSink = option.Value().(TraceSink)
|
||||
_ = option.Value(&traceSink)
|
||||
case identWhitelist{}:
|
||||
wl = option.Value().(Whitelist)
|
||||
_ = option.Value(&wl)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -123,11 +123,12 @@ func (c *controller) Add(ctx context.Context, r Resource, options ...AddOption)
|
||||
c.traceSink.Put(ctx, fmt.Sprintf("httprc controller: START Add(%q)", r.URL()))
|
||||
defer c.traceSink.Put(ctx, fmt.Sprintf("httprc controller: END Add(%q)", r.URL()))
|
||||
waitReady := true
|
||||
//nolint:forcetypeassert
|
||||
for _, option := range options {
|
||||
switch option.Ident() {
|
||||
case identWaitReady{}:
|
||||
waitReady = option.(addOption).Value().(bool)
|
||||
if err := option.Value(&waitReady); err != nil {
|
||||
return fmt.Errorf(`httprc.Controller.Add: failed to parse WaitReady option: %w`, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ package httprc
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/lestrrat-go/option"
|
||||
"github.com/lestrrat-go/option/v2"
|
||||
)
|
||||
|
||||
type NewClientOption interface {
|
||||
|
||||
+13
-6
@@ -41,17 +41,24 @@ func NewResource[T any](s string, transformer Transformer[T], options ...NewReso
|
||||
var interval time.Duration
|
||||
minInterval := DefaultMinInterval
|
||||
maxInterval := DefaultMaxInterval
|
||||
//nolint:forcetypeassert
|
||||
for _, option := range options {
|
||||
switch option.Ident() {
|
||||
case identHTTPClient{}:
|
||||
httpcl = option.Value().(HTTPClient)
|
||||
if err := option.Value(&httpcl); err != nil {
|
||||
return nil, fmt.Errorf(`httprc.NewResource: failed to parse HTTPClient option: %w`, err)
|
||||
}
|
||||
case identMinimumInterval{}:
|
||||
minInterval = option.Value().(time.Duration)
|
||||
if err := option.Value(&minInterval); err != nil {
|
||||
return nil, fmt.Errorf(`httprc.NewResource: failed to parse MinimumInterval option: %w`, err)
|
||||
}
|
||||
case identMaximumInterval{}:
|
||||
maxInterval = option.Value().(time.Duration)
|
||||
if err := option.Value(&maxInterval); err != nil {
|
||||
return nil, fmt.Errorf(`httprc.NewResource: failed to parse MaximumInterval option: %w`, err)
|
||||
}
|
||||
case identConstantInterval{}:
|
||||
interval = option.Value().(time.Duration)
|
||||
if err := option.Value(&interval); err != nil {
|
||||
return nil, fmt.Errorf(`httprc.NewResource: failed to parse ConstantInterval option: %w`, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if transformer == nil {
|
||||
@@ -109,7 +116,7 @@ func (r *ResourceBase[T]) Ready(ctx context.Context) error {
|
||||
// returns `A` or `B` depending on the type of the resource. When accessing the
|
||||
// resource through the `httprc.Resource` interface, use this method to obtain the
|
||||
// stored value.
|
||||
func (r *ResourceBase[T]) Get(dst interface{}) error {
|
||||
func (r *ResourceBase[T]) Get(dst any) error {
|
||||
return blackmagic.AssignIfCompatible(dst, r.Resource())
|
||||
}
|
||||
|
||||
|
||||
+3
@@ -106,6 +106,9 @@ linters:
|
||||
- revive
|
||||
path: jwt/internal/types/
|
||||
text: "var-naming: avoid meaningless package names"
|
||||
- linters:
|
||||
- godoclint
|
||||
path: (^|/)internal/
|
||||
paths:
|
||||
- third_party$
|
||||
- builtin$
|
||||
|
||||
+50
@@ -4,6 +4,56 @@ Changes
|
||||
v3 has many incompatibilities with v2. To see the full list of differences between
|
||||
v2 and v3, please read the Changes-v3.md file (https://github.com/lestrrat-go/jwx/blob/develop/v3/Changes-v3.md)
|
||||
|
||||
v3.0.13 12 Jan 2026
|
||||
* [jwt] The `jwt.WithContext()` option is now properly being passed to `jws.Verify()` from
|
||||
`jwt.Parse()`.
|
||||
* [jwx] github.com/lestrrat-go/httprc/v3 has been upgraded to remove dependency on
|
||||
github.com/lestrrat-go/option (v1)
|
||||
* [jwk] `jwk.Clone()` has been fixed to properly work with private fields.
|
||||
|
||||
v3.0.12 20 Oct 2025
|
||||
* [jwe] As part of the next change, now per-recipient headers that are empty
|
||||
are no longer serialized in flattened JSON serialization.
|
||||
|
||||
* [jwe] Introduce `jwe.WithLegacyHeaderMerging(bool)` option to control header
|
||||
merging behavior in during JWE encryption. This only applies to flattened
|
||||
JSON serialization.
|
||||
|
||||
Previously, when using flattened JSON serialization (i.e. you specified
|
||||
JSON serialization via `jwe.WithJSON()` and only supplied one key), per-recipient
|
||||
headers were merged into the protected headers during encryption, and then
|
||||
were left to be included in the final serialization as-is. This caused duplicate
|
||||
headers to be present in both the protected headers and the per-recipient headers.
|
||||
|
||||
Since there may be users who rely on this behavior already, instead of changing the
|
||||
default behavior to fix this duplication, a new option to `jwe.Encrypt()` was added
|
||||
to allow clearing the per-recipient headers after merging to leave the `"headers"`
|
||||
field empty. This in effect makes the flattened JSON serialization more similar to
|
||||
the compact serialization, where there are no per-recipient headers present, and
|
||||
leaves the headers disjoint.
|
||||
|
||||
Note that in compact mode, there are no per-recipient headers and thus the
|
||||
headers need to be merged regardless. In full JSON serialization, we never
|
||||
merge the headers, so it is left up to the user to keep the headers disjoint.
|
||||
|
||||
* [jws] Calling the deprecated `jws.NewSigner()` function for the first time will cause
|
||||
legacy signers to be loaded automatically. Previously, you had to explicitly
|
||||
call `jws.Settings(jws.WithLegacySigners(true))` to enable legacy signers.
|
||||
|
||||
We incorrectly assumed that users would not be using `jws.NewSigner()`, and thus
|
||||
disabled legacy signers by default. However, it turned out that some users
|
||||
were using `jws.NewSigner()` in their code, which lead to breakages in
|
||||
existing code. In hindsight we should have known that any API made public before will
|
||||
be used by _somebody_.
|
||||
|
||||
As a side effect, jws.Settings(jws.WithLegacySigners(...)) is now a no-op.
|
||||
|
||||
However, please do note that jws.Signer (and similar) objects were always intended to be
|
||||
used for _registering_ new signing/verifying algorithms, and not for end users to actually
|
||||
use them directly. If you are using them for other purposes, please consider changing
|
||||
your code, as it is more than likely that we will somehow deprecate/remove/discouraged
|
||||
their use in the future.
|
||||
|
||||
v3.0.11 14 Sep 2025
|
||||
* [jwk] Add `(jwk.Cache).Shutdown()` method that delegates to the httprc controller
|
||||
object, to shutdown the cache.
|
||||
|
||||
+2
-2
@@ -9,9 +9,9 @@ bazel_dep(name = "rules_go", version = "0.55.1")
|
||||
bazel_dep(name = "gazelle", version = "0.44.0")
|
||||
bazel_dep(name = "aspect_bazel_lib", version = "2.11.0")
|
||||
|
||||
# Go SDK setup - using Go 1.24.4 to match the toolchain in go.mod
|
||||
# Go SDK setup from go.mod
|
||||
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
|
||||
go_sdk.download(version = "1.24.4")
|
||||
go_sdk.from_file(go_mod = "//:go.mod")
|
||||
|
||||
# Go dependencies from go.mod
|
||||
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
|
||||
|
||||
+3
-2
@@ -22,8 +22,9 @@ const _FormatKind_name = "InvalidFormatUnknownFormatJWEJWSJWKJWKSJWT"
|
||||
var _FormatKind_index = [...]uint8{0, 13, 26, 29, 32, 35, 39, 42}
|
||||
|
||||
func (i FormatKind) String() string {
|
||||
if i < 0 || i >= FormatKind(len(_FormatKind_index)-1) {
|
||||
idx := int(i) - 0
|
||||
if i < 0 || idx >= len(_FormatKind_index)-1 {
|
||||
return "FormatKind(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return _FormatKind_name[_FormatKind_index[i]:_FormatKind_index[i+1]]
|
||||
return _FormatKind_name[_FormatKind_index[idx]:_FormatKind_index[idx+1]]
|
||||
}
|
||||
|
||||
-1
@@ -1,5 +1,4 @@
|
||||
//go:build jwx_goccy
|
||||
// +build jwx_goccy
|
||||
|
||||
package json
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
//go:build !jwx_goccy
|
||||
// +build !jwx_goccy
|
||||
|
||||
//nolint:revive
|
||||
package json
|
||||
|
||||
import (
|
||||
|
||||
-1
@@ -1,5 +1,4 @@
|
||||
//go:build jwx_es256k
|
||||
// +build jwx_es256k
|
||||
|
||||
package jwa
|
||||
|
||||
|
||||
+1
-3
@@ -72,9 +72,7 @@ func extractPadding(payload []byte) (toRemove int, good byte) {
|
||||
// The maximum possible padding length plus the actual length field
|
||||
toCheck := 256
|
||||
// The length of the padded data is public, so we can use an if here
|
||||
if toCheck > len(payload) {
|
||||
toCheck = len(payload)
|
||||
}
|
||||
toCheck = min(toCheck, len(payload))
|
||||
|
||||
for i := 1; i <= toCheck; i++ {
|
||||
t := uint(paddingLen) - uint(i)
|
||||
|
||||
+102
-20
@@ -99,16 +99,21 @@ func (b *recipientBuilder) Build(r Recipient, cek []byte, calg jwa.ContentEncryp
|
||||
rawKey = raw
|
||||
}
|
||||
|
||||
// Extract ECDH-ES specific parameters if needed
|
||||
// Extract ECDH-ES specific parameters if needed.
|
||||
var apu, apv []byte
|
||||
if b.headers != nil {
|
||||
if val, ok := b.headers.AgreementPartyUInfo(); ok {
|
||||
|
||||
hdr := b.headers
|
||||
if hdr == nil {
|
||||
hdr = NewHeaders()
|
||||
}
|
||||
|
||||
if val, ok := hdr.AgreementPartyUInfo(); ok {
|
||||
apu = val
|
||||
}
|
||||
if val, ok := b.headers.AgreementPartyVInfo(); ok {
|
||||
|
||||
if val, ok := hdr.AgreementPartyVInfo(); ok {
|
||||
apv = val
|
||||
}
|
||||
}
|
||||
|
||||
// Create the encrypter using the new jwebb pattern
|
||||
enc, err := newEncrypter(b.alg, calg, b.key, rawKey, apu, apv)
|
||||
@@ -116,20 +121,20 @@ func (b *recipientBuilder) Build(r Recipient, cek []byte, calg jwa.ContentEncryp
|
||||
return nil, fmt.Errorf(`jwe.Encrypt: recipientBuilder: failed to create encrypter: %w`, err)
|
||||
}
|
||||
|
||||
if hdrs := b.headers; hdrs != nil {
|
||||
_ = r.SetHeaders(hdrs)
|
||||
}
|
||||
_ = r.SetHeaders(hdr)
|
||||
|
||||
if err := r.Headers().Set(AlgorithmKey, b.alg); err != nil {
|
||||
// Populate headers with stuff that we automatically set
|
||||
if err := hdr.Set(AlgorithmKey, b.alg); err != nil {
|
||||
return nil, fmt.Errorf(`failed to set header: %w`, err)
|
||||
}
|
||||
|
||||
if keyID != "" {
|
||||
if err := r.Headers().Set(KeyIDKey, keyID); err != nil {
|
||||
if err := hdr.Set(KeyIDKey, keyID); err != nil {
|
||||
return nil, fmt.Errorf(`failed to set header: %w`, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle the encrypted key
|
||||
var rawCEK []byte
|
||||
enckey, err := enc.EncryptKey(cek)
|
||||
if err != nil {
|
||||
@@ -143,8 +148,9 @@ func (b *recipientBuilder) Build(r Recipient, cek []byte, calg jwa.ContentEncryp
|
||||
}
|
||||
}
|
||||
|
||||
// finally, anything specific should go here
|
||||
if hp, ok := enckey.(populater); ok {
|
||||
if err := hp.Populate(r.Headers()); err != nil {
|
||||
if err := hp.Populate(hdr); err != nil {
|
||||
return nil, fmt.Errorf(`failed to populate: %w`, err)
|
||||
}
|
||||
}
|
||||
@@ -154,7 +160,9 @@ func (b *recipientBuilder) Build(r Recipient, cek []byte, calg jwa.ContentEncryp
|
||||
|
||||
// Encrypt generates a JWE message for the given payload and returns
|
||||
// it in serialized form, which can be in either compact or
|
||||
// JSON format. Default is compact.
|
||||
// JSON format. Default is compact. When JSON format is specified and
|
||||
// there is only one recipient, the resulting serialization is
|
||||
// automatically converted to flattened JSON serialization format.
|
||||
//
|
||||
// You must pass at least one key to `jwe.Encrypt()` by using `jwe.WithKey()`
|
||||
// option.
|
||||
@@ -172,6 +180,10 @@ func (b *recipientBuilder) Build(r Recipient, cek []byte, calg jwa.ContentEncryp
|
||||
//
|
||||
// Look for options that return `jwe.EncryptOption` or `jws.EncryptDecryptOption`
|
||||
// for a complete list of options that can be passed to this function.
|
||||
//
|
||||
// As of v3.0.12, users can specify `jwe.WithLegacyHeaderMerging()` to
|
||||
// disable header merging behavior that was the default prior to v3.0.12.
|
||||
// Read the documentation for `jwe.WithLegacyHeaderMerging()` for more information.
|
||||
func Encrypt(payload []byte, options ...EncryptOption) ([]byte, error) {
|
||||
ec := encryptContextPool.Get()
|
||||
defer encryptContextPool.Put(ec)
|
||||
@@ -410,10 +422,26 @@ func (dc *decryptContext) decryptContent(msg *Message, alg jwa.KeyEncryptionAlgo
|
||||
Tag(msg.tag).
|
||||
CEK(dc.cek)
|
||||
|
||||
if v, ok := recipient.Headers().Algorithm(); !ok || v != alg {
|
||||
// algorithms don't match
|
||||
// The "alg" header can be in either protected/unprotected headers.
|
||||
// prefer per-recipient headers (as it might be the case that the algorithm differs
|
||||
// by each recipient), then look at protected headers.
|
||||
var algMatched bool
|
||||
for _, hdr := range []Headers{recipient.Headers(), protectedHeaders} {
|
||||
v, ok := hdr.Algorithm()
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if v == alg {
|
||||
algMatched = true
|
||||
break
|
||||
}
|
||||
// if we found something but didn't match, it's a failure
|
||||
return nil, fmt.Errorf(`jwe.Decrypt: key (%q) and recipient (%q) algorithms do not match`, alg, v)
|
||||
}
|
||||
if !algMatched {
|
||||
return nil, fmt.Errorf(`jwe.Decrypt: failed to find "alg" header in either protected or per-recipient headers`)
|
||||
}
|
||||
|
||||
h2, err := protectedHeaders.Clone()
|
||||
if err != nil {
|
||||
@@ -539,6 +567,7 @@ type encryptContext struct {
|
||||
format int
|
||||
builders []*recipientBuilder
|
||||
protected Headers
|
||||
legacyHeaderMerging bool
|
||||
}
|
||||
|
||||
var encryptContextPool = pool.New(allocEncryptContext, freeEncryptContext)
|
||||
@@ -561,6 +590,7 @@ func freeEncryptContext(ec *encryptContext) *encryptContext {
|
||||
}
|
||||
|
||||
func (ec *encryptContext) ProcessOptions(options []EncryptOption) error {
|
||||
ec.legacyHeaderMerging = true
|
||||
var mergeProtected bool
|
||||
var useRawCEK bool
|
||||
for _, option := range options {
|
||||
@@ -577,7 +607,11 @@ func (ec *encryptContext) ProcessOptions(options []EncryptOption) error {
|
||||
if v == jwa.DIRECT() || v == jwa.ECDH_ES() {
|
||||
useRawCEK = true
|
||||
}
|
||||
ec.builders = append(ec.builders, &recipientBuilder{alg: v, key: wk.key, headers: wk.headers})
|
||||
ec.builders = append(ec.builders, &recipientBuilder{
|
||||
alg: v,
|
||||
key: wk.key,
|
||||
headers: wk.headers,
|
||||
})
|
||||
case identContentEncryptionAlgorithm{}:
|
||||
var c jwa.ContentEncryptionAlgorithm
|
||||
if err := option.Value(&c); err != nil {
|
||||
@@ -616,6 +650,12 @@ func (ec *encryptContext) ProcessOptions(options []EncryptOption) error {
|
||||
return err
|
||||
}
|
||||
ec.format = fmtOpt
|
||||
case identLegacyHeaderMerging{}:
|
||||
var v bool
|
||||
if err := option.Value(&v); err != nil {
|
||||
return err
|
||||
}
|
||||
ec.legacyHeaderMerging = v
|
||||
}
|
||||
}
|
||||
|
||||
@@ -732,7 +772,8 @@ func (ec *encryptContext) EncryptMessage(payload []byte, cek []byte) ([]byte, er
|
||||
}
|
||||
}
|
||||
|
||||
recipients := recipientSlicePool.GetCapacity(len(ec.builders))
|
||||
lbuilders := len(ec.builders)
|
||||
recipients := recipientSlicePool.GetCapacity(lbuilders)
|
||||
defer recipientSlicePool.Put(recipients)
|
||||
|
||||
for i, builder := range ec.builders {
|
||||
@@ -767,14 +808,55 @@ func (ec *encryptContext) EncryptMessage(payload []byte, cek []byte) ([]byte, er
|
||||
}
|
||||
}
|
||||
|
||||
// If there's only one recipient, you want to include that in the
|
||||
// protected header
|
||||
if len(recipients) == 1 {
|
||||
// fmtCompact does not have per-recipient headers, nor a "header" field.
|
||||
// In this mode, we're going to have to merge everything to the protected
|
||||
// header.
|
||||
if ec.format == fmtCompact {
|
||||
// We have already established that the number of builders is 1 in
|
||||
// ec.ProcessOptions(). But we're going to be pedantic
|
||||
if lbuilders != 1 {
|
||||
return nil, fmt.Errorf(`internal error: expected exactly one recipient builder (got %d)`, lbuilders)
|
||||
}
|
||||
|
||||
// when we're using compact format, we can safely merge per-recipient
|
||||
// headers into the protected header, if any
|
||||
h, err := protected.Merge(recipients[0].Headers())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`failed to merge protected headers: %w`, err)
|
||||
return nil, fmt.Errorf(`failed to merge protected headers for compact serialization: %w`, err)
|
||||
}
|
||||
protected = h
|
||||
// per-recipient headers, if any, will be ignored in compact format
|
||||
} else {
|
||||
// If it got here, it's JSON (could be pretty mode, too).
|
||||
if lbuilders == 1 {
|
||||
// If it got here, then we're doing flattened JSON serialization.
|
||||
// In this mode, we should merge per-recipient headers into the protected header,
|
||||
// but we also need to make sure that the "header" field is reset so that
|
||||
// it does not contain the same fields as the protected header.
|
||||
//
|
||||
// However, old behavior was to merge per-recipient headers into the
|
||||
// protected header when there was only one recipient, AND leave the
|
||||
// original "header" field as is, so we need to support that for backwards compatibility.
|
||||
//
|
||||
// The legacy merging only takes effect when there is exactly one recipient.
|
||||
//
|
||||
// This behavior can be disabled by passing jwe.WithLegacyHeaderMerging(false)
|
||||
// If the user has explicitly asked for merging, do it
|
||||
h, err := protected.Merge(recipients[0].Headers())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`failed to merge protected headers for flattenend JSON format: %w`, err)
|
||||
}
|
||||
protected = h
|
||||
|
||||
if !ec.legacyHeaderMerging {
|
||||
// Clear per-recipient headers, since they have been merged.
|
||||
// But we only do it when legacy merging is disabled.
|
||||
// Note: we should probably introduce a Reset() method in v4
|
||||
if err := recipients[0].SetHeaders(NewHeaders()); err != nil {
|
||||
return nil, fmt.Errorf(`failed to clear per-recipient headers after merging: %w`, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
aad, err := protected.Encode()
|
||||
|
||||
+14
@@ -265,6 +265,14 @@ func (m *Message) MarshalJSON() ([]byte, error) {
|
||||
if recipients := m.Recipients(); len(recipients) > 0 {
|
||||
if len(recipients) == 1 { // Use flattened format
|
||||
if hdrs := recipients[0].Headers(); hdrs != nil {
|
||||
var skipHeaders bool
|
||||
if zeroer, ok := hdrs.(isZeroer); ok {
|
||||
if zeroer.isZero() {
|
||||
skipHeaders = true
|
||||
}
|
||||
}
|
||||
|
||||
if !skipHeaders {
|
||||
buf.Reset()
|
||||
if err := enc.Encode(hdrs); err != nil {
|
||||
return nil, fmt.Errorf(`failed to encode %s field: %w`, HeadersKey, err)
|
||||
@@ -274,6 +282,7 @@ func (m *Message) MarshalJSON() ([]byte, error) {
|
||||
Value: strings.TrimSpace(buf.String()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if ek := recipients[0].EncryptedKey(); len(ek) > 0 {
|
||||
buf.Reset()
|
||||
@@ -369,6 +378,10 @@ func (m *Message) UnmarshalJSON(buf []byte) error {
|
||||
// field. TODO: do both of these conditions need to meet, or just one?
|
||||
if proxy.Headers != nil || len(proxy.EncryptedKey) > 0 {
|
||||
recipient := NewRecipient()
|
||||
|
||||
// `"heders"` could be empty. If that's the case, just skip the
|
||||
// following unmarshaling step
|
||||
if proxy.Headers != nil {
|
||||
hdrs := NewHeaders()
|
||||
if err := json.Unmarshal(proxy.Headers, hdrs); err != nil {
|
||||
return fmt.Errorf(`failed to decode headers field: %w`, err)
|
||||
@@ -377,6 +390,7 @@ func (m *Message) UnmarshalJSON(buf []byte) error {
|
||||
if err := recipient.SetHeaders(hdrs); err != nil {
|
||||
return fmt.Errorf(`failed to set new headers: %w`, err)
|
||||
}
|
||||
}
|
||||
|
||||
if v := proxy.EncryptedKey; len(v) > 0 {
|
||||
buf, err := base64.DecodeString(v)
|
||||
|
||||
+3
-2
@@ -6,8 +6,9 @@ import (
|
||||
"github.com/lestrrat-go/option/v2"
|
||||
)
|
||||
|
||||
// Specify contents of the protected header. Some fields such as
|
||||
// "enc" and "zip" will be overwritten when encryption is performed.
|
||||
// WithProtectedHeaders is used to specify contents of the protected header.
|
||||
// Some fields such as "enc" and "zip" will be overwritten when encryption is
|
||||
// performed.
|
||||
//
|
||||
// There is no equivalent for unprotected headers in this implementation
|
||||
func WithProtectedHeaders(h Headers) EncryptOption {
|
||||
|
||||
+38
@@ -170,3 +170,41 @@ options:
|
||||
In v2, this option was called MaxBufferSize.
|
||||
|
||||
This option has a global effect.
|
||||
- ident: LegacyHeaderMerging
|
||||
interface: EncryptOption
|
||||
argument_type: bool
|
||||
option_name: WithLegacyHeaderMerging
|
||||
comment: |
|
||||
WithLegacyHeaderMerging specifies whether to perform legacy header merging
|
||||
when encrypting a JWE message in JSON serialization, when there is a single recipient.
|
||||
This behavior is enabled by default for backwards compatibility.
|
||||
|
||||
When a JWE message is encrypted in JSON serialization, and there is only
|
||||
one recipient, this library automatically serializes the message in
|
||||
flattened JSON serialization format. In older versions of this library,
|
||||
the protected headers and the per-recipient headers were merged together
|
||||
before computing the AAD (Additional Authenticated Data), but the per-recipient
|
||||
headers were kept as-is in the `header` field of the recipient object.
|
||||
|
||||
This behavior is not compliant with the JWE specification, which states that
|
||||
the headers must be disjoint.
|
||||
|
||||
Passing this option with a value of `false` disables this legacy behavior,
|
||||
and while the per-recipient headers and protected headers are still merged
|
||||
for the purpose of computing AAD, the per-recipient headers are cleared
|
||||
after merging, so that the resulting JWE message is compliant with the
|
||||
specification.
|
||||
|
||||
This option has no effect when there are multiple recipients, or when
|
||||
the serialization format is compact serialization. For multiple recipients
|
||||
(i.e. full JSON serialization), the protected headers and per-recipient
|
||||
headers are never merged, and it is the caller's responsibility to ensure
|
||||
that the headers are disjoint. In compact serialization, there are no per-recipient
|
||||
headers; in fact, the protected headers are the only headers that exist,
|
||||
and therefore there is no possibility of header collision after merging
|
||||
(note: while per-recipient headers do not make sense in compact serialization,
|
||||
this library does not prevent you from setting them -- they are all just
|
||||
merged into the protected headers).
|
||||
|
||||
In future versions, the new behavior will be the default. New users are
|
||||
encouraged to set this option to `false` now to avoid future issues.
|
||||
+42
@@ -147,6 +147,7 @@ type identFS struct{}
|
||||
type identKey struct{}
|
||||
type identKeyProvider struct{}
|
||||
type identKeyUsed struct{}
|
||||
type identLegacyHeaderMerging struct{}
|
||||
type identMaxDecompressBufferSize struct{}
|
||||
type identMaxPBES2Count struct{}
|
||||
type identMergeProtectedHeaders struct{}
|
||||
@@ -193,6 +194,10 @@ func (identKeyUsed) String() string {
|
||||
return "WithKeyUsed"
|
||||
}
|
||||
|
||||
func (identLegacyHeaderMerging) String() string {
|
||||
return "WithLegacyHeaderMerging"
|
||||
}
|
||||
|
||||
func (identMaxDecompressBufferSize) String() string {
|
||||
return "WithMaxDecompressBufferSize"
|
||||
}
|
||||
@@ -292,6 +297,43 @@ func WithKeyUsed(v any) DecryptOption {
|
||||
return &decryptOption{option.New(identKeyUsed{}, v)}
|
||||
}
|
||||
|
||||
// WithLegacyHeaderMerging specifies whether to perform legacy header merging
|
||||
// when encrypting a JWE message in JSON serialization, when there is a single recipient.
|
||||
// This behavior is enabled by default for backwards compatibility.
|
||||
//
|
||||
// When a JWE message is encrypted in JSON serialization, and there is only
|
||||
// one recipient, this library automatically serializes the message in
|
||||
// flattened JSON serialization format. In older versions of this library,
|
||||
// the protected headers and the per-recipient headers were merged together
|
||||
// before computing the AAD (Additional Authenticated Data), but the per-recipient
|
||||
// headers were kept as-is in the `header` field of the recipient object.
|
||||
//
|
||||
// This behavior is not compliant with the JWE specification, which states that
|
||||
// the headers must be disjoint.
|
||||
//
|
||||
// Passing this option with a value of `false` disables this legacy behavior,
|
||||
// and while the per-recipient headers and protected headers are still merged
|
||||
// for the purpose of computing AAD, the per-recipient headers are cleared
|
||||
// after merging, so that the resulting JWE message is compliant with the
|
||||
// specification.
|
||||
//
|
||||
// This option has no effect when there are multiple recipients, or when
|
||||
// the serialization format is compact serialization. For multiple recipients
|
||||
// (i.e. full JSON serialization), the protected headers and per-recipient
|
||||
// headers are never merged, and it is the caller's responsibility to ensure
|
||||
// that the headers are disjoint. In compact serialization, there are no per-recipient
|
||||
// headers; in fact, the protected headers are the only headers that exist,
|
||||
// and therefore there is no possibility of header collision after merging
|
||||
// (note: while per-recipient headers do not make sense in compact serialization,
|
||||
// this library does not prevent you from setting them -- they are all just
|
||||
// merged into the protected headers).
|
||||
//
|
||||
// In future versions, the new behavior will be the default. New users are
|
||||
// encouraged to set this option to `false` now to avoid future issues.
|
||||
func WithLegacyHeaderMerging(v bool) EncryptOption {
|
||||
return &encryptOption{option.New(identLegacyHeaderMerging{}, v)}
|
||||
}
|
||||
|
||||
// WithMaxDecompressBufferSize specifies the maximum buffer size for used when
|
||||
// decompressing the payload of a JWE message. If a compressed JWE payload
|
||||
// exceeds this amount when decompressed, jwe.Decrypt will return an error.
|
||||
|
||||
+1
-1
@@ -270,7 +270,7 @@ func (cs *cachedSet) cached() (Set, error) {
|
||||
return cs.r.Resource(), nil
|
||||
}
|
||||
|
||||
// Add is a no-op for `jwk.CachedSet`, as the `jwk.Set` should be treated read-only
|
||||
// AddKey is a no-op for `jwk.CachedSet`, as the `jwk.Set` should be treated read-only
|
||||
func (*cachedSet) AddKey(_ Key) error {
|
||||
return fmt.Errorf(`(jwk.Cachedset).AddKey: jwk.CachedSet is immutable`)
|
||||
}
|
||||
|
||||
+2
-2
@@ -141,8 +141,8 @@ func buildECDHPrivateKey(alg jwa.EllipticCurveAlgorithm, dbuf []byte) (*ecdh.Pri
|
||||
}
|
||||
|
||||
var ecdsaConvertibleTypes = []reflect.Type{
|
||||
reflect.TypeOf((*ECDSAPrivateKey)(nil)).Elem(),
|
||||
reflect.TypeOf((*ECDSAPublicKey)(nil)).Elem(),
|
||||
reflect.TypeFor[ECDSAPrivateKey](),
|
||||
reflect.TypeFor[ECDSAPublicKey](),
|
||||
}
|
||||
|
||||
func ecdsaJWKToRaw(keyif Key, hint any) (any, error) {
|
||||
|
||||
-1
@@ -1,5 +1,4 @@
|
||||
//go:build jwx_es256k
|
||||
// +build jwx_es256k
|
||||
|
||||
package jwk
|
||||
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ type CachedFetcher struct {
|
||||
cache *Cache
|
||||
}
|
||||
|
||||
// Creates a new `jwk.CachedFetcher` object.
|
||||
// NewCachedFetcher creates a new `jwk.CachedFetcher` object.
|
||||
func NewCachedFetcher(cache *Cache) *CachedFetcher {
|
||||
return &CachedFetcher{cache}
|
||||
}
|
||||
|
||||
+6
-1
@@ -92,9 +92,14 @@ type Set interface {
|
||||
Len() int
|
||||
|
||||
// LookupKeyID returns the first key matching the given key id.
|
||||
//
|
||||
// The second return value is false if there are no keys matching the key id.
|
||||
// The set *may* contain multiple keys with the same key id. If you
|
||||
// need all of them, use `Iterate()`
|
||||
// need all of them, Len() and Key(int)
|
||||
//
|
||||
// This method is meant to be used to lookup a key with a unique ID.
|
||||
// Bacauseof this, you cannot use this method to lookup keys with an empty key ID
|
||||
// (i.e. `kid` is not specified, or is an empty string).
|
||||
LookupKeyID(string) (Key, bool)
|
||||
|
||||
// RemoveKey removes the key from the set.
|
||||
|
||||
+6
-5
@@ -13,6 +13,7 @@ import (
|
||||
"io"
|
||||
"math/big"
|
||||
"reflect"
|
||||
"slices"
|
||||
|
||||
"github.com/lestrrat-go/jwx/v3/internal/base64"
|
||||
"github.com/lestrrat-go/jwx/v3/internal/json"
|
||||
@@ -30,14 +31,14 @@ func bigIntToBytes(n *big.Int) ([]byte, error) {
|
||||
func init() {
|
||||
if err := RegisterProbeField(reflect.StructField{
|
||||
Name: "Kty",
|
||||
Type: reflect.TypeOf(""),
|
||||
Type: reflect.TypeFor[string](),
|
||||
Tag: `json:"kty"`,
|
||||
}); err != nil {
|
||||
panic(fmt.Errorf("failed to register mandatory probe for 'kty' field: %w", err))
|
||||
}
|
||||
if err := RegisterProbeField(reflect.StructField{
|
||||
Name: "D",
|
||||
Type: reflect.TypeOf(json.RawMessage(nil)),
|
||||
Type: reflect.TypeFor[json.RawMessage](),
|
||||
Tag: `json:"d,omitempty"`,
|
||||
}); err != nil {
|
||||
panic(fmt.Errorf("failed to register mandatory probe for 'kty' field: %w", err))
|
||||
@@ -665,11 +666,11 @@ func extractEmbeddedKey(keyif Key, concretTypes []reflect.Type) (Key, error) {
|
||||
rv := reflect.ValueOf(keyif)
|
||||
|
||||
// If the value can be converted to one of the concrete types, then we're done
|
||||
for _, t := range concretTypes {
|
||||
if rv.Type().ConvertibleTo(t) {
|
||||
if slices.ContainsFunc(concretTypes, func(t reflect.Type) bool {
|
||||
return rv.Type().ConvertibleTo(t)
|
||||
}) {
|
||||
return keyif, nil
|
||||
}
|
||||
}
|
||||
|
||||
// When a struct implements the Key interface via embedding, you unfortunately
|
||||
// cannot use a type switch to determine the concrete type, because
|
||||
|
||||
+2
-2
@@ -141,8 +141,8 @@ func buildOKPPrivateKey(alg jwa.EllipticCurveAlgorithm, xbuf []byte, dbuf []byte
|
||||
}
|
||||
|
||||
var okpConvertibleKeys = []reflect.Type{
|
||||
reflect.TypeOf((*OKPPrivateKey)(nil)).Elem(),
|
||||
reflect.TypeOf((*OKPPublicKey)(nil)).Elem(),
|
||||
reflect.TypeFor[OKPPrivateKey](),
|
||||
reflect.TypeFor[OKPPublicKey](),
|
||||
}
|
||||
|
||||
// This is half baked. I think it will blow up if we used ecdh.* keys and/or x25519 keys
|
||||
|
||||
+2
-2
@@ -115,8 +115,8 @@ func buildRSAPublicKey(key *rsa.PublicKey, n, e []byte) {
|
||||
}
|
||||
|
||||
var rsaConvertibleKeys = []reflect.Type{
|
||||
reflect.TypeOf((*RSAPrivateKey)(nil)).Elem(),
|
||||
reflect.TypeOf((*RSAPublicKey)(nil)).Elem(),
|
||||
reflect.TypeFor[RSAPrivateKey](),
|
||||
reflect.TypeFor[RSAPublicKey](),
|
||||
}
|
||||
|
||||
func rsaJWKToRaw(key Key, hint any) (any, error) {
|
||||
|
||||
+11
-3
@@ -3,6 +3,7 @@ package jwk
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"maps"
|
||||
"reflect"
|
||||
"sort"
|
||||
|
||||
@@ -14,13 +15,17 @@ import (
|
||||
|
||||
const keysKey = `keys` // appease linter
|
||||
|
||||
// NewSet creates and empty `jwk.Set` object
|
||||
func NewSet() Set {
|
||||
func newSet() *set {
|
||||
return &set{
|
||||
privateParams: make(map[string]any),
|
||||
}
|
||||
}
|
||||
|
||||
// NewSet creates and empty `jwk.Set` object
|
||||
func NewSet() Set {
|
||||
return newSet()
|
||||
}
|
||||
|
||||
func (s *set) Set(n string, v any) error {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
@@ -300,12 +305,15 @@ func (s *set) SetDecodeCtx(dc DecodeCtx) {
|
||||
}
|
||||
|
||||
func (s *set) Clone() (Set, error) {
|
||||
s2 := &set{}
|
||||
s2 := newSet()
|
||||
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
s2.keys = make([]Key, len(s.keys))
|
||||
copy(s2.keys, s.keys)
|
||||
|
||||
maps.Copy(s2.privateParams, s.privateParams)
|
||||
|
||||
return s2, nil
|
||||
}
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ func (k *symmetricKey) Import(rawKey []byte) error {
|
||||
}
|
||||
|
||||
var symmetricConvertibleKeys = []reflect.Type{
|
||||
reflect.TypeOf((*SymmetricKey)(nil)).Elem(),
|
||||
reflect.TypeFor[SymmetricKey](),
|
||||
}
|
||||
|
||||
func octetSeqToRaw(key Key, hint any) (any, error) {
|
||||
|
||||
+1
-1
@@ -118,7 +118,7 @@ func NewPEMDecoder() PEMDecoder {
|
||||
|
||||
type pemDecoder struct{}
|
||||
|
||||
// DecodePEM decodes a key in PEM encoded ASN.1 DER format.
|
||||
// Decode decodes a key in PEM encoded ASN.1 DER format.
|
||||
// and returns a raw key.
|
||||
func (pemDecoder) Decode(src []byte) (any, []byte, error) {
|
||||
block, rest := pem.Decode(src)
|
||||
|
||||
-1
@@ -1,5 +1,4 @@
|
||||
//go:build jwx_es256k
|
||||
// +build jwx_es256k
|
||||
|
||||
package jws
|
||||
|
||||
|
||||
+10
-7
@@ -535,12 +535,12 @@ var rawKeyToKeyType = make(map[reflect.Type]jwa.KeyType)
|
||||
var keyTypeToAlgorithms = make(map[jwa.KeyType][]jwa.SignatureAlgorithm)
|
||||
|
||||
func init() {
|
||||
rawKeyToKeyType[reflect.TypeOf([]byte(nil))] = jwa.OctetSeq()
|
||||
rawKeyToKeyType[reflect.TypeOf(ed25519.PublicKey(nil))] = jwa.OKP()
|
||||
rawKeyToKeyType[reflect.TypeOf(rsa.PublicKey{})] = jwa.RSA()
|
||||
rawKeyToKeyType[reflect.TypeOf((*rsa.PublicKey)(nil))] = jwa.RSA()
|
||||
rawKeyToKeyType[reflect.TypeOf(ecdsa.PublicKey{})] = jwa.EC()
|
||||
rawKeyToKeyType[reflect.TypeOf((*ecdsa.PublicKey)(nil))] = jwa.EC()
|
||||
rawKeyToKeyType[reflect.TypeFor[[]byte]()] = jwa.OctetSeq()
|
||||
rawKeyToKeyType[reflect.TypeFor[ed25519.PublicKey]()] = jwa.OKP()
|
||||
rawKeyToKeyType[reflect.TypeFor[rsa.PublicKey]()] = jwa.RSA()
|
||||
rawKeyToKeyType[reflect.TypeFor[*rsa.PublicKey]()] = jwa.RSA()
|
||||
rawKeyToKeyType[reflect.TypeFor[ecdsa.PublicKey]()] = jwa.EC()
|
||||
rawKeyToKeyType[reflect.TypeFor[*ecdsa.PublicKey]()] = jwa.EC()
|
||||
|
||||
addAlgorithmForKeyType(jwa.OKP(), jwa.EdDSA())
|
||||
for _, alg := range []jwa.SignatureAlgorithm{jwa.HS256(), jwa.HS384(), jwa.HS512()} {
|
||||
@@ -586,11 +586,14 @@ func AlgorithmsForKey(key any) ([]jwa.SignatureAlgorithm, error) {
|
||||
return algs, nil
|
||||
}
|
||||
|
||||
// Settings allows you to set global settings for this JWS operations.
|
||||
//
|
||||
// Currently, the only setting available is `jws.WithLegacySigners()`,
|
||||
// which for various reason is now a no-op.
|
||||
func Settings(options ...GlobalOption) {
|
||||
for _, option := range options {
|
||||
switch option.Ident() {
|
||||
case identLegacySigners{}:
|
||||
enableLegacySigners()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ func (e headerNotFoundError) Is(target error) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// ErrHeaderdNotFound returns an error that can be passed to `errors.Is` to check if the error is
|
||||
// ErrHeaderNotFound returns an error that can be passed to `errors.Is` to check if the error is
|
||||
// the result of the field not being found
|
||||
func ErrHeaderNotFound() error {
|
||||
return headerNotFoundError{}
|
||||
|
||||
+4
-1
@@ -2,11 +2,14 @@ package jws
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/lestrrat-go/jwx/v3/jwa"
|
||||
"github.com/lestrrat-go/jwx/v3/jws/legacy"
|
||||
)
|
||||
|
||||
var enableLegacySignersOnce = &sync.Once{}
|
||||
|
||||
func enableLegacySigners() {
|
||||
for _, alg := range []jwa.SignatureAlgorithm{jwa.HS256(), jwa.HS384(), jwa.HS512()} {
|
||||
if err := RegisterSigner(alg, func(alg jwa.SignatureAlgorithm) SignerFactory {
|
||||
@@ -74,7 +77,7 @@ func legacySignerFor(alg jwa.SignatureAlgorithm) (Signer, error) {
|
||||
muSigner.Lock()
|
||||
s, ok := signers[alg]
|
||||
if !ok {
|
||||
v, err := NewSigner(alg)
|
||||
v, err := newLegacySigner(alg)
|
||||
if err != nil {
|
||||
muSigner.Unlock()
|
||||
return nil, fmt.Errorf(`failed to create payload signer: %w`, err)
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ type Signer interface {
|
||||
Algorithm() jwa.SignatureAlgorithm
|
||||
}
|
||||
|
||||
// This is for legacy support only.
|
||||
// Verifier is for legacy support only.
|
||||
type Verifier interface {
|
||||
// Verify checks whether the payload and signature are valid for
|
||||
// the given key.
|
||||
|
||||
+2
-2
@@ -38,7 +38,7 @@ type withKey struct {
|
||||
public Headers
|
||||
}
|
||||
|
||||
// This exists as an escape hatch to modify the header values after the fact
|
||||
// Protected exists as an escape hatch to modify the header values after the fact
|
||||
func (w *withKey) Protected(v Headers) Headers {
|
||||
if w.protected == nil && v != nil {
|
||||
w.protected = v
|
||||
@@ -221,7 +221,7 @@ type withInsecureNoSignature struct {
|
||||
protected Headers
|
||||
}
|
||||
|
||||
// This exists as an escape hatch to modify the header values after the fact
|
||||
// Protected exists as an escape hatch to modify the header values after the fact
|
||||
func (w *withInsecureNoSignature) Protected(v Headers) Headers {
|
||||
if w.protected == nil && v != nil {
|
||||
w.protected = v
|
||||
|
||||
+1
-5
@@ -227,8 +227,4 @@ options:
|
||||
interface: GlobalOption
|
||||
constant_value: true
|
||||
comment: |
|
||||
WithLegacySigners specifies whether the JWS package should use legacy
|
||||
signers for signing JWS messages.
|
||||
|
||||
Usually there's no need to use this option, as the new signers and
|
||||
verifiers are loaded by default.
|
||||
WithLegacySigners is a no-op option that exists only for backwards compatibility.
|
||||
|
||||
+1
-5
@@ -356,11 +356,7 @@ func WithKeyUsed(v any) VerifyOption {
|
||||
return &verifyOption{option.New(identKeyUsed{}, v)}
|
||||
}
|
||||
|
||||
// WithLegacySigners specifies whether the JWS package should use legacy
|
||||
// signers for signing JWS messages.
|
||||
//
|
||||
// Usually there's no need to use this option, as the new signers and
|
||||
// verifiers are loaded by default.
|
||||
// WithLegacySigners is a no-op option that exists only for backwards compatibility.
|
||||
func WithLegacySigners() GlobalOption {
|
||||
return &globalOption{option.New(identLegacySigners{}, true)}
|
||||
}
|
||||
|
||||
+42
-15
@@ -2,6 +2,7 @@ package jws
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/lestrrat-go/jwx/v3/jwa"
|
||||
@@ -33,6 +34,19 @@ func (fn SignerFactoryFn) Create() (Signer, error) {
|
||||
return fn()
|
||||
}
|
||||
|
||||
func init() {
|
||||
// register the signers using jwsbb. These will be used by default.
|
||||
for _, alg := range jwa.SignatureAlgorithms() {
|
||||
if alg == jwa.NoSignature() {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := RegisterSigner(alg, defaultSigner{alg: alg}); err != nil {
|
||||
panic(fmt.Sprintf("RegisterSigner failed: %v", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SignerFor returns a Signer2 for the given signature algorithm.
|
||||
//
|
||||
// Currently, this function will never fail. It will always return a
|
||||
@@ -43,6 +57,9 @@ func (fn SignerFactoryFn) Create() (Signer, error) {
|
||||
// 3. If no Signer2 or legacy Signer(Factory) is registered, it will return a
|
||||
// default signer that uses jwsbb.Sign.
|
||||
//
|
||||
// 1 and 2 will take care of 99% of the cases. The only time 3 will happen is
|
||||
// when you are using a custom algorithm that is not supported out of the box.
|
||||
//
|
||||
// jwsbb.Sign knows how to handle a static set of algorithms, so if the
|
||||
// algorithm is not supported, it will return an error when you call
|
||||
// `Sign` on the default signer.
|
||||
@@ -80,6 +97,14 @@ var signerDB = make(map[jwa.SignatureAlgorithm]SignerFactory)
|
||||
// Unlike the `UnregisterSigner` function, this function automatically
|
||||
// calls `jwa.RegisterSignatureAlgorithm` to register the algorithm
|
||||
// in this module's algorithm database.
|
||||
//
|
||||
// For backwards compatibility, this function also accepts
|
||||
// `SignerFactory` implementations, but this usage is deprecated.
|
||||
// You should use `Signer2` implementations instead.
|
||||
//
|
||||
// If you want to completely remove an algorithm, you must call
|
||||
// `jwa.UnregisterSignatureAlgorithm` yourself after calling
|
||||
// `UnregisterSigner`.
|
||||
func RegisterSigner(alg jwa.SignatureAlgorithm, f any) error {
|
||||
jwa.RegisterSignatureAlgorithm(alg)
|
||||
switch s := f.(type) {
|
||||
@@ -87,22 +112,10 @@ func RegisterSigner(alg jwa.SignatureAlgorithm, f any) error {
|
||||
muSigner2DB.Lock()
|
||||
signer2DB[alg] = s
|
||||
muSigner2DB.Unlock()
|
||||
|
||||
// delete the other signer, if there was one
|
||||
muSignerDB.Lock()
|
||||
delete(signerDB, alg)
|
||||
muSignerDB.Unlock()
|
||||
case SignerFactory:
|
||||
muSignerDB.Lock()
|
||||
signerDB[alg] = s
|
||||
muSignerDB.Unlock()
|
||||
|
||||
// Remove previous signer, if there was one
|
||||
removeSigner(alg)
|
||||
|
||||
muSigner2DB.Lock()
|
||||
delete(signer2DB, alg)
|
||||
muSigner2DB.Unlock()
|
||||
default:
|
||||
return fmt.Errorf(`jws.RegisterSigner: unsupported type %T for algorithm %q`, f, alg)
|
||||
}
|
||||
@@ -132,11 +145,25 @@ func UnregisterSigner(alg jwa.SignatureAlgorithm) {
|
||||
}
|
||||
|
||||
// NewSigner creates a signer that signs payloads using the given signature algorithm.
|
||||
// This function is deprecated. You should use `SignerFor()` instead.
|
||||
// This function is deprecated, and will either be removed to re-purposed using
|
||||
// a different signature.
|
||||
//
|
||||
// This function only exists for backwards compatibility, but will not work
|
||||
// unless you enable the legacy support mode by calling jws.Settings(jws.WithLegacySigners(true)).
|
||||
// When you want to load a Signer object, you should use `SignerFor()` instead.
|
||||
func NewSigner(alg jwa.SignatureAlgorithm) (Signer, error) {
|
||||
s, err := newLegacySigner(alg)
|
||||
if err == nil {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
if strings.HasPrefix(err.Error(), `jws.NewSigner: unsupported signature algorithm`) {
|
||||
// When newLegacySigner fails, automatically trigger to enable signers
|
||||
enableLegacySignersOnce.Do(enableLegacySigners)
|
||||
return newLegacySigner(alg)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func newLegacySigner(alg jwa.SignatureAlgorithm) (Signer, error) {
|
||||
muSignerDB.RLock()
|
||||
f, ok := signerDB[alg]
|
||||
muSignerDB.RUnlock()
|
||||
|
||||
+2
@@ -2,6 +2,8 @@
|
||||
//
|
||||
// It's internal because we don't want to expose _anything_ about these errors
|
||||
// so users absolutely cannot do anything other than use them as opaque errors.
|
||||
//
|
||||
//nolint:revive
|
||||
package errors
|
||||
|
||||
import (
|
||||
|
||||
+6
-1
@@ -211,8 +211,13 @@ func parseBytes(data []byte, options ...ParseOption) (Token, error) {
|
||||
for _, o := range options {
|
||||
if v, ok := o.(ValidateOption); ok {
|
||||
ctx.validateOpts = append(ctx.validateOpts, v)
|
||||
// context is used for both verification and validation, so we can't just continue
|
||||
switch o.Ident() {
|
||||
case identContext{}:
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
switch o.Ident() {
|
||||
case identKey{}:
|
||||
@@ -228,7 +233,7 @@ func parseBytes(data []byte, options ...ParseOption) (Token, error) {
|
||||
}
|
||||
}
|
||||
verifyOpts = append(verifyOpts, o)
|
||||
case identKeySet{}, identVerifyAuto{}, identKeyProvider{}, identBase64Encoder{}:
|
||||
case identKeySet{}, identVerifyAuto{}, identKeyProvider{}, identBase64Encoder{}, identContext{}:
|
||||
verifyOpts = append(verifyOpts, o)
|
||||
case identToken{}:
|
||||
var token Token
|
||||
|
||||
+10
@@ -1,7 +1,9 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/lestrrat-go/jwx/v3/jwa"
|
||||
@@ -137,6 +139,14 @@ func toVerifyOptions(options ...Option) ([]jws.VerifyOption, error) {
|
||||
return nil, fmt.Errorf(`failed to decode Base64Encoder: %w`, err)
|
||||
}
|
||||
voptions = append(voptions, jws.WithBase64Encoder(enc))
|
||||
case identContext{}:
|
||||
var ctx context.Context
|
||||
if err := option.Value(&ctx); err != nil {
|
||||
return nil, fmt.Errorf(`failed to decode Context: %w`, err)
|
||||
}
|
||||
voptions = append(voptions, jws.WithContext(ctx))
|
||||
default:
|
||||
return nil, fmt.Errorf(`invalid jws.VerifyOption %q passed`, `With`+strings.TrimPrefix(fmt.Sprintf(`%T`, option.Ident()), `jws.ident`))
|
||||
}
|
||||
}
|
||||
return voptions, nil
|
||||
|
||||
+1
-1
@@ -66,7 +66,7 @@ func (o *TokenOptionSet) Enable(flag TokenOption) {
|
||||
*o = TokenOptionSet(o.Value() | uint64(flag))
|
||||
}
|
||||
|
||||
// Enable sets the appropriate value to disable the option in the
|
||||
// Disable sets the appropriate value to disable the option in the
|
||||
// option set
|
||||
func (o *TokenOptionSet) Disable(flag TokenOption) {
|
||||
*o = TokenOptionSet(o.Value() & ^uint64(flag))
|
||||
|
||||
+4
-4
@@ -17,9 +17,9 @@ const _TokenOption_name = "FlattenAudienceMaxPerTokenOption"
|
||||
var _TokenOption_index = [...]uint8{0, 15, 32}
|
||||
|
||||
func (i TokenOption) String() string {
|
||||
i -= 1
|
||||
if i >= TokenOption(len(_TokenOption_index)-1) {
|
||||
return "TokenOption(" + strconv.FormatInt(int64(i+1), 10) + ")"
|
||||
idx := int(i) - 1
|
||||
if i < 1 || idx >= len(_TokenOption_index)-1 {
|
||||
return "TokenOption(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return _TokenOption_name[_TokenOption_index[i]:_TokenOption_index[i+1]]
|
||||
return _TokenOption_name[_TokenOption_index[idx]:_TokenOption_index[idx+1]]
|
||||
}
|
||||
|
||||
+4
-5
@@ -3,6 +3,7 @@ package jwt
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -344,12 +345,10 @@ func (ccs claimContainsString) Validate(_ context.Context, t Token) error {
|
||||
return ccs.makeErr(`claim %q does not exist or is not a []string: %w`, ccs.name, err)
|
||||
}
|
||||
|
||||
for _, v := range list {
|
||||
if v == ccs.value {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if !slices.Contains(list, ccs.value) {
|
||||
return ccs.makeErr(`%q not satisfied`, ccs.name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// audienceClaimContainsString can be used to check if the audience claim, which is
|
||||
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 lestrrat-go
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
-245
@@ -1,245 +0,0 @@
|
||||
# option
|
||||
|
||||
Base object for the "Optional Parameters Pattern".
|
||||
|
||||
# DESCRIPTION
|
||||
|
||||
The beauty of this pattern is that you can achieve a method that can
|
||||
take the following simple calling style
|
||||
|
||||
```go
|
||||
obj.Method(mandatory1, mandatory2)
|
||||
```
|
||||
|
||||
or the following, if you want to modify its behavior with optional parameters
|
||||
|
||||
```go
|
||||
obj.Method(mandatory1, mandatory2, optional1, optional2, optional3)
|
||||
```
|
||||
|
||||
Instead of the more clunky zero value for optionals style
|
||||
|
||||
```go
|
||||
obj.Method(mandatory1, mandatory2, nil, "", 0)
|
||||
```
|
||||
|
||||
or the equally clunky config object style, which requires you to create a
|
||||
struct with `NamesThatLookReallyLongBecauseItNeedsToIncludeMethodNamesConfig
|
||||
|
||||
```go
|
||||
cfg := &ConfigForMethod{
|
||||
Optional1: ...,
|
||||
Optional2: ...,
|
||||
Optional3: ...,
|
||||
}
|
||||
obj.Method(mandatory1, mandatory2, &cfg)
|
||||
```
|
||||
|
||||
# SYNOPSIS
|
||||
|
||||
Create an "identifier" for the option. We recommend using an unexported empty struct,
|
||||
because
|
||||
|
||||
1. It is uniquely identifiable globally
|
||||
1. Takes minimal space
|
||||
1. Since it's unexported, you do not have to worry about it leaking elsewhere or having it changed by consumers
|
||||
|
||||
```go
|
||||
// an unexported empty struct
|
||||
type identFeatureX struct{}
|
||||
```
|
||||
|
||||
Then define a method to create an option using this identifier. Here we assume
|
||||
that the option will be a boolean option.
|
||||
|
||||
```go
|
||||
// this is optional, but for readability we usually use a wrapper
|
||||
// around option.Interface, or a type alias.
|
||||
type Option
|
||||
func WithFeatureX(v bool) Option {
|
||||
// use the constructor to create a new option
|
||||
return option.New(identFeatureX{}, v)
|
||||
}
|
||||
```
|
||||
|
||||
Now you can create an option, which essentially a two element tuple consisting
|
||||
of an identifier and its associated value.
|
||||
|
||||
To consume this, you will need to create a function with variadic parameters,
|
||||
and iterate over the list looking for a particular identifier:
|
||||
|
||||
```go
|
||||
func MyAwesomeFunc( /* mandatory parameters omitted */, options ...[]Option) {
|
||||
var enableFeatureX bool
|
||||
// The nolint directive is recommended if you are using linters such
|
||||
// as golangci-lint
|
||||
//nolint:forcetypeassert
|
||||
for _, option := range options {
|
||||
switch option.Ident() {
|
||||
case identFeatureX{}:
|
||||
enableFeatureX = option.Value().(bool)
|
||||
// other cases omitted
|
||||
}
|
||||
}
|
||||
if enableFeatureX {
|
||||
....
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# Option objects
|
||||
|
||||
Option objects take two arguments, its identifier and the value it contains.
|
||||
|
||||
The identifier can be anything, but it's usually better to use a an unexported
|
||||
empty struct so that only you have the ability to generate said option:
|
||||
|
||||
```go
|
||||
type identOptionalParamOne struct{}
|
||||
type identOptionalParamTwo struct{}
|
||||
type identOptionalParamThree struct{}
|
||||
|
||||
func WithOptionOne(v ...) Option {
|
||||
return option.New(identOptionalParamOne{}, v)
|
||||
}
|
||||
```
|
||||
|
||||
Then you can call the method we described above as
|
||||
|
||||
```go
|
||||
obj.Method(m1, m2, WithOptionOne(...), WithOptionTwo(...), WithOptionThree(...))
|
||||
```
|
||||
|
||||
Options should be parsed in a code that looks somewhat like this
|
||||
|
||||
```go
|
||||
func (obj *Object) Method(m1 Type1, m2 Type2, options ...Option) {
|
||||
paramOne := defaultValueParamOne
|
||||
for _, option := range options {
|
||||
switch option.Ident() {
|
||||
case identOptionalParamOne{}:
|
||||
paramOne = option.Value().(...)
|
||||
}
|
||||
}
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
The loop requires a bit of boilerplate, and admittedly, this is the main downside
|
||||
of this module. However, if you think you want use the Option as a Function pattern,
|
||||
please check the FAQ below for rationale.
|
||||
|
||||
# Simple usage
|
||||
|
||||
Most of the times all you need to do is to declare the Option type as an alias
|
||||
in your code:
|
||||
|
||||
```go
|
||||
package myawesomepkg
|
||||
|
||||
import "github.com/lestrrat-go/option"
|
||||
|
||||
type Option = option.Interface
|
||||
```
|
||||
|
||||
Then you can start defining options like they are described in the SYNOPSIS section.
|
||||
|
||||
# Differentiating Options
|
||||
|
||||
When you have multiple methods and options, and those options can only be passed to
|
||||
each one the methods, it's hard to see which options should be passed to which method.
|
||||
|
||||
```go
|
||||
func WithX() Option { ... }
|
||||
func WithY() Option { ... }
|
||||
|
||||
// Now, which of WithX/WithY go to which method?
|
||||
func (*Obj) Method1(options ...Option) {}
|
||||
func (*Obj) Method2(options ...Option) {}
|
||||
```
|
||||
|
||||
In this case the easiest way to make it obvious is to put an extra layer around
|
||||
the options so that they have different types
|
||||
|
||||
```go
|
||||
type Method1Option interface {
|
||||
Option
|
||||
method1Option()
|
||||
}
|
||||
|
||||
type method1Option struct { Option }
|
||||
func (*method1Option) method1Option() {}
|
||||
|
||||
func WithX() Method1Option {
|
||||
return &methodOption{option.New(...)}
|
||||
}
|
||||
|
||||
func (*Obj) Method1(options ...Method1Option) {}
|
||||
```
|
||||
|
||||
This way the compiler knows if an option can be passed to a given method.
|
||||
|
||||
# FAQ
|
||||
|
||||
## Why aren't these function-based?
|
||||
|
||||
Using a base option type like `type Option func(ctx interface{})` is certainly one way to achieve the same goal. In this case, you are giving the option itself the ability to "configure" the main object. For example:
|
||||
|
||||
```go
|
||||
type Foo struct {
|
||||
optionaValue bool
|
||||
}
|
||||
|
||||
type Option func(*Foo) error
|
||||
|
||||
func WithOptionalValue(v bool) Option {
|
||||
return Option(func(f *Foo) error {
|
||||
f.optionalValue = v
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func NewFoo(options ...Option) (*Foo, error) {
|
||||
var f Foo
|
||||
for _, o := range options {
|
||||
if err := o(&f); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return &f
|
||||
}
|
||||
```
|
||||
|
||||
This in itself is fine, but we think there are a few problems:
|
||||
|
||||
### 1. It's hard to create a reusable "Option" type
|
||||
|
||||
We create many libraries using this optional pattern. We would like to provide a default base object. However, this function based approach is not reusuable because each "Option" type requires that it has a context-specific input type. For example, if the "Option" type in the previous example was `func(interface{}) error`, then its usability will significantly decrease because of the type conversion.
|
||||
|
||||
This is not to say that this library's approach is better as it also requires type conversion to convert the _value_ of the option. However, part of the beauty of the original function based approach was the ease of its use, and we claim that this significantly decreases the merits of the function based approach.
|
||||
|
||||
### 2. The receiver requires exported fields
|
||||
|
||||
Part of the appeal for a function-based option pattern is by giving the option itself the ability to do what it wants, you open up the possibility of allowing third-parties to create options that do things that the library authors did not think about.
|
||||
|
||||
```go
|
||||
package thirdparty
|
||||
, but when I read drum sheet music, I kind of get thrown off b/c many times it says to hit the bass drum where I feel like it's a snare hit.
|
||||
func WithMyAwesomeOption( ... ) mypkg.Option {
|
||||
return mypkg.Option(func(f *mypkg) error {
|
||||
f.X = ...
|
||||
f.Y = ...
|
||||
f.Z = ...
|
||||
return nil
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
However, for any third party code to access and set field values, these fields (`X`, `Y`, `Z`) must be exported. Basically you will need an "open" struct.
|
||||
|
||||
Exported fields are absolutely no problem when you have a struct that represents data alone (i.e., API calls that refer or change state information) happen, but we think that casually expose fields for a library struct is a sure way to maintenance hell in the future. What happens when you want to change the API? What happens when you realize that you want to use the field as state (i.e. use it for more than configuration)? What if they kept referring to that field, and then you have concurrent code accessing it?
|
||||
|
||||
Giving third parties complete access to exported fields is like handing out a loaded weapon to the users, and you are at their mercy.
|
||||
|
||||
Of course, providing public APIs for everything so you can validate and control concurrency is an option, but then ... it's a lot of work, and you may have to provide APIs _only_ so that users can refer it in the option-configuration phase. That sounds like a lot of extra work.
|
||||
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
package option
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Interface defines the minimum interface that an option must fulfill
|
||||
type Interface interface {
|
||||
// Ident returns the "identity" of this option, a unique identifier that
|
||||
// can be used to differentiate between options
|
||||
Ident() interface{}
|
||||
|
||||
// Value returns the corresponding value.
|
||||
Value() interface{}
|
||||
}
|
||||
|
||||
type pair struct {
|
||||
ident interface{}
|
||||
value interface{}
|
||||
}
|
||||
|
||||
// New creates a new Option
|
||||
func New(ident, value interface{}) Interface {
|
||||
return &pair{
|
||||
ident: ident,
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pair) Ident() interface{} {
|
||||
return p.ident
|
||||
}
|
||||
|
||||
func (p *pair) Value() interface{} {
|
||||
return p.value
|
||||
}
|
||||
|
||||
func (p *pair) String() string {
|
||||
return fmt.Sprintf(`%v(%v)`, p.ident, p.value)
|
||||
}
|
||||
-3
@@ -2,9 +2,6 @@
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build go1.16
|
||||
// +build go1.16
|
||||
|
||||
package capabilities
|
||||
|
||||
import (
|
||||
|
||||
Generated
Vendored
+22
-11
@@ -40,7 +40,8 @@
|
||||
"type": "boolean"
|
||||
},
|
||||
"type": "function"
|
||||
}
|
||||
},
|
||||
"deprecated": true
|
||||
},
|
||||
{
|
||||
"name": "and",
|
||||
@@ -95,7 +96,8 @@
|
||||
"type": "boolean"
|
||||
},
|
||||
"type": "function"
|
||||
}
|
||||
},
|
||||
"deprecated": true
|
||||
},
|
||||
{
|
||||
"name": "array.concat",
|
||||
@@ -385,7 +387,8 @@
|
||||
"type": "array"
|
||||
},
|
||||
"type": "function"
|
||||
}
|
||||
},
|
||||
"deprecated": true
|
||||
},
|
||||
{
|
||||
"name": "cast_boolean",
|
||||
@@ -399,7 +402,8 @@
|
||||
"type": "boolean"
|
||||
},
|
||||
"type": "function"
|
||||
}
|
||||
},
|
||||
"deprecated": true
|
||||
},
|
||||
{
|
||||
"name": "cast_null",
|
||||
@@ -413,7 +417,8 @@
|
||||
"type": "null"
|
||||
},
|
||||
"type": "function"
|
||||
}
|
||||
},
|
||||
"deprecated": true
|
||||
},
|
||||
{
|
||||
"name": "cast_object",
|
||||
@@ -435,7 +440,8 @@
|
||||
"type": "object"
|
||||
},
|
||||
"type": "function"
|
||||
}
|
||||
},
|
||||
"deprecated": true
|
||||
},
|
||||
{
|
||||
"name": "cast_set",
|
||||
@@ -452,7 +458,8 @@
|
||||
"type": "set"
|
||||
},
|
||||
"type": "function"
|
||||
}
|
||||
},
|
||||
"deprecated": true
|
||||
},
|
||||
{
|
||||
"name": "cast_string",
|
||||
@@ -466,7 +473,8 @@
|
||||
"type": "string"
|
||||
},
|
||||
"type": "function"
|
||||
}
|
||||
},
|
||||
"deprecated": true
|
||||
},
|
||||
{
|
||||
"name": "ceil",
|
||||
@@ -2975,7 +2983,8 @@
|
||||
"type": "boolean"
|
||||
},
|
||||
"type": "function"
|
||||
}
|
||||
},
|
||||
"deprecated": true
|
||||
},
|
||||
{
|
||||
"name": "net.lookup_ip_addr",
|
||||
@@ -3493,7 +3502,8 @@
|
||||
"type": "boolean"
|
||||
},
|
||||
"type": "function"
|
||||
}
|
||||
},
|
||||
"deprecated": true
|
||||
},
|
||||
{
|
||||
"name": "regex.find_all_string_submatch_n",
|
||||
@@ -3808,7 +3818,8 @@
|
||||
"type": "set"
|
||||
},
|
||||
"type": "function"
|
||||
}
|
||||
},
|
||||
"deprecated": true
|
||||
},
|
||||
{
|
||||
"name": "sort",
|
||||
+4878
File diff suppressed because it is too large
Load Diff
+4896
File diff suppressed because it is too large
Load Diff
+4896
File diff suppressed because it is too large
Load Diff
+4896
File diff suppressed because it is too large
Load Diff
+4896
File diff suppressed because it is too large
Load Diff
+4916
File diff suppressed because it is too large
Load Diff
+4916
File diff suppressed because it is too large
Load Diff
+4916
File diff suppressed because it is too large
Load Diff
+4916
File diff suppressed because it is too large
Load Diff
+4916
File diff suppressed because it is too large
Load Diff
+654
-569
File diff suppressed because one or more lines are too long
BIN
Binary file not shown.
+1
-1
@@ -133,7 +133,7 @@ func (c *Compiler) removeUnusedCode() error {
|
||||
}
|
||||
caller, ok := c.funcs[callerName]
|
||||
if !ok {
|
||||
return fmt.Errorf("caller not found: %s (%s)", cg[i][0], callerName)
|
||||
continue // without a caller, it should get removed anyways (right?)
|
||||
}
|
||||
callee, ok := c.funcs[calleeName]
|
||||
if !ok {
|
||||
|
||||
+213
-168
@@ -32,7 +32,7 @@ const (
|
||||
opaWasmABIMinorVersionVar = "opa_wasm_abi_minor_version"
|
||||
)
|
||||
|
||||
// nolint: deadcode,varcheck
|
||||
// nolint: varcheck
|
||||
const (
|
||||
opaTypeNull int32 = iota + 1
|
||||
opaTypeBoolean
|
||||
@@ -90,6 +90,7 @@ var builtinsFunctions = map[string]string{
|
||||
ast.Floor.Name: "opa_arith_floor",
|
||||
ast.Rem.Name: "opa_arith_rem",
|
||||
ast.ArrayConcat.Name: "opa_array_concat",
|
||||
ast.ArrayFlatten.Name: "opa_array_flatten",
|
||||
ast.ArrayReverse.Name: "opa_array_reverse",
|
||||
ast.ArraySlice.Name: "opa_array_slice",
|
||||
ast.SetDiff.Name: "opa_set_diff",
|
||||
@@ -162,6 +163,7 @@ var builtinsFunctions = map[string]string{
|
||||
ast.TrimRight.Name: "opa_strings_trim_right",
|
||||
ast.TrimSuffix.Name: "opa_strings_trim_suffix",
|
||||
ast.TrimSpace.Name: "opa_strings_trim_space",
|
||||
ast.InternalTemplateString.Name: "opa_template_string",
|
||||
ast.NumbersRange.Name: "opa_numbers_range",
|
||||
ast.ToNumber.Name: "opa_to_number",
|
||||
ast.WalkBuiltin.Name: "opa_value_transitive_closure",
|
||||
@@ -414,7 +416,7 @@ func (c *Compiler) initModule() error {
|
||||
},
|
||||
},
|
||||
},
|
||||
Init: bytes.Repeat([]byte{0}, int(heapBase-offset)),
|
||||
Init: make([]byte, int(heapBase-offset)),
|
||||
})
|
||||
|
||||
return nil
|
||||
@@ -1058,9 +1060,11 @@ func (c *Compiler) compileBlock(block *ir.Block) ([]instruction.Instruction, err
|
||||
},
|
||||
})
|
||||
case *ir.AssignIntStmt:
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(stmt.Target)})
|
||||
instrs = append(instrs, instruction.I64Const{Value: stmt.Value})
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaValueNumberSetInt)})
|
||||
instrs = append(instrs,
|
||||
instruction.GetLocal{Index: c.local(stmt.Target)},
|
||||
instruction.I64Const{Value: stmt.Value},
|
||||
instruction.Call{Index: c.function(opaValueNumberSetInt)},
|
||||
)
|
||||
case *ir.ScanStmt:
|
||||
if err := c.compileScan(stmt, &instrs); err != nil {
|
||||
return nil, err
|
||||
@@ -1073,12 +1077,14 @@ func (c *Compiler) compileBlock(block *ir.Block) ([]instruction.Instruction, err
|
||||
}
|
||||
case *ir.DotStmt:
|
||||
if loc, ok := stmt.Source.Value.(ir.Local); ok {
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(loc)})
|
||||
instrs = append(instrs, c.instrRead(stmt.Key))
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaValueGet)})
|
||||
instrs = append(instrs, instruction.TeeLocal{Index: c.local(stmt.Target)})
|
||||
instrs = append(instrs, instruction.I32Eqz{})
|
||||
instrs = append(instrs, instruction.BrIf{Index: 0})
|
||||
instrs = append(instrs,
|
||||
instruction.GetLocal{Index: c.local(loc)},
|
||||
c.instrRead(stmt.Key),
|
||||
instruction.Call{Index: c.function(opaValueGet)},
|
||||
instruction.TeeLocal{Index: c.local(stmt.Target)},
|
||||
instruction.I32Eqz{},
|
||||
instruction.BrIf{Index: 0},
|
||||
)
|
||||
} else {
|
||||
// Booleans and string sources would lead to the BrIf (since opa_value_get
|
||||
// on them returns 0), so let's skip trying that.
|
||||
@@ -1086,97 +1092,131 @@ func (c *Compiler) compileBlock(block *ir.Block) ([]instruction.Instruction, err
|
||||
break
|
||||
}
|
||||
case *ir.LenStmt:
|
||||
instrs = append(instrs, c.instrRead(stmt.Source))
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaValueLength)})
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaNumberSize)})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(stmt.Target)})
|
||||
instrs = append(instrs,
|
||||
c.instrRead(stmt.Source),
|
||||
instruction.Call{Index: c.function(opaValueLength)},
|
||||
instruction.Call{Index: c.function(opaNumberSize)},
|
||||
instruction.SetLocal{Index: c.local(stmt.Target)},
|
||||
)
|
||||
case *ir.EqualStmt:
|
||||
instrs = append(instrs, c.instrRead(stmt.A))
|
||||
instrs = append(instrs, c.instrRead(stmt.B))
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaValueCompare)})
|
||||
instrs = append(instrs, instruction.BrIf{Index: 0})
|
||||
instrs = append(instrs,
|
||||
c.instrRead(stmt.A),
|
||||
c.instrRead(stmt.B),
|
||||
instruction.Call{Index: c.function(opaValueCompare)},
|
||||
instruction.BrIf{Index: 0},
|
||||
)
|
||||
case *ir.NotEqualStmt:
|
||||
instrs = append(instrs, c.instrRead(stmt.A))
|
||||
instrs = append(instrs, c.instrRead(stmt.B))
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaValueCompare)})
|
||||
instrs = append(instrs, instruction.I32Eqz{})
|
||||
instrs = append(instrs, instruction.BrIf{Index: 0})
|
||||
instrs = append(instrs,
|
||||
c.instrRead(stmt.A),
|
||||
c.instrRead(stmt.B),
|
||||
instruction.Call{Index: c.function(opaValueCompare)},
|
||||
instruction.I32Eqz{},
|
||||
instruction.BrIf{Index: 0},
|
||||
)
|
||||
case *ir.MakeNullStmt:
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaNull)})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(stmt.Target)})
|
||||
instrs = append(instrs,
|
||||
instruction.Call{Index: c.function(opaNull)},
|
||||
instruction.SetLocal{Index: c.local(stmt.Target)},
|
||||
)
|
||||
case *ir.MakeNumberIntStmt:
|
||||
instrs = append(instrs, instruction.I64Const{Value: stmt.Value})
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaNumberInt)})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(stmt.Target)})
|
||||
instrs = append(instrs,
|
||||
instruction.I64Const{Value: stmt.Value},
|
||||
instruction.Call{Index: c.function(opaNumberInt)},
|
||||
instruction.SetLocal{Index: c.local(stmt.Target)},
|
||||
)
|
||||
case *ir.MakeNumberRefStmt:
|
||||
instrs = append(instrs, instruction.I32Const{Value: c.stringAddr(stmt.Index)})
|
||||
instrs = append(instrs, instruction.I32Const{Value: int32(len(c.policy.Static.Strings[stmt.Index].Value))})
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaNumberRef)})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(stmt.Target)})
|
||||
instrs = append(instrs,
|
||||
instruction.I32Const{Value: c.stringAddr(stmt.Index)},
|
||||
instruction.I32Const{Value: int32(len(c.policy.Static.Strings[stmt.Index].Value))},
|
||||
instruction.Call{Index: c.function(opaNumberRef)},
|
||||
instruction.SetLocal{Index: c.local(stmt.Target)},
|
||||
)
|
||||
case *ir.MakeArrayStmt:
|
||||
instrs = append(instrs, instruction.I32Const{Value: stmt.Capacity})
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaArrayWithCap)})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(stmt.Target)})
|
||||
instrs = append(instrs,
|
||||
instruction.I32Const{Value: stmt.Capacity},
|
||||
instruction.Call{Index: c.function(opaArrayWithCap)},
|
||||
instruction.SetLocal{Index: c.local(stmt.Target)},
|
||||
)
|
||||
case *ir.MakeObjectStmt:
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaObject)})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(stmt.Target)})
|
||||
instrs = append(instrs,
|
||||
instruction.Call{Index: c.function(opaObject)},
|
||||
instruction.SetLocal{Index: c.local(stmt.Target)},
|
||||
)
|
||||
case *ir.MakeSetStmt:
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaSet)})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(stmt.Target)})
|
||||
instrs = append(instrs,
|
||||
instruction.Call{Index: c.function(opaSet)},
|
||||
instruction.SetLocal{Index: c.local(stmt.Target)},
|
||||
)
|
||||
case *ir.IsArrayStmt:
|
||||
if loc, ok := stmt.Source.Value.(ir.Local); ok {
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(loc)})
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaValueType)})
|
||||
instrs = append(instrs, instruction.I32Const{Value: opaTypeArray})
|
||||
instrs = append(instrs, instruction.I32Ne{})
|
||||
instrs = append(instrs, instruction.BrIf{Index: 0})
|
||||
instrs = append(instrs,
|
||||
instruction.GetLocal{Index: c.local(loc)},
|
||||
instruction.Call{Index: c.function(opaValueType)},
|
||||
instruction.I32Const{Value: opaTypeArray},
|
||||
instruction.I32Ne{},
|
||||
instruction.BrIf{Index: 0},
|
||||
)
|
||||
} else {
|
||||
instrs = append(instrs, instruction.Br{Index: 0})
|
||||
break
|
||||
}
|
||||
case *ir.IsObjectStmt:
|
||||
if loc, ok := stmt.Source.Value.(ir.Local); ok {
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(loc)})
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaValueType)})
|
||||
instrs = append(instrs, instruction.I32Const{Value: opaTypeObject})
|
||||
instrs = append(instrs, instruction.I32Ne{})
|
||||
instrs = append(instrs, instruction.BrIf{Index: 0})
|
||||
instrs = append(instrs,
|
||||
instruction.GetLocal{Index: c.local(loc)},
|
||||
instruction.Call{Index: c.function(opaValueType)},
|
||||
instruction.I32Const{Value: opaTypeObject},
|
||||
instruction.I32Ne{},
|
||||
instruction.BrIf{Index: 0},
|
||||
)
|
||||
} else {
|
||||
instrs = append(instrs, instruction.Br{Index: 0})
|
||||
break
|
||||
}
|
||||
case *ir.IsSetStmt:
|
||||
if loc, ok := stmt.Source.Value.(ir.Local); ok {
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(loc)})
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaValueType)})
|
||||
instrs = append(instrs, instruction.I32Const{Value: opaTypeSet})
|
||||
instrs = append(instrs, instruction.I32Ne{})
|
||||
instrs = append(instrs, instruction.BrIf{Index: 0})
|
||||
instrs = append(instrs,
|
||||
instruction.GetLocal{Index: c.local(loc)},
|
||||
instruction.Call{Index: c.function(opaValueType)},
|
||||
instruction.I32Const{Value: opaTypeSet},
|
||||
instruction.I32Ne{},
|
||||
instruction.BrIf{Index: 0},
|
||||
)
|
||||
} else {
|
||||
instrs = append(instrs, instruction.Br{Index: 0})
|
||||
break
|
||||
}
|
||||
case *ir.IsUndefinedStmt:
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(stmt.Source)})
|
||||
instrs = append(instrs, instruction.I32Const{Value: 0})
|
||||
instrs = append(instrs, instruction.I32Ne{})
|
||||
instrs = append(instrs, instruction.BrIf{Index: 0})
|
||||
instrs = append(instrs,
|
||||
instruction.GetLocal{Index: c.local(stmt.Source)},
|
||||
instruction.I32Const{Value: 0},
|
||||
instruction.I32Ne{},
|
||||
instruction.BrIf{Index: 0},
|
||||
)
|
||||
case *ir.ResetLocalStmt:
|
||||
instrs = append(instrs, instruction.I32Const{Value: 0})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(stmt.Target)})
|
||||
instrs = append(instrs,
|
||||
instruction.I32Const{Value: 0},
|
||||
instruction.SetLocal{Index: c.local(stmt.Target)},
|
||||
)
|
||||
case *ir.IsDefinedStmt:
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(stmt.Source)})
|
||||
instrs = append(instrs, instruction.I32Eqz{})
|
||||
instrs = append(instrs, instruction.BrIf{Index: 0})
|
||||
instrs = append(instrs,
|
||||
instruction.GetLocal{Index: c.local(stmt.Source)},
|
||||
instruction.I32Eqz{},
|
||||
instruction.BrIf{Index: 0},
|
||||
)
|
||||
case *ir.ArrayAppendStmt:
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(stmt.Array)})
|
||||
instrs = append(instrs, c.instrRead(stmt.Value))
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaArrayAppend)})
|
||||
instrs = append(instrs,
|
||||
instruction.GetLocal{Index: c.local(stmt.Array)},
|
||||
c.instrRead(stmt.Value),
|
||||
instruction.Call{Index: c.function(opaArrayAppend)},
|
||||
)
|
||||
case *ir.ObjectInsertStmt:
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(stmt.Object)})
|
||||
instrs = append(instrs, c.instrRead(stmt.Key))
|
||||
instrs = append(instrs, c.instrRead(stmt.Value))
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaObjectInsert)})
|
||||
instrs = append(instrs,
|
||||
instruction.GetLocal{Index: c.local(stmt.Object)},
|
||||
c.instrRead(stmt.Key),
|
||||
c.instrRead(stmt.Value),
|
||||
instruction.Call{Index: c.function(opaObjectInsert)},
|
||||
)
|
||||
case *ir.ObjectInsertOnceStmt:
|
||||
tmp := c.genLocal()
|
||||
instrs = append(instrs, instruction.Block{
|
||||
@@ -1203,14 +1243,18 @@ func (c *Compiler) compileBlock(block *ir.Block) ([]instruction.Instruction, err
|
||||
},
|
||||
})
|
||||
case *ir.ObjectMergeStmt:
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(stmt.A)})
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(stmt.B)})
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaValueMerge)})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(stmt.Target)})
|
||||
instrs = append(instrs,
|
||||
instruction.GetLocal{Index: c.local(stmt.A)},
|
||||
instruction.GetLocal{Index: c.local(stmt.B)},
|
||||
instruction.Call{Index: c.function(opaValueMerge)},
|
||||
instruction.SetLocal{Index: c.local(stmt.Target)},
|
||||
)
|
||||
case *ir.SetAddStmt:
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(stmt.Set)})
|
||||
instrs = append(instrs, c.instrRead(stmt.Value))
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaSetAdd)})
|
||||
instrs = append(instrs,
|
||||
instruction.GetLocal{Index: c.local(stmt.Set)},
|
||||
c.instrRead(stmt.Value),
|
||||
instruction.Call{Index: c.function(opaSetAdd)},
|
||||
)
|
||||
default:
|
||||
var buf bytes.Buffer
|
||||
err := ir.Pretty(&buf, stmt)
|
||||
@@ -1226,8 +1270,7 @@ func (c *Compiler) compileBlock(block *ir.Block) ([]instruction.Instruction, err
|
||||
|
||||
func (c *Compiler) compileScan(scan *ir.ScanStmt, result *[]instruction.Instruction) error {
|
||||
var instrs = *result
|
||||
instrs = append(instrs, instruction.I32Const{Value: 0})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(scan.Key)})
|
||||
instrs = append(instrs, instruction.I32Const{Value: 0}, instruction.SetLocal{Index: c.local(scan.Key)})
|
||||
body, err := c.compileScanBlock(scan)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1242,23 +1285,22 @@ func (c *Compiler) compileScan(scan *ir.ScanStmt, result *[]instruction.Instruct
|
||||
}
|
||||
|
||||
func (c *Compiler) compileScanBlock(scan *ir.ScanStmt) ([]instruction.Instruction, error) {
|
||||
var instrs []instruction.Instruction
|
||||
|
||||
//nolint:prealloc // instruction list is known and fixed, clearer as slice literal
|
||||
instrs := []instruction.Instruction{
|
||||
// Execute iterator.
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(scan.Source)})
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(scan.Key)})
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaValueIter)})
|
||||
|
||||
instruction.GetLocal{Index: c.local(scan.Source)},
|
||||
instruction.GetLocal{Index: c.local(scan.Key)},
|
||||
instruction.Call{Index: c.function(opaValueIter)},
|
||||
// Check for emptiness.
|
||||
instrs = append(instrs, instruction.TeeLocal{Index: c.local(scan.Key)})
|
||||
instrs = append(instrs, instruction.I32Eqz{})
|
||||
instrs = append(instrs, instruction.BrIf{Index: 1})
|
||||
|
||||
instruction.TeeLocal{Index: c.local(scan.Key)},
|
||||
instruction.I32Eqz{},
|
||||
instruction.BrIf{Index: 1},
|
||||
// Load value.
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(scan.Source)})
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(scan.Key)})
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaValueGet)})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(scan.Value)})
|
||||
instruction.GetLocal{Index: c.local(scan.Source)},
|
||||
instruction.GetLocal{Index: c.local(scan.Key)},
|
||||
instruction.Call{Index: c.function(opaValueGet)},
|
||||
instruction.SetLocal{Index: c.local(scan.Value)},
|
||||
}
|
||||
|
||||
// Loop body.
|
||||
nested, err := c.compileBlock(scan.Block)
|
||||
@@ -1278,8 +1320,7 @@ func (c *Compiler) compileNot(not *ir.NotStmt, result *[]instruction.Instruction
|
||||
|
||||
// generate and initialize condition variable
|
||||
cond := c.genLocal()
|
||||
instrs = append(instrs, instruction.I32Const{Value: 1})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: cond})
|
||||
instrs = append(instrs, instruction.I32Const{Value: 1}, instruction.SetLocal{Index: cond})
|
||||
|
||||
nested, err := c.compileBlock(not.Block)
|
||||
if err != nil {
|
||||
@@ -1287,14 +1328,15 @@ func (c *Compiler) compileNot(not *ir.NotStmt, result *[]instruction.Instruction
|
||||
}
|
||||
|
||||
// unset condition variable if end of block is reached
|
||||
nested = append(nested, instruction.I32Const{Value: 0})
|
||||
nested = append(nested, instruction.SetLocal{Index: cond})
|
||||
instrs = append(instrs, instruction.Block{Instrs: nested})
|
||||
|
||||
instrs = append(instrs, instruction.Block{Instrs: append(nested,
|
||||
instruction.I32Const{Value: 0},
|
||||
instruction.SetLocal{Index: cond},
|
||||
)},
|
||||
// break out of block if condition variable was unset
|
||||
instrs = append(instrs, instruction.GetLocal{Index: cond})
|
||||
instrs = append(instrs, instruction.I32Eqz{})
|
||||
instrs = append(instrs, instruction.BrIf{Index: 0})
|
||||
instruction.GetLocal{Index: cond},
|
||||
instruction.I32Eqz{},
|
||||
instruction.BrIf{Index: 0},
|
||||
)
|
||||
|
||||
*result = instrs
|
||||
return nil
|
||||
@@ -1304,34 +1346,36 @@ func (c *Compiler) compileWithStmt(with *ir.WithStmt, result *[]instruction.Inst
|
||||
|
||||
var instrs = *result
|
||||
save := c.genLocal()
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaMemoizePush)})
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(with.Local)})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: save})
|
||||
instrs = append(instrs,
|
||||
instruction.Call{Index: c.function(opaMemoizePush)},
|
||||
instruction.GetLocal{Index: c.local(with.Local)},
|
||||
instruction.SetLocal{Index: save},
|
||||
)
|
||||
|
||||
if len(with.Path) == 0 {
|
||||
instrs = append(instrs, c.instrRead(with.Value))
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(with.Local)})
|
||||
instrs = append(instrs, c.instrRead(with.Value), instruction.SetLocal{Index: c.local(with.Local)})
|
||||
} else {
|
||||
instrs = c.compileUpsert(with.Local, with.Path, with.Value, with.Location, instrs)
|
||||
}
|
||||
|
||||
undefined := c.genLocal()
|
||||
instrs = append(instrs, instruction.I32Const{Value: 1})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: undefined})
|
||||
instrs = append(instrs, instruction.I32Const{Value: 1}, instruction.SetLocal{Index: undefined})
|
||||
|
||||
nested, err := c.compileBlock(with.Block)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
nested = append(nested, instruction.I32Const{Value: 0})
|
||||
nested = append(nested, instruction.SetLocal{Index: undefined})
|
||||
instrs = append(instrs, instruction.Block{Instrs: nested})
|
||||
instrs = append(instrs, instruction.GetLocal{Index: save})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: c.local(with.Local)})
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaMemoizePop)})
|
||||
instrs = append(instrs, instruction.GetLocal{Index: undefined})
|
||||
instrs = append(instrs, instruction.BrIf{Index: 0})
|
||||
nested = append(nested, instruction.I32Const{Value: 0}, instruction.SetLocal{Index: undefined})
|
||||
|
||||
instrs = append(instrs,
|
||||
instruction.Block{Instrs: nested},
|
||||
instruction.GetLocal{Index: save},
|
||||
instruction.SetLocal{Index: c.local(with.Local)},
|
||||
instruction.Call{Index: c.function(opaMemoizePop)},
|
||||
instruction.GetLocal{Index: undefined},
|
||||
instruction.BrIf{Index: 0},
|
||||
)
|
||||
|
||||
*result = instrs
|
||||
|
||||
@@ -1339,13 +1383,12 @@ func (c *Compiler) compileWithStmt(with *ir.WithStmt, result *[]instruction.Inst
|
||||
}
|
||||
|
||||
func (c *Compiler) compileUpsert(local ir.Local, path []int, value ir.Operand, _ ir.Location, instrs []instruction.Instruction) []instruction.Instruction {
|
||||
|
||||
lcopy := c.genLocal() // holds copy of local
|
||||
instrs = append(instrs, instruction.GetLocal{Index: c.local(local)})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: lcopy})
|
||||
|
||||
instrs = append(instrs,
|
||||
instruction.GetLocal{Index: c.local(local)},
|
||||
instruction.SetLocal{Index: lcopy},
|
||||
// Shallow copy the local if defined otherwise initialize to an empty object.
|
||||
instrs = append(instrs, instruction.Block{
|
||||
instruction.Block{
|
||||
Instrs: []instruction.Instruction{
|
||||
instruction.Block{Instrs: []instruction.Instruction{
|
||||
instruction.GetLocal{Index: lcopy},
|
||||
@@ -1368,8 +1411,10 @@ func (c *Compiler) compileUpsert(local ir.Local, path []int, value ir.Operand, _
|
||||
|
||||
for i := range path {
|
||||
lpath[i] = c.genLocal()
|
||||
instrs = append(instrs, instruction.I32Const{Value: c.opaStringAddr(path[i])})
|
||||
instrs = append(instrs, instruction.SetLocal{Index: lpath[i]})
|
||||
instrs = append(instrs,
|
||||
instruction.I32Const{Value: c.opaStringAddr(path[i])},
|
||||
instruction.SetLocal{Index: lpath[i]},
|
||||
)
|
||||
}
|
||||
|
||||
// Generate a block that traverses the path of the upsert operation,
|
||||
@@ -1379,36 +1424,34 @@ func (c *Compiler) compileUpsert(local ir.Local, path []int, value ir.Operand, _
|
||||
ltemp := c.genLocal()
|
||||
|
||||
for i := range len(path) - 1 {
|
||||
|
||||
inner = append(inner,
|
||||
// Lookup the next part of the path.
|
||||
inner = append(inner, instruction.GetLocal{Index: lcopy})
|
||||
inner = append(inner, instruction.GetLocal{Index: lpath[i]})
|
||||
inner = append(inner, instruction.Call{Index: c.function(opaValueGet)})
|
||||
inner = append(inner, instruction.SetLocal{Index: ltemp})
|
||||
|
||||
instruction.GetLocal{Index: lcopy},
|
||||
instruction.GetLocal{Index: lpath[i]},
|
||||
instruction.Call{Index: c.function(opaValueGet)},
|
||||
instruction.SetLocal{Index: ltemp},
|
||||
// If the next node is missing, break.
|
||||
inner = append(inner, instruction.GetLocal{Index: ltemp})
|
||||
inner = append(inner, instruction.I32Eqz{})
|
||||
inner = append(inner, instruction.BrIf{Index: uint32(i)})
|
||||
|
||||
instruction.GetLocal{Index: ltemp},
|
||||
instruction.I32Eqz{},
|
||||
instruction.BrIf{Index: uint32(i)},
|
||||
// If the next node is not an object, break.
|
||||
inner = append(inner, instruction.GetLocal{Index: ltemp})
|
||||
inner = append(inner, instruction.Call{Index: c.function(opaValueType)})
|
||||
inner = append(inner, instruction.I32Const{Value: opaTypeObject})
|
||||
inner = append(inner, instruction.I32Ne{})
|
||||
inner = append(inner, instruction.BrIf{Index: uint32(i)})
|
||||
|
||||
instruction.GetLocal{Index: ltemp},
|
||||
instruction.Call{Index: c.function(opaValueType)},
|
||||
instruction.I32Const{Value: opaTypeObject},
|
||||
instruction.I32Ne{},
|
||||
instruction.BrIf{Index: uint32(i)},
|
||||
// Otherwise, shallow copy the next node node and insert into the copy
|
||||
// before continuing.
|
||||
inner = append(inner, instruction.GetLocal{Index: ltemp})
|
||||
inner = append(inner, instruction.Call{Index: c.function(opaValueShallowCopy)})
|
||||
inner = append(inner, instruction.SetLocal{Index: ltemp})
|
||||
inner = append(inner, instruction.GetLocal{Index: lcopy})
|
||||
inner = append(inner, instruction.GetLocal{Index: lpath[i]})
|
||||
inner = append(inner, instruction.GetLocal{Index: ltemp})
|
||||
inner = append(inner, instruction.Call{Index: c.function(opaObjectInsert)})
|
||||
inner = append(inner, instruction.GetLocal{Index: ltemp})
|
||||
inner = append(inner, instruction.SetLocal{Index: lcopy})
|
||||
instruction.GetLocal{Index: ltemp},
|
||||
instruction.Call{Index: c.function(opaValueShallowCopy)},
|
||||
instruction.SetLocal{Index: ltemp},
|
||||
instruction.GetLocal{Index: lcopy},
|
||||
instruction.GetLocal{Index: lpath[i]},
|
||||
instruction.GetLocal{Index: ltemp},
|
||||
instruction.Call{Index: c.function(opaObjectInsert)},
|
||||
instruction.GetLocal{Index: ltemp},
|
||||
instruction.SetLocal{Index: lcopy},
|
||||
)
|
||||
}
|
||||
|
||||
inner = append(inner, instruction.Br{Index: uint32(len(path) - 1)})
|
||||
@@ -1418,31 +1461,33 @@ func (c *Compiler) compileUpsert(local ir.Local, path []int, value ir.Operand, _
|
||||
lval := c.genLocal()
|
||||
|
||||
for i := range len(path) - 1 {
|
||||
block = append(block, instruction.Block{Instrs: inner})
|
||||
block = append(block, instruction.Call{Index: c.function(opaObject)})
|
||||
block = append(block, instruction.SetLocal{Index: lval})
|
||||
block = append(block, instruction.GetLocal{Index: lcopy})
|
||||
block = append(block, instruction.GetLocal{Index: lpath[i]})
|
||||
block = append(block, instruction.GetLocal{Index: lval})
|
||||
block = append(block, instruction.Call{Index: c.function(opaObjectInsert)})
|
||||
block = append(block, instruction.GetLocal{Index: lval})
|
||||
block = append(block, instruction.SetLocal{Index: lcopy})
|
||||
block = append(block,
|
||||
instruction.Block{Instrs: inner},
|
||||
instruction.Call{Index: c.function(opaObject)},
|
||||
instruction.SetLocal{Index: lval},
|
||||
instruction.GetLocal{Index: lcopy},
|
||||
instruction.GetLocal{Index: lpath[i]},
|
||||
instruction.GetLocal{Index: lval},
|
||||
instruction.Call{Index: c.function(opaObjectInsert)},
|
||||
instruction.GetLocal{Index: lval},
|
||||
instruction.SetLocal{Index: lcopy},
|
||||
)
|
||||
inner = block
|
||||
block = nil
|
||||
}
|
||||
|
||||
// Finish by inserting the statement's value into the shallow copied node.
|
||||
instrs = append(instrs, instruction.Block{Instrs: inner})
|
||||
instrs = append(instrs, instruction.GetLocal{Index: lcopy})
|
||||
instrs = append(instrs, instruction.GetLocal{Index: lpath[len(path)-1]})
|
||||
instrs = append(instrs, c.instrRead(value))
|
||||
instrs = append(instrs, instruction.Call{Index: c.function(opaObjectInsert)})
|
||||
|
||||
return instrs
|
||||
return append(instrs,
|
||||
instruction.Block{Instrs: inner},
|
||||
instruction.GetLocal{Index: lcopy},
|
||||
instruction.GetLocal{Index: lpath[len(path)-1]},
|
||||
c.instrRead(value),
|
||||
instruction.Call{Index: c.function(opaObjectInsert)},
|
||||
)
|
||||
}
|
||||
|
||||
func (c *Compiler) compileCallDynamicStmt(stmt *ir.CallDynamicStmt, result *[]instruction.Instruction) error {
|
||||
instrs := []instruction.Instruction{}
|
||||
instrs := make([]instruction.Instruction, 0, 3+3*len(stmt.Path)+len(stmt.Args)+10)
|
||||
larray := c.genLocal()
|
||||
lidx := c.genLocal()
|
||||
|
||||
@@ -1515,7 +1560,7 @@ func (c *Compiler) compileCallStmt(stmt *ir.CallStmt, result *[]instruction.Inst
|
||||
|
||||
func (c *Compiler) compileInternalCall(stmt *ir.CallStmt, index uint32, result *[]instruction.Instruction) error {
|
||||
|
||||
instrs := []instruction.Instruction{}
|
||||
instrs := make([]instruction.Instruction, 0, len(stmt.Args)+4)
|
||||
|
||||
// Prepare function args and call.
|
||||
for _, arg := range stmt.Args {
|
||||
|
||||
-176
@@ -1,176 +0,0 @@
|
||||
// Copyright 2020 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package config implements helper functions to parse OPA's configuration.
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
"github.com/open-policy-agent/opa/internal/strvals"
|
||||
"github.com/open-policy-agent/opa/v1/keys"
|
||||
"github.com/open-policy-agent/opa/v1/logging"
|
||||
"github.com/open-policy-agent/opa/v1/plugins/rest"
|
||||
"github.com/open-policy-agent/opa/v1/tracing"
|
||||
"github.com/open-policy-agent/opa/v1/util"
|
||||
)
|
||||
|
||||
// ServiceOptions stores the options passed to ParseServicesConfig
|
||||
type ServiceOptions struct {
|
||||
Raw json.RawMessage
|
||||
AuthPlugin rest.AuthPluginLookupFunc
|
||||
Keys map[string]*keys.Config
|
||||
Logger logging.Logger
|
||||
DistributedTacingOpts tracing.Options
|
||||
}
|
||||
|
||||
// ParseServicesConfig returns a set of named service clients. The service
|
||||
// clients can be specified either as an array or as a map. Some systems (e.g.,
|
||||
// Helm) do not have proper support for configuration values nested under
|
||||
// arrays, so just support both here.
|
||||
func ParseServicesConfig(opts ServiceOptions) (map[string]rest.Client, error) {
|
||||
|
||||
services := map[string]rest.Client{}
|
||||
|
||||
var arr []json.RawMessage
|
||||
var obj map[string]json.RawMessage
|
||||
|
||||
if err := util.Unmarshal(opts.Raw, &arr); err == nil {
|
||||
for _, s := range arr {
|
||||
client, err := rest.New(s, opts.Keys, rest.AuthPluginLookup(opts.AuthPlugin), rest.Logger(opts.Logger), rest.DistributedTracingOpts(opts.DistributedTacingOpts))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
services[client.Service()] = client
|
||||
}
|
||||
} else if util.Unmarshal(opts.Raw, &obj) == nil {
|
||||
for k := range obj {
|
||||
client, err := rest.New(obj[k], opts.Keys, rest.Name(k), rest.AuthPluginLookup(opts.AuthPlugin), rest.Logger(opts.Logger), rest.DistributedTracingOpts(opts.DistributedTacingOpts))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
services[client.Service()] = client
|
||||
}
|
||||
} else {
|
||||
// Return error from array decode as that is the default format.
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return services, nil
|
||||
}
|
||||
|
||||
// Load implements configuration file loading. The supplied config file will be
|
||||
// read from disk (if specified) and overrides will be applied. If no config file is
|
||||
// specified, the overrides can still be applied to an empty config.
|
||||
func Load(configFile string, overrides []string, overrideFiles []string) ([]byte, error) {
|
||||
baseConf := map[string]any{}
|
||||
|
||||
// User specified config file
|
||||
if configFile != "" {
|
||||
var bytes []byte
|
||||
var err error
|
||||
bytes, err = os.ReadFile(configFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
processedConf := subEnvVars(string(bytes))
|
||||
|
||||
if err := yaml.Unmarshal([]byte(processedConf), &baseConf); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse %s: %s", configFile, err)
|
||||
}
|
||||
}
|
||||
|
||||
overrideConf := map[string]any{}
|
||||
|
||||
// User specified a config override via --set
|
||||
for _, override := range overrides {
|
||||
processedOverride := subEnvVars(override)
|
||||
if err := strvals.ParseInto(processedOverride, overrideConf); err != nil {
|
||||
return nil, fmt.Errorf("failed parsing --set data: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// User specified a config override value via --set-file
|
||||
for _, override := range overrideFiles {
|
||||
reader := func(rs []rune) (any, error) {
|
||||
bytes, err := os.ReadFile(string(rs))
|
||||
value := strings.TrimSpace(string(bytes))
|
||||
return value, err
|
||||
}
|
||||
if err := strvals.ParseIntoFile(override, overrideConf, reader); err != nil {
|
||||
return nil, fmt.Errorf("failed parsing --set-file data: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Merge together base config file and overrides, prefer the overrides
|
||||
conf := mergeValues(baseConf, overrideConf)
|
||||
|
||||
// Take the patched config and marshal back to YAML
|
||||
return yaml.Marshal(conf)
|
||||
}
|
||||
|
||||
// regex looking for ${...} notation strings
|
||||
var envRegex = regexp.MustCompile(`(?U:\${.*})`)
|
||||
|
||||
// SubEnvVars will look for any environment variables in the passed in string
|
||||
// with the syntax of ${VAR_NAME} and replace that string with ENV[VAR_NAME]
|
||||
func SubEnvVars(s string) string {
|
||||
return subEnvVars(s)
|
||||
}
|
||||
|
||||
func subEnvVars(s string) string {
|
||||
updatedConfig := envRegex.ReplaceAllStringFunc(s, func(s string) string {
|
||||
// Trim off the '${' and '}'
|
||||
if len(s) <= 3 {
|
||||
// This should never happen..
|
||||
return ""
|
||||
}
|
||||
varName := s[2 : len(s)-1]
|
||||
|
||||
// Lookup the variable in the environment. We do not
|
||||
// play by bash rules: if its undefined we'll keep it
|
||||
// as-is, it could be replaced somewhere down the line.
|
||||
// If it's set to "", we'll return that.
|
||||
if lu, ok := os.LookupEnv(varName); ok {
|
||||
return lu
|
||||
}
|
||||
return s
|
||||
})
|
||||
|
||||
return updatedConfig
|
||||
}
|
||||
|
||||
// mergeValues will merge source and destination map, preferring values from the source map
|
||||
func mergeValues(dest map[string]any, src map[string]any) map[string]any {
|
||||
for k, v := range src {
|
||||
// If the key doesn't exist already, then just set the key to that value
|
||||
if _, exists := dest[k]; !exists {
|
||||
dest[k] = v
|
||||
continue
|
||||
}
|
||||
nextMap, ok := v.(map[string]any)
|
||||
// If it isn't another map, overwrite the value
|
||||
if !ok {
|
||||
dest[k] = v
|
||||
continue
|
||||
}
|
||||
// Edge case: If the key exists in the destination, but isn't a map
|
||||
destMap, isMap := dest[k].(map[string]any)
|
||||
// If the source map has a map for this key, prefer it
|
||||
if !isMap {
|
||||
dest[k] = v
|
||||
continue
|
||||
}
|
||||
// If we got to this point, it is a map in both, so merge them
|
||||
dest[k] = mergeValues(destMap, nextMap)
|
||||
}
|
||||
return dest
|
||||
}
|
||||
+21
-4
@@ -2,10 +2,12 @@
|
||||
// which supports lookups, sets, appends, insertions, and deletions.
|
||||
package bitvector
|
||||
|
||||
import "slices"
|
||||
|
||||
// A BitVector is a variable sized vector of bits. It supports
|
||||
// lookups, sets, appends, insertions, and deletions.
|
||||
//
|
||||
// This class is not thread safe.
|
||||
// Operations are not thread safe.
|
||||
type BitVector struct {
|
||||
data []byte
|
||||
length int
|
||||
@@ -14,10 +16,25 @@ type BitVector struct {
|
||||
// NewBitVector creates and initializes a new bit vector with length
|
||||
// elements, using data as its initial contents.
|
||||
func NewBitVector(data []byte, length int) *BitVector {
|
||||
return &BitVector{
|
||||
data: data,
|
||||
length: length,
|
||||
return &BitVector{data: data, length: length}
|
||||
}
|
||||
|
||||
func (vector *BitVector) Clear() *BitVector {
|
||||
if vector == nil {
|
||||
return nil
|
||||
}
|
||||
clear(vector.data)
|
||||
vector.length = 0
|
||||
|
||||
return vector
|
||||
}
|
||||
|
||||
func (vector *BitVector) Reset(size, length int) *BitVector {
|
||||
clear(vector.data)
|
||||
vector.data = slices.Grow(vector.data, size)[:size]
|
||||
vector.length = length
|
||||
|
||||
return vector
|
||||
}
|
||||
|
||||
// Bytes returns a slice of the contents of the bit vector. If the caller changes the returned slice,
|
||||
|
||||
+158
-98
@@ -148,13 +148,17 @@ package edittree
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/open-policy-agent/opa/internal/edittree/bitvector"
|
||||
"github.com/open-policy-agent/opa/v1/ast"
|
||||
"github.com/open-policy-agent/opa/v1/util"
|
||||
)
|
||||
|
||||
var refPool = util.NewSlicePool[*ast.Term](1)
|
||||
|
||||
var editTreePool = util.NewSyncPool[EditTree]()
|
||||
|
||||
// Deletions are encoded with a nil value pointer.
|
||||
type EditTree struct {
|
||||
value *ast.Term
|
||||
@@ -171,31 +175,66 @@ func NewEditTree(term *ast.Term) *EditTree {
|
||||
return nil
|
||||
}
|
||||
|
||||
var tree EditTree
|
||||
return initForTerm(&EditTree{}, term)
|
||||
}
|
||||
|
||||
func EditTreeFromPool(term *ast.Term) *EditTree {
|
||||
return initForTerm(editTreePool.Get(), term)
|
||||
}
|
||||
|
||||
func Dispose(e *EditTree) {
|
||||
if e != nil {
|
||||
editTreePool.Put(e.Reset())
|
||||
}
|
||||
}
|
||||
|
||||
func (e *EditTree) Reset() *EditTree {
|
||||
e.value = nil
|
||||
clear(e.childKeys)
|
||||
clear(e.childScalarValues)
|
||||
clear(e.childCompositeValues)
|
||||
|
||||
e.eliminated = e.eliminated.Clear()
|
||||
e.insertions = e.insertions.Clear()
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func initForTerm(tree *EditTree, term *ast.Term) *EditTree {
|
||||
tree.value = term
|
||||
|
||||
switch x := term.Value.(type) {
|
||||
case ast.Object, ast.Set:
|
||||
tree = EditTree{
|
||||
value: term,
|
||||
childKeys: map[int]*ast.Term{},
|
||||
childScalarValues: map[int]*ast.Term{},
|
||||
childCompositeValues: map[int]*EditTree{},
|
||||
if tree.childKeys == nil {
|
||||
tree.childKeys = map[int]*ast.Term{}
|
||||
}
|
||||
if tree.childScalarValues == nil {
|
||||
tree.childScalarValues = map[int]*ast.Term{}
|
||||
}
|
||||
if tree.childCompositeValues == nil {
|
||||
tree.childCompositeValues = map[int]*EditTree{}
|
||||
}
|
||||
case *ast.Array:
|
||||
tree = EditTree{
|
||||
value: term,
|
||||
childScalarValues: map[int]*ast.Term{},
|
||||
childCompositeValues: map[int]*EditTree{},
|
||||
if tree.childScalarValues == nil {
|
||||
tree.childScalarValues = map[int]*ast.Term{}
|
||||
}
|
||||
if tree.childCompositeValues == nil {
|
||||
tree.childCompositeValues = map[int]*EditTree{}
|
||||
}
|
||||
bytesLength := ((x.Len() - 1) / 8) + 1 // How many bytes to use for the bit-vectors.
|
||||
if tree.eliminated == nil {
|
||||
tree.eliminated = bitvector.NewBitVector(make([]byte, bytesLength), x.Len())
|
||||
} else {
|
||||
tree.eliminated = tree.eliminated.Reset(bytesLength, x.Len())
|
||||
}
|
||||
if tree.insertions == nil {
|
||||
tree.insertions = bitvector.NewBitVector(make([]byte, bytesLength), x.Len())
|
||||
default:
|
||||
tree = EditTree{
|
||||
value: term,
|
||||
} else {
|
||||
tree.insertions = tree.insertions.Reset(bytesLength, x.Len())
|
||||
}
|
||||
}
|
||||
|
||||
return &tree
|
||||
return tree
|
||||
}
|
||||
|
||||
// Returns correct (collision-resolved) hash for this term + whether or not
|
||||
@@ -231,17 +270,14 @@ func isComposite(t *ast.Term) bool {
|
||||
}
|
||||
}
|
||||
|
||||
//gcassert:inline
|
||||
func (e *EditTree) setChildKey(hash int, key *ast.Term) {
|
||||
e.childKeys[hash] = key
|
||||
}
|
||||
|
||||
//gcassert:inline
|
||||
func (e *EditTree) setChildScalarValue(hash int, value *ast.Term) {
|
||||
e.childScalarValues[hash] = value
|
||||
}
|
||||
|
||||
//gcassert:inline
|
||||
func (e *EditTree) setChildCompositeValue(hash int, child *EditTree) {
|
||||
e.childCompositeValues[hash] = child
|
||||
}
|
||||
@@ -278,11 +314,10 @@ func (e *EditTree) Insert(key, value *ast.Term) (*EditTree, error) {
|
||||
// We only collapse this Set-typed node if a composite type is involved.
|
||||
if isComposite(key) {
|
||||
// TODO: Investigate re-rendering *only* the immediate composite children.
|
||||
collapsed := e.Render()
|
||||
e.value = collapsed
|
||||
e.childKeys = map[int]*ast.Term{}
|
||||
e.childScalarValues = map[int]*ast.Term{}
|
||||
e.childCompositeValues = map[int]*EditTree{}
|
||||
e.value = e.Render()
|
||||
clear(e.childKeys)
|
||||
clear(e.childScalarValues)
|
||||
clear(e.childCompositeValues)
|
||||
}
|
||||
return e.unsafeInsertSet(key, value), nil
|
||||
case *ast.Array:
|
||||
@@ -301,12 +336,13 @@ func (e *EditTree) Insert(key, value *ast.Term) (*EditTree, error) {
|
||||
}
|
||||
|
||||
func (e *EditTree) unsafeInsertObject(key, value *ast.Term) *EditTree {
|
||||
child := NewEditTree(value)
|
||||
keyHash, found := e.getKeyHash(key)
|
||||
if found {
|
||||
e.deleteChildValue(keyHash)
|
||||
}
|
||||
e.setChildKey(keyHash, key)
|
||||
|
||||
child := NewEditTree(value)
|
||||
if isComposite(value) {
|
||||
e.setChildCompositeValue(keyHash, child)
|
||||
} else {
|
||||
@@ -331,10 +367,9 @@ func (e *EditTree) unsafeInsertSet(key, value *ast.Term) *EditTree {
|
||||
}
|
||||
|
||||
func (e *EditTree) unsafeInsertArray(idx int, value *ast.Term) *EditTree {
|
||||
child := NewEditTree(value)
|
||||
// Collect insertion indexes above the insertion site for rewriting.
|
||||
rewritesScalars := []int{}
|
||||
rewritesComposites := []int{}
|
||||
var rewritesScalars, rewritesComposites []int
|
||||
|
||||
for i := idx; i < e.insertions.Length(); i++ {
|
||||
if e.insertions.Element(i) == 1 {
|
||||
if _, ok := e.childScalarValues[i]; ok {
|
||||
@@ -369,6 +404,8 @@ func (e *EditTree) unsafeInsertArray(idx int, value *ast.Term) *EditTree {
|
||||
} else {
|
||||
e.insertions.Insert(1, idx)
|
||||
}
|
||||
|
||||
child := NewEditTree(value)
|
||||
if isComposite(value) {
|
||||
e.setChildCompositeValue(idx, child)
|
||||
} else {
|
||||
@@ -427,9 +464,9 @@ func (e *EditTree) Delete(key *ast.Term) (*EditTree, error) {
|
||||
// TODO: Investigate re-rendering *only* the immediate composite children.
|
||||
collapsed := e.Render()
|
||||
e.value = collapsed
|
||||
e.childKeys = map[int]*ast.Term{}
|
||||
e.childScalarValues = map[int]*ast.Term{}
|
||||
e.childCompositeValues = map[int]*EditTree{}
|
||||
clear(e.childKeys)
|
||||
clear(e.childScalarValues)
|
||||
clear(e.childCompositeValues)
|
||||
} else {
|
||||
keyHash, found := e.getKeyHash(key)
|
||||
// If child found, replace with delete node. If delete node already existed, error.
|
||||
@@ -539,7 +576,14 @@ func findIndexOfNthZero(n int, bv *bitvector.BitVector) (int, bool) {
|
||||
// Helper function for sets/objects when the key isn't present in either
|
||||
// child map.
|
||||
func (e *EditTree) fallbackDelete(key *ast.Term) (*EditTree, error) {
|
||||
value, err := e.value.Value.Find(ast.Ref{key})
|
||||
// get ref from pool
|
||||
rptr := refPool.Get(1)
|
||||
defer refPool.Put(rptr)
|
||||
|
||||
ref := *rptr
|
||||
ref[0] = key
|
||||
|
||||
value, err := e.value.Value.Find(ref)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot delete child key %v that does not exist", key)
|
||||
}
|
||||
@@ -595,14 +639,14 @@ func (e *EditTree) Unfold(path ast.Ref) (*EditTree, error) {
|
||||
}
|
||||
// Fall back to looking up the key in e.value.
|
||||
// Extend the tree if key is present. Error otherwise.
|
||||
if v, err := x.Find(ast.Ref{path[0]}); err == nil {
|
||||
if v, err := x.Find(path[:1]); err == nil {
|
||||
child, err := e.Insert(path[0], ast.NewTerm(v))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return child.Unfold(path[1:])
|
||||
}
|
||||
return nil, fmt.Errorf("path %v does not exist in object term %v", ast.Ref{path[0]}, e.value.Value)
|
||||
return nil, fmt.Errorf("path %v does not exist in object term %v", path[0], e.value.Value)
|
||||
case ast.Set:
|
||||
// Sets' keys *are* their values, so in order to allow accurate
|
||||
// traversal, we have to collapse the tree beneath this node,
|
||||
@@ -611,12 +655,11 @@ func (e *EditTree) Unfold(path ast.Ref) (*EditTree, error) {
|
||||
if isComposite(key) {
|
||||
collapsed := e.Render()
|
||||
e.value = collapsed
|
||||
e.childKeys = map[int]*ast.Term{}
|
||||
e.childScalarValues = map[int]*ast.Term{}
|
||||
e.childCompositeValues = map[int]*EditTree{}
|
||||
clear(e.childKeys)
|
||||
clear(e.childScalarValues)
|
||||
clear(e.childCompositeValues)
|
||||
} else {
|
||||
keyHash, found := e.getKeyHash(key)
|
||||
if found {
|
||||
if keyHash, found := e.getKeyHash(key); found {
|
||||
if term, ok := e.childScalarValues[keyHash]; ok {
|
||||
child := NewEditTree(term)
|
||||
return child.Unfold(path[1:])
|
||||
@@ -625,14 +668,14 @@ func (e *EditTree) Unfold(path ast.Ref) (*EditTree, error) {
|
||||
}
|
||||
// Fall back to looking up the key in e.value.
|
||||
// Extend the tree if key is present. Error otherwise.
|
||||
if v, err := e.value.Value.Find(ast.Ref{path[0]}); err == nil {
|
||||
if v, err := e.value.Value.Find(path[:1]); err == nil {
|
||||
child, err := e.Insert(path[0], ast.NewTerm(v))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return child.Unfold(path[1:])
|
||||
}
|
||||
return nil, fmt.Errorf("path %v does not exist in set term %v", ast.Ref{path[0]}, e.value.Value)
|
||||
return nil, fmt.Errorf("path %v does not exist in set term %v", path[:1], e.value.Value)
|
||||
case *ast.Array:
|
||||
idx, err := toIndex(e.insertions.Length(), path[0])
|
||||
if err != nil {
|
||||
@@ -647,13 +690,17 @@ func (e *EditTree) Unfold(path ast.Ref) (*EditTree, error) {
|
||||
}
|
||||
|
||||
idxt := ast.InternedTerm(idx)
|
||||
rptr := refPool.Get(1)
|
||||
defer refPool.Put(rptr)
|
||||
|
||||
ref := *rptr
|
||||
ref[0] = idxt
|
||||
|
||||
// Fall back to looking up the key in e.value.
|
||||
// Extend the tree if key is present. Error otherwise.
|
||||
if v, err := x.Find(ast.Ref{idxt}); err == nil {
|
||||
if v, err := x.Find(ref); err == nil {
|
||||
// TODO: Consider a more efficient "Replace" function that special-cases this for arrays instead?
|
||||
_, err := e.Delete(idxt)
|
||||
if err != nil {
|
||||
if _, err := e.Delete(idxt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
child, err := e.Insert(idxt, ast.NewTerm(v))
|
||||
@@ -662,10 +709,10 @@ func (e *EditTree) Unfold(path ast.Ref) (*EditTree, error) {
|
||||
}
|
||||
return child.Unfold(path[1:])
|
||||
}
|
||||
return nil, fmt.Errorf("path %v does not exist in array term %v", ast.Ref{ast.IntNumberTerm(idx)}, e.value.Value)
|
||||
return nil, fmt.Errorf("path %v does not exist in array term %v", ast.InternedTerm(idx), e.value.Value)
|
||||
default:
|
||||
// Catch all primitive types.
|
||||
return nil, fmt.Errorf("expected composite type for path %v, found value: %v (type: %T)", ast.Ref{path[0]}, x, x)
|
||||
return nil, fmt.Errorf("expected composite type for path %v, found value: %v (type: %T)", path[0], x, x)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -787,8 +834,7 @@ func (e *EditTree) Render() *ast.Term {
|
||||
if t, ok := e.childScalarValues[i]; ok {
|
||||
out = append(out, t)
|
||||
} else if child, ok := e.childCompositeValues[i]; ok {
|
||||
t := child.Render()
|
||||
out = append(out, t)
|
||||
out = append(out, child.Render())
|
||||
} else {
|
||||
panic(fmt.Errorf("invalid index %d does not exist in array", i))
|
||||
}
|
||||
@@ -810,9 +856,10 @@ func (e *EditTree) InsertAtPath(path ast.Ref, value *ast.Term) (*EditTree, error
|
||||
|
||||
if len(path) == 0 {
|
||||
e.value = value
|
||||
e.childKeys = map[int]*ast.Term{}
|
||||
e.childScalarValues = map[int]*ast.Term{}
|
||||
e.childCompositeValues = map[int]*EditTree{}
|
||||
clear(e.childKeys)
|
||||
clear(e.childScalarValues)
|
||||
clear(e.childCompositeValues)
|
||||
|
||||
if v, ok := value.Value.(*ast.Array); ok {
|
||||
bytesLength := ((v.Len() - 1) / 8) + 1 // How many bytes to use for the bit-vectors.
|
||||
e.eliminated = bitvector.NewBitVector(make([]byte, bytesLength), v.Len())
|
||||
@@ -921,24 +968,19 @@ func (e *EditTree) Exists(path ast.Ref) bool {
|
||||
// so that we can accurately unfold it again for an update,
|
||||
// once we know that the key we care about is present.
|
||||
if isComposite(key) {
|
||||
collapsed := e.Render()
|
||||
e.value = collapsed
|
||||
e.childKeys = map[int]*ast.Term{}
|
||||
e.childScalarValues = map[int]*ast.Term{}
|
||||
e.childCompositeValues = map[int]*EditTree{}
|
||||
} else {
|
||||
keyHash, found := e.getKeyHash(key)
|
||||
if found {
|
||||
e.value = e.Render()
|
||||
clear(e.childKeys)
|
||||
clear(e.childScalarValues)
|
||||
clear(e.childCompositeValues)
|
||||
} else if keyHash, found := e.getKeyHash(key); found {
|
||||
if _, ok := e.childScalarValues[keyHash]; ok {
|
||||
return len(path) == 1
|
||||
}
|
||||
}
|
||||
}
|
||||
// Fallback if child lookup failed.
|
||||
_, err := e.value.Value.Find(path)
|
||||
return err == nil
|
||||
case *ast.Array:
|
||||
var idx int
|
||||
idx, err := toIndex(e.insertions.Length(), path[0])
|
||||
if err != nil {
|
||||
return false
|
||||
@@ -951,7 +993,16 @@ func (e *EditTree) Exists(path ast.Ref) bool {
|
||||
}
|
||||
// Fallback if child lookup failed.
|
||||
// We have to ensure that the lookup term is a number here, or Find will fail.
|
||||
_, err = x.Find(ast.Ref{ast.InternedTerm(idx)}.Concat(path[1:]))
|
||||
rptr := refPool.Get(len(path))
|
||||
|
||||
ref := *rptr
|
||||
ref[0] = ast.InternedTerm(idx)
|
||||
copy(ref[1:], path[1:])
|
||||
|
||||
_, err = x.Find(ref)
|
||||
|
||||
refPool.Put(rptr)
|
||||
|
||||
return err == nil
|
||||
default:
|
||||
// Catch all primitive types.
|
||||
@@ -978,8 +1029,7 @@ func toIndex(arrayLength int, term *ast.Term) (int, error) {
|
||||
if v == "-" {
|
||||
return arrayLength, nil
|
||||
}
|
||||
num := ast.Number(v)
|
||||
if i, ok = num.Int(); !ok {
|
||||
if i, ok = ast.Number(v).Int(); !ok {
|
||||
return 0, errors.New("invalid string for indexing")
|
||||
}
|
||||
if v != "0" && strings.HasPrefix(string(v), "0") {
|
||||
@@ -1004,6 +1054,14 @@ func (e *EditTree) Filter(paths []ast.Ref) *ast.Term {
|
||||
return nil
|
||||
}
|
||||
|
||||
// term pointer and ref pointer for reuse in lookups and iteration below.
|
||||
tptr, rptr := ast.TermPtrPool.Get(), refPool.Get(1)
|
||||
defer func() {
|
||||
tptr.Value = nil
|
||||
ast.TermPtrPool.Put(tptr)
|
||||
refPool.Put(rptr)
|
||||
}()
|
||||
|
||||
// Separate out keys for this level.
|
||||
// In the event of paths like "a", "a/b", "a/b/c", the "a" path will win out.
|
||||
// Nil keys, such as "" or [], are not permitted. (legacy behavior)
|
||||
@@ -1026,7 +1084,8 @@ func (e *EditTree) Filter(paths []ast.Ref) *ast.Term {
|
||||
renderNow := ast.NewSet(renderNowList...)
|
||||
// Clear everything out of the pathMap that has a renderNow candidate.
|
||||
for k := range pathMap {
|
||||
if renderNow.Contains(ast.NewTerm(k)) {
|
||||
tptr.Value = k
|
||||
if renderNow.Contains(tptr) {
|
||||
delete(pathMap, k)
|
||||
}
|
||||
}
|
||||
@@ -1035,37 +1094,46 @@ func (e *EditTree) Filter(paths []ast.Ref) *ast.Term {
|
||||
switch e.value.Value.(type) {
|
||||
case ast.Object:
|
||||
out := make([][2]*ast.Term, 0, renderNow.Len()+len(pathMap))
|
||||
ref := *rptr
|
||||
|
||||
// Render any finished paths.
|
||||
renderNow.Foreach(func(k *ast.Term) {
|
||||
if e.Exists(ast.Ref{k}) {
|
||||
subtreeResult, _ := e.RenderAtPath(ast.Ref{k})
|
||||
for _, k := range renderNow.Slice() {
|
||||
ref[0] = k
|
||||
if e.Exists(ref) {
|
||||
subtreeResult, _ := e.RenderAtPath(ref)
|
||||
out = append(out, [2]*ast.Term{k, subtreeResult})
|
||||
}
|
||||
})
|
||||
}
|
||||
// Recursively descend remaining paths.
|
||||
for k, p := range pathMap {
|
||||
if e.Exists(ast.Ref{ast.NewTerm(k)}) {
|
||||
child, _ := e.Unfold(ast.Ref{ast.NewTerm(k)})
|
||||
tptr.Value = k
|
||||
ref[0] = tptr
|
||||
if e.Exists(ref) {
|
||||
child, _ := e.Unfold(ref)
|
||||
subtreeResult := child.Filter(p)
|
||||
out = append(out, [2]*ast.Term{ast.NewTerm(k), subtreeResult})
|
||||
}
|
||||
}
|
||||
|
||||
return ast.ObjectTerm(out...)
|
||||
case ast.Set:
|
||||
out := make([]*ast.Term, 0, renderNow.Len()+len(pathMap))
|
||||
ref := *rptr
|
||||
// Render any finished paths.
|
||||
renderNow.Foreach(func(k *ast.Term) {
|
||||
if e.Exists(ast.Ref{k}) {
|
||||
subtreeResult, _ := e.RenderAtPath(ast.Ref{k})
|
||||
for _, k := range renderNow.Slice() {
|
||||
ref[0] = k
|
||||
if e.Exists(ref) {
|
||||
subtreeResult, _ := e.RenderAtPath(ref)
|
||||
out = append(out, subtreeResult)
|
||||
}
|
||||
})
|
||||
}
|
||||
// Recursively descend remaining paths.
|
||||
for k, p := range pathMap {
|
||||
if e.Exists(ast.Ref{ast.NewTerm(k)}) {
|
||||
child, _ := e.Unfold(ast.Ref{ast.NewTerm(k)})
|
||||
subtreeResult := child.Filter(p)
|
||||
out = append(out, subtreeResult)
|
||||
tptr.Value = k
|
||||
ref[0] = tptr
|
||||
if e.Exists(ref) {
|
||||
child, _ := e.Unfold(ref)
|
||||
out = append(out, child.Filter(p))
|
||||
}
|
||||
}
|
||||
return ast.SetTerm(out...)
|
||||
@@ -1073,27 +1141,25 @@ func (e *EditTree) Filter(paths []ast.Ref) *ast.Term {
|
||||
// No early exit here, because we might have just deletes on the
|
||||
// original array. We build a new Array with modified/deleted keys.
|
||||
out := make([]*ast.Term, 0, renderNow.Len()+len(pathMap))
|
||||
// Sort array indexes before descending.
|
||||
idxList := make([]*ast.Term, 0, len(pathMap))
|
||||
renderNow.Foreach(func(k *ast.Term) {
|
||||
idxList = append(idxList, k)
|
||||
})
|
||||
idxList := append(make([]*ast.Term, 0, renderNow.Len()+len(pathMap)), renderNow.Slice()...)
|
||||
for k := range pathMap {
|
||||
idxList = append(idxList, ast.NewTerm(k))
|
||||
}
|
||||
sort.Sort(termSlice(idxList))
|
||||
// Render child or recursively descend as needed.
|
||||
for i := range idxList {
|
||||
|
||||
ref := *rptr
|
||||
|
||||
// Render child or recursively descend sorted indexes as needed.
|
||||
for i := range util.SortedFunc(idxList, ast.TermValueCompare) {
|
||||
k := idxList[i]
|
||||
ref[0] = k
|
||||
if e.Exists(ref) {
|
||||
if renderNow.Contains(k) {
|
||||
if e.Exists(ast.Ref{k}) {
|
||||
subtreeResult, _ := e.RenderAtPath(ast.Ref{k})
|
||||
subtreeResult, _ := e.RenderAtPath(ref)
|
||||
out = append(out, subtreeResult)
|
||||
} else {
|
||||
child, _ := e.Unfold(ref)
|
||||
out = append(out, child.Filter(pathMap[k.Value]))
|
||||
}
|
||||
} else if e.Exists(ast.Ref{k}) {
|
||||
child, _ := e.Unfold(ast.Ref{k})
|
||||
subtreeResult := child.Filter(pathMap[k.Value])
|
||||
out = append(out, subtreeResult)
|
||||
}
|
||||
}
|
||||
return ast.ArrayTerm(out...)
|
||||
@@ -1101,9 +1167,3 @@ func (e *EditTree) Filter(paths []ast.Ref) *ast.Term {
|
||||
return e.value
|
||||
}
|
||||
}
|
||||
|
||||
type termSlice []*ast.Term
|
||||
|
||||
func (s termSlice) Less(i, j int) bool { return ast.Compare(s[i].Value, s[j].Value) < 0 }
|
||||
func (s termSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s termSlice) Len() int { return len(s) }
|
||||
|
||||
+53
-20
@@ -4,39 +4,72 @@ import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// MustWriteTarGz write the list of file names and content
|
||||
// into a tarball.
|
||||
func MustWriteTarGz(files [][2]string) *bytes.Buffer {
|
||||
var buf bytes.Buffer
|
||||
gw := gzip.NewWriter(&buf)
|
||||
defer gw.Close()
|
||||
tw := tar.NewWriter(gw)
|
||||
defer tw.Close()
|
||||
for _, file := range files {
|
||||
if err := WriteFile(tw, file[0], []byte(file[1])); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return &buf
|
||||
type TarGzWriter struct {
|
||||
*tar.Writer
|
||||
|
||||
gw *gzip.Writer
|
||||
}
|
||||
|
||||
// WriteFile adds a file header with content to the given tar writer
|
||||
func WriteFile(tw *tar.Writer, path string, bs []byte) error {
|
||||
func NewTarGzWriter(w io.Writer) *TarGzWriter {
|
||||
gw := gzip.NewWriter(w)
|
||||
tw := tar.NewWriter(gw)
|
||||
|
||||
return &TarGzWriter{
|
||||
Writer: tw,
|
||||
gw: gw,
|
||||
}
|
||||
}
|
||||
|
||||
func (tgw *TarGzWriter) WriteFile(path string, bs []byte) (err error) {
|
||||
hdr := &tar.Header{
|
||||
Name: "/" + strings.TrimLeft(path, "/"),
|
||||
Name: path,
|
||||
Mode: 0600,
|
||||
Typeflag: tar.TypeReg,
|
||||
Size: int64(len(bs)),
|
||||
}
|
||||
|
||||
if err := tw.WriteHeader(hdr); err != nil {
|
||||
if err = tgw.WriteHeader(hdr); err == nil {
|
||||
_, err = tgw.Write(bs)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (tgw *TarGzWriter) WriteJSONFile(path string, v any) error {
|
||||
buf := &bytes.Buffer{}
|
||||
if err := json.NewEncoder(buf).Encode(v); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := tw.Write(bs)
|
||||
return err
|
||||
return tgw.WriteFile(path, buf.Bytes())
|
||||
}
|
||||
|
||||
func (tgw *TarGzWriter) Close() error {
|
||||
return errors.Join(tgw.Writer.Close(), tgw.gw.Close())
|
||||
}
|
||||
|
||||
// MustWriteTarGz writes the list of file names and content into a tarball.
|
||||
// Paths are prefixed with "/".
|
||||
func MustWriteTarGz(files [][2]string) *bytes.Buffer {
|
||||
buf := &bytes.Buffer{}
|
||||
tgw := NewTarGzWriter(buf)
|
||||
defer tgw.Close()
|
||||
|
||||
for _, file := range files {
|
||||
if !strings.HasPrefix(file[0], "/") {
|
||||
file[0] = "/" + file[0]
|
||||
}
|
||||
|
||||
if err := tgw.WriteFile(file[0], []byte(file[1])); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@
|
||||
//
|
||||
// created 26-02-2013
|
||||
|
||||
// nolint: deadcode,unused,varcheck // Package in development (2021).
|
||||
// nolint:unused,varcheck // Package in development (2021).
|
||||
package gojsonschema
|
||||
|
||||
import (
|
||||
|
||||
+2
-2
@@ -1768,7 +1768,7 @@ func (p *Planner) planRef(ref ast.Ref, iter planiter) error {
|
||||
return errors.New("illegal ref: non-var head")
|
||||
}
|
||||
|
||||
if head.Compare(ast.DefaultRootDocument.Value) == 0 {
|
||||
if head.Equal(ast.DefaultRootDocument.Value) {
|
||||
virtual := p.rules.Get(ref[0].Value)
|
||||
base := &baseptr{local: p.vars.GetOrEmpty(ast.DefaultRootDocument.Value.(ast.Var))}
|
||||
return p.planRefData(virtual, base, ref, 1, iter)
|
||||
@@ -2070,7 +2070,7 @@ func (p *Planner) planRefDataExtent(virtual *ruletrie, base *baseptr, iter plani
|
||||
}
|
||||
}
|
||||
if anyKeyNonGround {
|
||||
var rules []*ast.Rule
|
||||
rules := make([]*ast.Rule, 0, len(virtual.Children()))
|
||||
for _, key := range virtual.Children() {
|
||||
// TODO(sr): skip functions
|
||||
rules = append(rules, virtual.Get(key).Rules()...)
|
||||
|
||||
+2
@@ -158,6 +158,8 @@ func SignV4(headers map[string][]string, method string, theURL *url.URL, body []
|
||||
// include the values for the signed headers
|
||||
orderedKeys := util.KeysSorted(headersToSign)
|
||||
for _, k := range orderedKeys {
|
||||
// TODO: fix later
|
||||
//nolint:perfsprint
|
||||
canonicalReq += k + ":" + strings.Join(headersToSign[k], ",") + "\n"
|
||||
}
|
||||
canonicalReq += "\n" // linefeed to terminate headers
|
||||
|
||||
+2
-2
@@ -7,16 +7,16 @@ package ref
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/open-policy-agent/opa/v1/ast"
|
||||
"github.com/open-policy-agent/opa/v1/storage"
|
||||
"github.com/open-policy-agent/opa/v1/util"
|
||||
)
|
||||
|
||||
// ParseDataPath returns a ref from the slash separated path s rooted at data.
|
||||
// All path segments are treated as identifier strings.
|
||||
func ParseDataPath(s string) (ast.Ref, error) {
|
||||
path, ok := storage.ParsePath("/" + strings.TrimPrefix(s, "/"))
|
||||
path, ok := storage.ParsePath(util.WithPrefix(s, "/"))
|
||||
if !ok {
|
||||
return nil, errors.New("invalid path")
|
||||
}
|
||||
|
||||
-218
@@ -1,218 +0,0 @@
|
||||
// Copyright 2020 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package report provides functions to report OPA's version information to an external service and process the response.
|
||||
package report
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/open-policy-agent/opa/internal/semver"
|
||||
"github.com/open-policy-agent/opa/v1/keys"
|
||||
"github.com/open-policy-agent/opa/v1/logging"
|
||||
"github.com/open-policy-agent/opa/v1/version"
|
||||
|
||||
"github.com/open-policy-agent/opa/v1/plugins/rest"
|
||||
"github.com/open-policy-agent/opa/v1/util"
|
||||
)
|
||||
|
||||
// ExternalServiceURL is the base HTTP URL for a github instance used
|
||||
// to query for more recent version.
|
||||
// If not otherwise specified, it will use the hard-coded default, api.github.com.
|
||||
// GHRepo is the repository to use, and defaults to "open-policy-agent/opa"
|
||||
//
|
||||
// Override at build time via:
|
||||
//
|
||||
// -ldflags "-X github.com/open-policy-agent/opa/internal/report.ExternalServiceURL=<url>"
|
||||
// -ldflags "-X github.com/open-policy-agent/opa/internal/report.GHRepo=<url>"
|
||||
//
|
||||
// ExternalServiceURL will be overridden if the OPA_TELEMETRY_SERVICE_URL environment variable
|
||||
// is provided.
|
||||
var ExternalServiceURL = "https://api.github.com"
|
||||
var GHRepo = "open-policy-agent/opa"
|
||||
|
||||
// Reporter reports information such as the version, heap usage about the running OPA instance to an external service
|
||||
type Reporter interface {
|
||||
SendReport(ctx context.Context) (*DataResponse, error)
|
||||
RegisterGatherer(key string, f Gatherer)
|
||||
}
|
||||
|
||||
// Gatherer represents a mechanism to inject additional data in the telemetry report
|
||||
type Gatherer func(ctx context.Context) (any, error)
|
||||
|
||||
// DataResponse represents the data returned by the external service
|
||||
type DataResponse struct {
|
||||
Latest ReleaseDetails `json:"latest"`
|
||||
}
|
||||
|
||||
// ReleaseDetails holds information about the latest OPA release
|
||||
type ReleaseDetails struct {
|
||||
Download string `json:"download,omitempty"` // link to download the OPA release
|
||||
ReleaseNotes string `json:"release_notes,omitempty"` // link to the OPA release notes
|
||||
LatestRelease string `json:"latest_release,omitempty"` // latest OPA released version
|
||||
OPAUpToDate bool `json:"opa_up_to_date,omitempty"` // is running OPA version greater than or equal to the latest released
|
||||
}
|
||||
|
||||
// Options supplies parameters to the reporter.
|
||||
type Options struct {
|
||||
Logger logging.Logger
|
||||
}
|
||||
|
||||
type GHVersionCollector struct {
|
||||
client rest.Client
|
||||
}
|
||||
|
||||
type GHResponse struct {
|
||||
TagName string `json:"tag_name,omitempty"` // latest OPA release tag
|
||||
ReleaseNotes string `json:"html_url,omitempty"` // link to the OPA release notes
|
||||
Download string `json:"assets_url,omitempty"` // link to download the OPA release
|
||||
}
|
||||
|
||||
// New returns an instance of the Reporter
|
||||
func New(opts Options) (Reporter, error) {
|
||||
r := GHVersionCollector{}
|
||||
|
||||
url := cmp.Or(os.Getenv("OPA_TELEMETRY_SERVICE_URL"), ExternalServiceURL)
|
||||
|
||||
restConfig := fmt.Appendf(nil, `{
|
||||
"url": %q,
|
||||
}`, url)
|
||||
|
||||
client, err := rest.New(restConfig, map[string]*keys.Config{}, rest.Logger(opts.Logger))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.client = client
|
||||
|
||||
// heap_usage_bytes is always present, so register it unconditionally
|
||||
r.RegisterGatherer("heap_usage_bytes", readRuntimeMemStats)
|
||||
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
// SendReport sends the telemetry report which includes information such as the OPA version, current memory usage to
|
||||
// the external service
|
||||
func (r *GHVersionCollector) SendReport(ctx context.Context) (*DataResponse, error) {
|
||||
rCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
resp, err := r.client.Do(rCtx, "GET", fmt.Sprintf("/repos/%s/releases/latest", GHRepo))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer util.Close(resp)
|
||||
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
if resp.Body != nil {
|
||||
var result GHResponse
|
||||
err := json.NewDecoder(resp.Body).Decode(&result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return createDataResponse(result)
|
||||
}
|
||||
return nil, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("server replied with HTTP %v", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func createDataResponse(ghResp GHResponse) (*DataResponse, error) {
|
||||
if ghResp.TagName == "" {
|
||||
return nil, errors.New("server response does not contain tag_name")
|
||||
}
|
||||
|
||||
v := strings.TrimPrefix(version.Version, "v")
|
||||
sv, err := semver.NewVersion(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse current version %q: %w", v, err)
|
||||
}
|
||||
|
||||
latestV := strings.TrimPrefix(ghResp.TagName, "v")
|
||||
latestSV, err := semver.NewVersion(latestV)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse latest version %q: %w", latestV, err)
|
||||
}
|
||||
|
||||
isLatest := sv.Compare(*latestSV) >= 0
|
||||
|
||||
// Note: alternatively, we could look through the assets in the GH API response to find a matching asset,
|
||||
// and use its URL. However, this is not guaranteed to be more robust, and wouldn't use the 'openpolicyagent.org' domain.
|
||||
downloadLink := fmt.Sprintf("https://openpolicyagent.org/downloads/%v/opa_%v_%v",
|
||||
ghResp.TagName, runtime.GOOS, runtime.GOARCH)
|
||||
|
||||
if runtime.GOARCH == "arm64" {
|
||||
downloadLink = fmt.Sprintf("%v_static", downloadLink)
|
||||
}
|
||||
|
||||
if strings.HasPrefix(runtime.GOOS, "win") {
|
||||
downloadLink = fmt.Sprintf("%v.exe", downloadLink)
|
||||
}
|
||||
|
||||
return &DataResponse{
|
||||
Latest: ReleaseDetails{
|
||||
Download: downloadLink,
|
||||
ReleaseNotes: ghResp.ReleaseNotes,
|
||||
LatestRelease: ghResp.TagName,
|
||||
OPAUpToDate: isLatest,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (*GHVersionCollector) RegisterGatherer(_ string, _ Gatherer) {
|
||||
// no-op for this implementation
|
||||
}
|
||||
|
||||
// IsSet returns true if dr is populated.
|
||||
func (dr *DataResponse) IsSet() bool {
|
||||
return dr != nil && dr.Latest.LatestRelease != "" && dr.Latest.Download != "" && dr.Latest.ReleaseNotes != ""
|
||||
}
|
||||
|
||||
// Slice returns the dr as a slice of key-value string pairs. If dr is nil, this function returns an empty slice.
|
||||
func (dr *DataResponse) Slice() [][2]string {
|
||||
|
||||
if !dr.IsSet() {
|
||||
return nil
|
||||
}
|
||||
|
||||
return [][2]string{
|
||||
{"Latest Upstream Version", strings.TrimPrefix(dr.Latest.LatestRelease, "v")},
|
||||
{"Download", dr.Latest.Download},
|
||||
{"Release Notes", dr.Latest.ReleaseNotes},
|
||||
}
|
||||
}
|
||||
|
||||
// Pretty returns OPA release information in a human-readable format.
|
||||
func (dr *DataResponse) Pretty() string {
|
||||
if !dr.IsSet() {
|
||||
return ""
|
||||
}
|
||||
|
||||
pairs := dr.Slice()
|
||||
lines := make([]string, 0, len(pairs))
|
||||
|
||||
for _, pair := range pairs {
|
||||
lines = append(lines, fmt.Sprintf("%v: %v", pair[0], pair[1]))
|
||||
}
|
||||
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func readRuntimeMemStats(_ context.Context) (any, error) {
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
return strconv.FormatUint(m.Alloc, 10), nil
|
||||
}
|
||||
-261
@@ -1,261 +0,0 @@
|
||||
// Copyright 2020 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package init is an internal package with helpers for data and policy loading during initialization.
|
||||
package init
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
storedversion "github.com/open-policy-agent/opa/internal/version"
|
||||
"github.com/open-policy-agent/opa/v1/ast"
|
||||
"github.com/open-policy-agent/opa/v1/bundle"
|
||||
"github.com/open-policy-agent/opa/v1/loader"
|
||||
"github.com/open-policy-agent/opa/v1/metrics"
|
||||
"github.com/open-policy-agent/opa/v1/storage"
|
||||
)
|
||||
|
||||
// InsertAndCompileOptions contains the input for the operation.
|
||||
type InsertAndCompileOptions struct {
|
||||
Store storage.Store
|
||||
Txn storage.Transaction
|
||||
Files loader.Result
|
||||
Bundles map[string]*bundle.Bundle
|
||||
MaxErrors int
|
||||
EnablePrintStatements bool
|
||||
ParserOptions ast.ParserOptions
|
||||
BundleActivatorPlugin string
|
||||
}
|
||||
|
||||
// InsertAndCompileResult contains the output of the operation.
|
||||
type InsertAndCompileResult struct {
|
||||
Compiler *ast.Compiler
|
||||
Metrics metrics.Metrics
|
||||
}
|
||||
|
||||
// InsertAndCompile writes data and policy into the store and returns a compiler for the
|
||||
// store contents.
|
||||
func InsertAndCompile(ctx context.Context, opts InsertAndCompileOptions) (*InsertAndCompileResult, error) {
|
||||
if len(opts.Files.Documents) > 0 {
|
||||
if err := opts.Store.Write(ctx, opts.Txn, storage.AddOp, storage.RootPath, opts.Files.Documents); err != nil {
|
||||
return nil, fmt.Errorf("storage error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
policies := make(map[string]*ast.Module, len(opts.Files.Modules))
|
||||
|
||||
for id, parsed := range opts.Files.Modules {
|
||||
policies[id] = parsed.Parsed
|
||||
}
|
||||
|
||||
compiler := ast.NewCompiler().
|
||||
WithDefaultRegoVersion(opts.ParserOptions.RegoVersion).
|
||||
SetErrorLimit(opts.MaxErrors).
|
||||
WithPathConflictsCheck(storage.NonEmpty(ctx, opts.Store, opts.Txn)).
|
||||
WithEnablePrintStatements(opts.EnablePrintStatements)
|
||||
m := metrics.New()
|
||||
|
||||
activation := &bundle.ActivateOpts{
|
||||
Ctx: ctx,
|
||||
Store: opts.Store,
|
||||
Txn: opts.Txn,
|
||||
Compiler: compiler,
|
||||
Metrics: m,
|
||||
Bundles: opts.Bundles,
|
||||
ExtraModules: policies,
|
||||
ParserOptions: opts.ParserOptions,
|
||||
Plugin: opts.BundleActivatorPlugin,
|
||||
}
|
||||
|
||||
err := bundle.Activate(activation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Policies in bundles will have already been added to the store, but
|
||||
// modules loaded outside of bundles will need to be added manually.
|
||||
for id, parsed := range opts.Files.Modules {
|
||||
if err := opts.Store.UpsertPolicy(ctx, opts.Txn, id, parsed.Raw); err != nil {
|
||||
return nil, fmt.Errorf("storage error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Set the version in the store last to prevent data files from overwriting.
|
||||
if err := storedversion.Write(ctx, opts.Store, opts.Txn); err != nil {
|
||||
return nil, fmt.Errorf("storage error: %w", err)
|
||||
}
|
||||
|
||||
return &InsertAndCompileResult{Compiler: compiler, Metrics: m}, nil
|
||||
}
|
||||
|
||||
// LoadPathsResult contains the output loading a set of paths.
|
||||
type LoadPathsResult struct {
|
||||
Bundles map[string]*bundle.Bundle
|
||||
Files loader.Result
|
||||
}
|
||||
|
||||
// WalkPathsResult contains the output loading a set of paths.
|
||||
type WalkPathsResult struct {
|
||||
BundlesLoader []BundleLoader
|
||||
FileDescriptors []*Descriptor
|
||||
}
|
||||
|
||||
// BundleLoader contains information about files in a bundle
|
||||
type BundleLoader struct {
|
||||
DirectoryLoader bundle.DirectoryLoader
|
||||
IsDir bool
|
||||
}
|
||||
|
||||
// Descriptor contains information about a file
|
||||
type Descriptor struct {
|
||||
Root string
|
||||
Path string
|
||||
}
|
||||
|
||||
// LoadPaths reads data and policy from the given paths and returns a set of bundles or
|
||||
// raw loader file results.
|
||||
func LoadPaths(paths []string,
|
||||
filter loader.Filter,
|
||||
asBundle bool,
|
||||
bvc *bundle.VerificationConfig,
|
||||
skipVerify bool,
|
||||
bundleLazyLoading bool,
|
||||
processAnnotations bool,
|
||||
caps *ast.Capabilities,
|
||||
fsys fs.FS) (*LoadPathsResult, error) {
|
||||
return LoadPathsForRegoVersion(ast.RegoV0, paths, filter, asBundle, bvc, skipVerify, bundleLazyLoading, processAnnotations, false, caps, fsys)
|
||||
}
|
||||
|
||||
func LoadPathsForRegoVersion(regoVersion ast.RegoVersion,
|
||||
paths []string,
|
||||
filter loader.Filter,
|
||||
asBundle bool,
|
||||
bvc *bundle.VerificationConfig,
|
||||
skipVerify bool,
|
||||
bundleLazyLoading bool,
|
||||
processAnnotations bool,
|
||||
followSymlinks bool,
|
||||
caps *ast.Capabilities,
|
||||
fsys fs.FS) (*LoadPathsResult, error) {
|
||||
|
||||
if caps == nil {
|
||||
caps = ast.CapabilitiesForThisVersion()
|
||||
}
|
||||
|
||||
// tar.gz files are automatically loaded as bundles
|
||||
var likelyBundles, nonBundlePaths []string
|
||||
if !asBundle {
|
||||
likelyBundles, nonBundlePaths = splitByTarGzExt(paths)
|
||||
paths = likelyBundles
|
||||
}
|
||||
|
||||
var result LoadPathsResult
|
||||
var err error
|
||||
if asBundle || len(likelyBundles) > 0 {
|
||||
result.Bundles = make(map[string]*bundle.Bundle, len(paths))
|
||||
for _, path := range paths {
|
||||
result.Bundles[path], err = loader.NewFileLoader().
|
||||
WithFS(fsys).
|
||||
WithBundleVerificationConfig(bvc).
|
||||
WithSkipBundleVerification(skipVerify).
|
||||
WithBundleLazyLoadingMode(bundleLazyLoading).
|
||||
WithFilter(filter).
|
||||
WithProcessAnnotation(processAnnotations).
|
||||
WithCapabilities(caps).
|
||||
WithRegoVersion(regoVersion).
|
||||
WithFollowSymlinks(followSymlinks).
|
||||
AsBundle(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if asBundle {
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
files, err := loader.NewFileLoader().
|
||||
WithFS(fsys).
|
||||
WithBundleLazyLoadingMode(bundleLazyLoading).
|
||||
WithProcessAnnotation(processAnnotations).
|
||||
WithCapabilities(caps).
|
||||
WithRegoVersion(regoVersion).
|
||||
Filtered(nonBundlePaths, filter)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result.Files = *files
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// splitByTarGzExt splits the paths in 2 groups. Ones with .tar.gz and another with
|
||||
// non .tar.gz extensions.
|
||||
func splitByTarGzExt(paths []string) (targzs []string, nonTargzs []string) {
|
||||
for _, path := range paths {
|
||||
if strings.HasSuffix(path, ".tar.gz") {
|
||||
targzs = append(targzs, path)
|
||||
} else {
|
||||
nonTargzs = append(nonTargzs, path)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// WalkPaths reads data and policy from the given paths and returns a set of bundle directory loaders
|
||||
// or descriptors that contain information about files.
|
||||
func WalkPaths(paths []string, filter loader.Filter, asBundle bool) (*WalkPathsResult, error) {
|
||||
|
||||
var result WalkPathsResult
|
||||
|
||||
if asBundle {
|
||||
result.BundlesLoader = make([]BundleLoader, len(paths))
|
||||
for i, path := range paths {
|
||||
bundleLoader, isDir, err := loader.GetBundleDirectoryLoader(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result.BundlesLoader[i] = BundleLoader{
|
||||
DirectoryLoader: bundleLoader,
|
||||
IsDir: isDir,
|
||||
}
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
result.FileDescriptors = []*Descriptor{}
|
||||
for _, path := range paths {
|
||||
filePaths, err := loader.FilteredPaths([]string{path}, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, fp := range filePaths {
|
||||
// Trim off the root directory and return path as if chrooted
|
||||
cleanedPath := strings.TrimPrefix(fp, path)
|
||||
if path == "." && filepath.Base(fp) == bundle.ManifestExt {
|
||||
cleanedPath = fp
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(cleanedPath, "/") {
|
||||
cleanedPath = "/" + cleanedPath
|
||||
}
|
||||
|
||||
result.FileDescriptors = append(result.FileDescriptors, &Descriptor{
|
||||
Root: path,
|
||||
Path: cleanedPath,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
+193
-196
@@ -14,237 +14,234 @@
|
||||
|
||||
// Semantic Versions http://semver.org
|
||||
|
||||
// Package semver has been vendored from:
|
||||
// This file was originally vendored from:
|
||||
// https://github.com/coreos/go-semver/tree/e214231b295a8ea9479f11b70b35d5acf3556d9b/semver
|
||||
// A number of the original functions of the package have been removed since
|
||||
// they are not required for our built-ins.
|
||||
// There isn't a single line left from the original source today, but being generous about
|
||||
// attribution won't hurt.
|
||||
package semver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/open-policy-agent/opa/v1/util"
|
||||
)
|
||||
|
||||
// reMetaIdentifier matches pre-release and metadata identifiers against the spec requirements
|
||||
var reMetaIdentifier = regexp.MustCompile(`^[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*$`)
|
||||
|
||||
// Version represents a parsed SemVer
|
||||
type Version struct {
|
||||
Major int64
|
||||
Minor int64
|
||||
Patch int64
|
||||
PreRelease PreRelease
|
||||
Metadata string
|
||||
PreRelease string `json:"PreRelease,omitempty"`
|
||||
Metadata string `json:"Metadata,omitempty"`
|
||||
}
|
||||
|
||||
// PreRelease represents a pre-release suffix string
|
||||
type PreRelease string
|
||||
// Parse constructs new semver Version from version string.
|
||||
func Parse(version string) (v Version, err error) {
|
||||
version = strings.TrimPrefix(version, "v")
|
||||
|
||||
func splitOff(input *string, delim string) (val string) {
|
||||
parts := strings.SplitN(*input, delim, 2)
|
||||
|
||||
if len(parts) == 2 {
|
||||
*input = parts[0]
|
||||
val = parts[1]
|
||||
version, v.Metadata = cut(version, '+')
|
||||
if v.Metadata != "" && !reMetaIdentifier.MatchString(v.Metadata) {
|
||||
return v, fmt.Errorf("invalid metadata identifier: %s", v.Metadata)
|
||||
}
|
||||
|
||||
return val
|
||||
version, v.PreRelease = cut(version, '-')
|
||||
if v.PreRelease != "" && !reMetaIdentifier.MatchString(v.PreRelease) {
|
||||
return v, fmt.Errorf("invalid pre-release identifier: %s", v.PreRelease)
|
||||
}
|
||||
|
||||
if strings.Count(version, ".") != 2 {
|
||||
return v, fmt.Errorf("%s should contain major, minor, and patch versions", version)
|
||||
}
|
||||
|
||||
major, after := cut(version, '.')
|
||||
if v.Major, err = strconv.ParseInt(major, 10, 64); err != nil {
|
||||
return v, err
|
||||
}
|
||||
|
||||
minor, after := cut(after, '.')
|
||||
if v.Minor, err = strconv.ParseInt(minor, 10, 64); err != nil {
|
||||
return v, err
|
||||
}
|
||||
|
||||
if v.Patch, err = strconv.ParseInt(after, 10, 64); err != nil {
|
||||
return v, err
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// NewVersion constructs new SemVers from strings
|
||||
func NewVersion(version string) (*Version, error) {
|
||||
v := Version{}
|
||||
|
||||
if err := v.Set(version); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
// Set parses and updates v from the given version string. Implements flag.Value
|
||||
func (v *Version) Set(version string) error {
|
||||
metadata := splitOff(&version, "+")
|
||||
preRelease := PreRelease(splitOff(&version, "-"))
|
||||
dotParts := strings.SplitN(version, ".", 3)
|
||||
|
||||
if len(dotParts) != 3 {
|
||||
return fmt.Errorf("%s is not in dotted-tri format", version)
|
||||
}
|
||||
|
||||
if err := validateIdentifier(string(preRelease)); err != nil {
|
||||
return fmt.Errorf("failed to validate pre-release: %v", err)
|
||||
}
|
||||
|
||||
if err := validateIdentifier(metadata); err != nil {
|
||||
return fmt.Errorf("failed to validate metadata: %v", err)
|
||||
}
|
||||
|
||||
parsed := make([]int64, 3)
|
||||
|
||||
for i, v := range dotParts[:3] {
|
||||
val, err := strconv.ParseInt(v, 10, 64)
|
||||
parsed[i] = val
|
||||
// MustParse is like Parse but panics if the version string is invalid instead of returning an error.
|
||||
func MustParse(version string) Version {
|
||||
v, err := Parse(version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
v.Metadata = metadata
|
||||
v.PreRelease = preRelease
|
||||
v.Major = parsed[0]
|
||||
v.Minor = parsed[1]
|
||||
v.Patch = parsed[2]
|
||||
return nil
|
||||
return v
|
||||
}
|
||||
|
||||
func (v Version) String() string {
|
||||
var buffer bytes.Buffer
|
||||
|
||||
fmt.Fprintf(&buffer, "%d.%d.%d", v.Major, v.Minor, v.Patch)
|
||||
|
||||
if v.PreRelease != "" {
|
||||
fmt.Fprintf(&buffer, "-%s", v.PreRelease)
|
||||
}
|
||||
|
||||
if v.Metadata != "" {
|
||||
fmt.Fprintf(&buffer, "+%s", v.Metadata)
|
||||
}
|
||||
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
// Compare tests if v is less than, equal to, or greater than versionB,
|
||||
// returning -1, 0, or +1 respectively.
|
||||
func (v Version) Compare(versionB Version) int {
|
||||
if cmp := recursiveCompare(v.Slice(), versionB.Slice()); cmp != 0 {
|
||||
return cmp
|
||||
}
|
||||
return preReleaseCompare(v, versionB)
|
||||
}
|
||||
|
||||
// Slice converts the comparable parts of the semver into a slice of integers.
|
||||
func (v Version) Slice() []int64 {
|
||||
return []int64{v.Major, v.Minor, v.Patch}
|
||||
}
|
||||
|
||||
// Slice splits the pre-release suffix string
|
||||
func (p PreRelease) Slice() []string {
|
||||
preRelease := string(p)
|
||||
return strings.Split(preRelease, ".")
|
||||
}
|
||||
|
||||
func preReleaseCompare(versionA Version, versionB Version) int {
|
||||
a := versionA.PreRelease
|
||||
b := versionB.PreRelease
|
||||
|
||||
/* Handle the case where if two versions are otherwise equal it is the
|
||||
* one without a PreRelease that is greater */
|
||||
if len(a) == 0 && (len(b) > 0) {
|
||||
return 1
|
||||
} else if len(b) == 0 && (len(a) > 0) {
|
||||
return -1
|
||||
}
|
||||
|
||||
// If there is a prerelease, check and compare each part.
|
||||
return recursivePreReleaseCompare(a.Slice(), b.Slice())
|
||||
}
|
||||
|
||||
func recursiveCompare(versionA []int64, versionB []int64) int {
|
||||
if len(versionA) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
a := versionA[0]
|
||||
b := versionB[0]
|
||||
|
||||
if a > b {
|
||||
return 1
|
||||
} else if a < b {
|
||||
return -1
|
||||
}
|
||||
|
||||
return recursiveCompare(versionA[1:], versionB[1:])
|
||||
}
|
||||
|
||||
func recursivePreReleaseCompare(versionA []string, versionB []string) int {
|
||||
// A larger set of pre-release fields has a higher precedence than a smaller set,
|
||||
// if all of the preceding identifiers are equal.
|
||||
if len(versionA) == 0 {
|
||||
if len(versionB) > 0 {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
} else if len(versionB) == 0 {
|
||||
// We're longer than versionB so return 1.
|
||||
return 1
|
||||
}
|
||||
|
||||
a := versionA[0]
|
||||
b := versionB[0]
|
||||
|
||||
aInt := false
|
||||
bInt := false
|
||||
|
||||
aI, err := strconv.Atoi(versionA[0])
|
||||
if err == nil {
|
||||
aInt = true
|
||||
}
|
||||
|
||||
bI, err := strconv.Atoi(versionB[0])
|
||||
if err == nil {
|
||||
bInt = true
|
||||
}
|
||||
|
||||
// Numeric identifiers always have lower precedence than non-numeric identifiers.
|
||||
if aInt && !bInt {
|
||||
return -1
|
||||
} else if !aInt && bInt {
|
||||
return 1
|
||||
}
|
||||
|
||||
// Handle Integer Comparison
|
||||
if aInt && bInt {
|
||||
if aI > bI {
|
||||
return 1
|
||||
} else if aI < bI {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
// Handle String Comparison
|
||||
if a > b {
|
||||
return 1
|
||||
} else if a < b {
|
||||
return -1
|
||||
}
|
||||
|
||||
return recursivePreReleaseCompare(versionA[1:], versionB[1:])
|
||||
}
|
||||
|
||||
// validateIdentifier makes sure the provided identifier satisfies semver spec
|
||||
func validateIdentifier(id string) error {
|
||||
if id != "" && !reIdentifier.MatchString(id) {
|
||||
return fmt.Errorf("%s is not a valid semver identifier", id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// reIdentifier is a regular expression used to check that pre-release and metadata
|
||||
// identifiers satisfy the spec requirements
|
||||
var reIdentifier = regexp.MustCompile(`^[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*$`)
|
||||
|
||||
// Compare compares two semver strings.
|
||||
func Compare(a, b string) int {
|
||||
aV, err := NewVersion(strings.TrimPrefix(a, "v"))
|
||||
aV, err := Parse(a)
|
||||
if err != nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
bV, err := NewVersion(strings.TrimPrefix(b, "v"))
|
||||
bV, err := Parse(b)
|
||||
if err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
return aV.Compare(*bV)
|
||||
return aV.Compare(bV)
|
||||
}
|
||||
|
||||
// AppendText appends the textual representation of the version to b and returns the extended buffer.
|
||||
// This method conforms to the encoding.TextAppender interface, and is useful for serializing the Version
|
||||
// without allocating, provided the caller has pre-allocated sufficient space in b.
|
||||
func (v Version) AppendText(b []byte) ([]byte, error) {
|
||||
if b == nil {
|
||||
b = make([]byte, 0, length(v))
|
||||
}
|
||||
|
||||
b = append(strconv.AppendInt(b, v.Major, 10), '.')
|
||||
b = append(strconv.AppendInt(b, v.Minor, 10), '.')
|
||||
b = strconv.AppendInt(b, v.Patch, 10)
|
||||
|
||||
if v.PreRelease != "" {
|
||||
b = append(append(b, '-'), v.PreRelease...)
|
||||
}
|
||||
if v.Metadata != "" {
|
||||
b = append(append(b, '+'), v.Metadata...)
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// String returns the string representation of the version.
|
||||
func (v Version) String() string {
|
||||
bs := make([]byte, 0, length(v))
|
||||
bs, _ = v.AppendText(bs)
|
||||
|
||||
return string(bs)
|
||||
}
|
||||
|
||||
// Compare tests if v is less than, equal to, or greater than other, returning -1, 0, or +1 respectively.
|
||||
// Comparison is based on the SemVer specification (https://semver.org/#spec-item-11).
|
||||
func (v Version) Compare(other Version) int {
|
||||
if v.Major > other.Major {
|
||||
return 1
|
||||
} else if v.Major < other.Major {
|
||||
return -1
|
||||
}
|
||||
|
||||
if v.Minor > other.Minor {
|
||||
return 1
|
||||
} else if v.Minor < other.Minor {
|
||||
return -1
|
||||
}
|
||||
|
||||
if v.Patch > other.Patch {
|
||||
return 1
|
||||
} else if v.Patch < other.Patch {
|
||||
return -1
|
||||
}
|
||||
|
||||
if v.PreRelease == other.PreRelease {
|
||||
return 0
|
||||
}
|
||||
|
||||
// if two versions are otherwise equal it is the one without a pre-release that is greater
|
||||
if v.PreRelease == "" && other.PreRelease != "" {
|
||||
return 1
|
||||
}
|
||||
if other.PreRelease == "" && v.PreRelease != "" {
|
||||
return -1
|
||||
}
|
||||
|
||||
a, afterA := cut(v.PreRelease, '.')
|
||||
b, afterB := cut(other.PreRelease, '.')
|
||||
|
||||
for {
|
||||
if a == "" && b != "" {
|
||||
return -1
|
||||
}
|
||||
if a != "" && b == "" {
|
||||
return 1
|
||||
}
|
||||
|
||||
aIsInt := isAllDecimals(a)
|
||||
bIsInt := isAllDecimals(b)
|
||||
|
||||
// numeric identifiers have lower precedence than non-numeric
|
||||
if aIsInt && !bIsInt {
|
||||
return -1
|
||||
} else if !aIsInt && bIsInt {
|
||||
return 1
|
||||
}
|
||||
|
||||
if aIsInt && bIsInt {
|
||||
aInt, _ := strconv.Atoi(a)
|
||||
bInt, _ := strconv.Atoi(b)
|
||||
|
||||
if aInt > bInt {
|
||||
return 1
|
||||
} else if aInt < bInt {
|
||||
return -1
|
||||
}
|
||||
} else {
|
||||
// string comparison
|
||||
if a > b {
|
||||
return 1
|
||||
} else if a < b {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
// a larger set of pre-release fields has a higher precedence than a
|
||||
// smaller set, if all of the preceding identifiers are equal.
|
||||
if afterA != "" && afterB == "" {
|
||||
return 1
|
||||
} else if afterA == "" && afterB != "" {
|
||||
return -1
|
||||
}
|
||||
|
||||
a, afterA = cut(afterA, '.')
|
||||
b, afterB = cut(afterB, '.')
|
||||
}
|
||||
}
|
||||
|
||||
func isAllDecimals(s string) bool {
|
||||
for _, r := range s {
|
||||
if r < '0' || r > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return s != ""
|
||||
}
|
||||
|
||||
// length allows calculating the length of the version for pre-allocation.
|
||||
func length(v Version) int {
|
||||
n := util.NumDigitsInt64(v.Major) + util.NumDigitsInt64(v.Minor) + util.NumDigitsInt64(v.Patch) + 2
|
||||
if v.PreRelease != "" {
|
||||
n += len(v.PreRelease) + 1
|
||||
}
|
||||
if v.Metadata != "" {
|
||||
n += len(v.Metadata) + 1
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// cut is a *slightly* faster version of strings.Cut only accepting
|
||||
// single byte separators, and skipping the boolean return value.
|
||||
func cut(s string, sep byte) (before, after string) {
|
||||
if i := strings.IndexByte(s, sep); i >= 0 {
|
||||
return s[:i], s[i+1:]
|
||||
}
|
||||
return s, ""
|
||||
}
|
||||
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
Package strvals provides tools for working with strval lines.
|
||||
|
||||
OPA runtime config supports a compressed format for YAML settings which we call strvals.
|
||||
The format is roughly like this:
|
||||
|
||||
name=value,topname.subname=value
|
||||
|
||||
The above is equivalent to the YAML document
|
||||
|
||||
name: value
|
||||
topname:
|
||||
subname: value
|
||||
|
||||
This package provides a parser and utilities for converting the strvals format
|
||||
to other formats.
|
||||
*/
|
||||
package strvals
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user