From 114127063c3e24aa8508528c5f89397ede50cfc9 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Fri, 29 Nov 2024 21:15:23 +0900 Subject: [PATCH 1/9] Use goccy/go-yaml only --- capture/runbook.go | 8 ++++++-- go.mod | 4 ++-- go.sum | 18 ++++-------------- runbook.go | 32 +++++++++++++------------------- runbook_test.go | 2 +- testdata/grpc.yml.runbook.golden | 20 ++++++++++---------- testdata/http.yml.runbook.golden | 19 +------------------ yaml.go | 12 ++++++++++-- 8 files changed, 47 insertions(+), 68 deletions(-) diff --git a/capture/runbook.go b/capture/runbook.go index c9639050..b52e0249 100644 --- a/capture/runbook.go +++ b/capture/runbook.go @@ -13,9 +13,9 @@ import ( "sync" "github.com/goccy/go-json" + "github.com/goccy/go-yaml" "github.com/k1LoW/runn" "google.golang.org/grpc/status" - "gopkg.in/yaml.v2" ) var _ runn.Capturer = (*cRunbook)(nil) @@ -560,7 +560,11 @@ func (c *cRunbook) writeRunbook(trs runn.Trails, bookPath string) { r.Desc = fmt.Sprintf("Captured of %s run", filepath.Base(bookPath)) } r.Labels = c.labels - b, err := yaml.Marshal(r) + encOpts := []yaml.EncodeOption{ + yaml.UseSingleQuote(false), + yaml.UseLiteralStyleIfMultiline(true), + } + b, err := yaml.MarshalWithOptions(r, encOpts...) if err != nil { c.errs = errors.Join(c.errs, fmt.Errorf("failed to yaml.Marshal: %w", err)) return diff --git a/go.mod b/go.mod index fc0f6dbf..d262a3f4 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/gliderlabs/ssh v0.3.7 github.com/go-sql-driver/mysql v1.8.1 github.com/goccy/go-json v0.10.3 - github.com/goccy/go-yaml v1.13.4 + github.com/goccy/go-yaml v1.15.3 github.com/golang-sql/sqlexp v0.1.0 github.com/google/go-cmp v0.6.0 github.com/google/go-github/v58 v58.0.0 @@ -39,7 +39,7 @@ require ( github.com/k1LoW/donegroup v1.10.2 github.com/k1LoW/duration v1.2.0 github.com/k1LoW/exec v0.3.0 - github.com/k1LoW/expand v0.13.0 + github.com/k1LoW/expand v0.14.0 github.com/k1LoW/ghfs v1.3.1 github.com/k1LoW/go-github-client/v58 v58.0.12 github.com/k1LoW/grpcstub v0.25.0 diff --git a/go.sum b/go.sum index 129a1177..67fcbb0c 100644 --- a/go.sum +++ b/go.sum @@ -797,8 +797,6 @@ github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWo github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fullstorydev/grpcurl v1.8.9 h1:JMvZXK8lHDGyLmTQ0ZdGDnVVGuwjbpaumf8p42z0d+c= github.com/fullstorydev/grpcurl v1.8.9/go.mod h1:PNNKevV5VNAV2loscyLISrEnWQI61eqR0F8l3bVadAA= -github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= -github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE= github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8= @@ -819,12 +817,6 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-pdf/fpdf v0.5.0/go.mod h1:HzcnA+A23uwogo0tp9yU+l3V+KXhiESpt1PMayhOh5M= github.com/go-pdf/fpdf v0.6.0/go.mod h1:HzcnA+A23uwogo0tp9yU+l3V+KXhiESpt1PMayhOh5M= -github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= -github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= -github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= -github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA= -github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= @@ -837,8 +829,8 @@ github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakr github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= -github.com/goccy/go-yaml v1.13.4 h1:XOnLX9GqT+kH/gB7YzCMUiDBFU9B7pm3HZz6kyeDPkk= -github.com/goccy/go-yaml v1.13.4/go.mod h1:IjYwxUiJDoqpx2RmbdjMUceGHZwYLon3sfOGl5Hi9lc= +github.com/goccy/go-yaml v1.15.3 h1:wQ4UwLFkgbSazdi+i9AVmZE3vKTktlNlI2kQqXo5L+I= +github.com/goccy/go-yaml v1.15.3/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= @@ -1024,8 +1016,8 @@ github.com/k1LoW/duration v1.2.0 h1:qq1gWtPh7YROFyerBufVP+ATR11mOOHDInrcC/Xe/6A= github.com/k1LoW/duration v1.2.0/go.mod h1:qUa0NptIiUl5EUsCc8wIiSaHuNjS4wmpYNMHp0l6pos= github.com/k1LoW/exec v0.3.0 h1:lNUqhF5IXhm2aDKcaWxhGwYC/WLcCbLOqoe97oxW1XQ= github.com/k1LoW/exec v0.3.0/go.mod h1:LSd4t5/1qGJHUdB2RUtoHuHfaZ3ks+BfQ+sGHzvwhnE= -github.com/k1LoW/expand v0.13.0 h1:ru9NQ6qntnS/TzE7QmxsQN+DUnpreZEuxGVD+72+3Fs= -github.com/k1LoW/expand v0.13.0/go.mod h1:LU8mfXnSF+DrzrAts+0L6YU5+ySVTWt33hUmOvR50A8= +github.com/k1LoW/expand v0.14.0 h1:jmGOxEC/qmB+0oT44H4S9AA5UpJes4SZ/cguVbnNzGc= +github.com/k1LoW/expand v0.14.0/go.mod h1:dTRPh19IUlp+whqWo3kZRWO+qf/6/EMbJyS98AuUDE4= github.com/k1LoW/ghfs v1.3.1 h1:xq/VenCDhSUDf1IsL0LBfWIn3QTCOOd8+pE0SNZF6wM= github.com/k1LoW/ghfs v1.3.1/go.mod h1:IaAooDKpTdx/I0lSethYNMowo0rs0h/+btPPHdZlnE4= github.com/k1LoW/go-github-client/v58 v58.0.12 h1:rIK+H2cSpK0ptOxNq0jITGu3ffRjLTe5ITCz1jik5c0= @@ -1071,8 +1063,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kULo2bwGEkFvCePZ3qHDDTC3/J9Swo= github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs= -github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= -github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/lestrrat-go/backoff/v2 v2.0.8 h1:oNb5E5isby2kiro9AgdHLv5N5tint1AnDVVf2E2un5A= github.com/lestrrat-go/backoff/v2 v2.0.8/go.mod h1:rHP/q/r9aT27n24JQLa7JhSQZCKBBOiM/uP402WwN8Y= github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I= diff --git a/runbook.go b/runbook.go index 7ddc89df..6707bc34 100644 --- a/runbook.go +++ b/runbook.go @@ -10,7 +10,7 @@ import ( "strings" "github.com/Songmu/axslogparser" - goyaml "github.com/goccy/go-yaml" + "github.com/goccy/go-yaml" "github.com/goccy/go-yaml/ast" "github.com/goccy/go-yaml/lexer" "github.com/goccy/go-yaml/parser" @@ -18,7 +18,6 @@ import ( "github.com/k1LoW/curlreq" "github.com/k1LoW/expand" "github.com/k1LoW/grpcurlreq" - "gopkg.in/yaml.v2" ) type position struct { @@ -111,13 +110,8 @@ func parseRunbook(b []byte) (*runbook, error) { return nil, err } - flattened, err := flattenYamlAliases([]byte(rep)) - if err != nil { - return nil, err - } - - if err := yaml.Unmarshal(flattened, rb); err != nil { - if err := parseRunbookMapped(flattened, rb); err != nil { + if err := yaml.UnmarshalWithOptions([]byte(rep), rb, yaml.UseOrderedMap()); err != nil { + if err := parseRunbookMapped([]byte(rep), rb); err != nil { return nil, err } } @@ -130,25 +124,25 @@ func parseRunbook(b []byte) (*runbook, error) { } func flattenYamlAliases(in []byte) ([]byte, error) { - decOpts := []goyaml.DecodeOption{ - goyaml.UseOrderedMap(), + decOpts := []yaml.DecodeOption{ + yaml.UseOrderedMap(), } - encOpts := []goyaml.EncodeOption{ - goyaml.Flow(false), - goyaml.UseSingleQuote(false), - goyaml.UseLiteralStyleIfMultiline(false), - goyaml.IndentSequence(false), + encOpts := []yaml.EncodeOption{ + yaml.Flow(false), + yaml.UseSingleQuote(true), + yaml.UseLiteralStyleIfMultiline(true), + yaml.IndentSequence(false), } var tmp any - err := goyaml.UnmarshalWithOptions(in, &tmp, decOpts...) + err := yaml.UnmarshalWithOptions(in, &tmp, decOpts...) if err != nil { return nil, err } - flattened, err := goyaml.MarshalWithOptions(tmp, encOpts...) + flattened, err := yaml.MarshalWithOptions(tmp, encOpts...) if err != nil { return nil, err } @@ -158,7 +152,7 @@ func flattenYamlAliases(in []byte) ([]byte, error) { func parseRunbookMapped(b []byte, rb *runbook) error { m := &runbookMapped{} - if err := yaml.Unmarshal(b, m); err != nil { + if err := yaml.UnmarshalWithOptions(b, m, yaml.UseOrderedMap()); err != nil { return err } rb.useMap = true diff --git a/runbook_test.go b/runbook_test.go index 4a35a749..16e6d0b4 100644 --- a/runbook_test.go +++ b/runbook_test.go @@ -9,11 +9,11 @@ import ( "strings" "testing" + "github.com/goccy/go-yaml" "github.com/goccy/go-yaml/token" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/tenntenn/golden" - "gopkg.in/yaml.v2" ) func TestParseRunbook(t *testing.T) { diff --git a/testdata/grpc.yml.runbook.golden b/testdata/grpc.yml.runbook.golden index 718111ee..c7aa328c 100644 --- a/testdata/grpc.yml.runbook.golden +++ b/testdata/grpc.yml.runbook.golden @@ -3,10 +3,10 @@ desc: Captured of grpc.yml run runners: greq: addr: ${TEST_GRPC_ADDR-:grpc.example.com:443} - tls: true - skipVerify: true bufDirs: - ../ + skipVerify: true + tls: true steps: - greq: grpctest.GrpcTestService/Hello: @@ -18,7 +18,7 @@ steps: message: name: alice num: 3 - request_time: "2022-06-25T05:24:43.861872Z" + request_time: 2022-06-25T05:24:43.861872Z test: | current.res.headers["content-type"][0] == "application/grpc" && current.res.headers["hello"][0] == "header" @@ -40,7 +40,7 @@ steps: message: name: alice num: 3 - request_time: "2022-06-25T05:24:43.861872Z" + request_time: 2022-06-25T05:24:43.861872Z test: | current.res.trailers["content-type"][0] == "application/grpc" && current.res.status == 1 @@ -51,7 +51,7 @@ steps: message: name: bob num: 4 - request_time: "2022-06-25T05:24:43.861872Z" + request_time: 2022-06-25T05:24:43.861872Z test: | compare(current.res.messages[0], {"create_time":"2022-06-25T05:24:43.861872Z","message":"hello","num":33}) && compare(current.res.messages[1], {"create_time":"2022-06-25T05:24:44.382783Z","message":"hello","num":34}) @@ -66,10 +66,10 @@ steps: messages: - name: alice num: 5 - request_time: "2022-06-25T05:24:43.861872Z" + request_time: 2022-06-25T05:24:43.861872Z - name: bob num: 6 - request_time: "2022-06-25T05:24:43.861872Z" + request_time: 2022-06-25T05:24:43.861872Z test: | compare(current.res.message, {"create_time":"2022-06-25T05:24:45.382783Z","message":"hello","num":35}) && current.res.headers["content-type"][0] == "application/grpc" @@ -83,14 +83,14 @@ steps: messages: - name: alice num: 7 - request_time: "2022-06-25T05:24:43.861872Z" + request_time: 2022-06-25T05:24:43.861872Z - receive - name: bob num: 8 - request_time: "2022-06-25T05:24:43.861872Z" + request_time: 2022-06-25T05:24:43.861872Z - name: charlie num: 9 - request_time: "2022-06-25T05:24:43.861872Z" + request_time: 2022-06-25T05:24:43.861872Z - close test: | current.res.headers["content-type"][0] == "application/grpc" diff --git a/testdata/http.yml.runbook.golden b/testdata/http.yml.runbook.golden index 7304803c..499ec4d5 100644 --- a/testdata/http.yml.runbook.golden +++ b/testdata/http.yml.runbook.golden @@ -115,24 +115,7 @@ steps: /upload: post: body: - application/octet-stream: !!binary | - /9j/4AAQSkZJRgABAQEAYABgAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZy - BJSkcgSlBFRyB2ODApLCBkZWZhdWx0IHF1YWxpdHkK/9sAQwAIBgYHBgUIBwcHCQkICgwU - DQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQw - EJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy - MjIyMjIyMjIyMjIy/8AAEQgAKAA8AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAA - ABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQy - gZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWV - pjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrC - w8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQ - EAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEH - YXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSl - NUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKz - tLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQ - MRAD8A+f6KKKACiiigAooooAKKKKACiiigDp5UtWgf7ELASJKiQGeSAh4cHLEEDB/1ed2W - 5bGMNWWkOmyG8YyFArsbVS2PMHOA3HHY579O+5cyigDes005dOQedaST+YrASAht5R8Akg - DYGCZ5x1J4PGbqQUTRf6nzvLHneTt2bsnGNvy/d25x3zVOigAooooAKKKKACiiigAooooA - KKKKAP/Z + application/octet-stream: /9j/4AAQSkZJRgABAQEAYABgAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBkZWZhdWx0IHF1YWxpdHkK/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgAKAA8AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8A+f6KKKACiiigAooooAKKKKACiiigDp5UtWgf7ELASJKiQGeSAh4cHLEEDB/1ed2W5bGMNWWkOmyG8YyFArsbVS2PMHOA3HHY579O+5cyigDes005dOQedaST+YrASAht5R8AkgDYGCZ5x1J4PGbqQUTRf6nzvLHneTt2bsnGNvy/d25x3zVOigAooooAKKKKACiiigAooooAKKKKAP/Z test: | current.res.status == 201 && current.res.headers["Content-Length"][0] == "15" diff --git a/yaml.go b/yaml.go index 05e3c45c..0869ad9d 100644 --- a/yaml.go +++ b/yaml.go @@ -2,6 +2,7 @@ package runn import ( "bytes" + "encoding/base64" "encoding/json" "errors" "fmt" @@ -11,7 +12,7 @@ import ( "net/url" "strings" - "gopkg.in/yaml.v2" + "github.com/goccy/go-yaml" ) // CreateHTTPStepMapSlice creates yaml.MapSlice from *http.Request. @@ -138,9 +139,16 @@ func CreateHTTPStepMapSlice(key string, req *http.Request) (yaml.MapSlice, error bd = yaml.MapSlice{ {Key: MediaTypeMultipartFormData, Value: f}, } + case strings.Contains(contentType, MediaTypeApplicationOctetStream): + b, err := io.ReadAll(save) + if err != nil { + return nil, fmt.Errorf("failed to io.ReadAll: %w", err) + } + bd = yaml.MapSlice{ + {Key: contentType, Value: base64.StdEncoding.EncodeToString(b)}, + } default: // case contentType == runn.MediaTypeTextPlain: - // case contentType == runn.MediaTypeApplicationOctetStream b, err := io.ReadAll(save) if err != nil { return nil, fmt.Errorf("failed to io.ReadAll: %w", err) From 6953847da15daead7e6c5c46b1e26ea7a0010c7b Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sat, 30 Nov 2024 15:58:19 +0900 Subject: [PATCH 2/9] Fix test --- go.mod | 2 +- go.sum | 4 +- runbook.go | 47 +++++++++++++------ runbook_test.go | 9 +++- testdata/book/grpc.yml | 2 +- testdata/book/only_if_included.yml | 2 +- testdata/grpc_command.append_step.golden | 2 +- .../pick_step.single_step_map.yml.0.golden | 1 + .../pick_step.yaml_anchor_alias.yml.0.golden | 3 +- ...l_anchor_alias_always_failure.yml.0.golden | 3 +- 10 files changed, 49 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index d262a3f4..5d66e3f4 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/gliderlabs/ssh v0.3.7 github.com/go-sql-driver/mysql v1.8.1 github.com/goccy/go-json v0.10.3 - github.com/goccy/go-yaml v1.15.3 + github.com/goccy/go-yaml v1.15.4 github.com/golang-sql/sqlexp v0.1.0 github.com/google/go-cmp v0.6.0 github.com/google/go-github/v58 v58.0.0 diff --git a/go.sum b/go.sum index 67fcbb0c..0e0c617d 100644 --- a/go.sum +++ b/go.sum @@ -829,8 +829,8 @@ github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakr github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= -github.com/goccy/go-yaml v1.15.3 h1:wQ4UwLFkgbSazdi+i9AVmZE3vKTktlNlI2kQqXo5L+I= -github.com/goccy/go-yaml v1.15.3/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/goccy/go-yaml v1.15.4 h1:Xk5iHVvnsv6KD0HrMuZ0CiJbfdR9KUYQw7U/t9binGQ= +github.com/goccy/go-yaml v1.15.4/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= diff --git a/runbook.go b/runbook.go index 6707bc34..e9afae41 100644 --- a/runbook.go +++ b/runbook.go @@ -59,6 +59,8 @@ type runbook struct { stepKeys []string } +type runbookListed runbook + type runbookMapped struct { Desc string `yaml:"desc,omitempty"` Labels []string `yaml:"labels,omitempty"` @@ -78,6 +80,14 @@ type runbookMapped struct { Trace bool `yaml:"trace,omitempty"` } +var decOpts = []yaml.DecodeOption{ + yaml.UseOrderedMap(), +} + +var encOpts = []yaml.EncodeOption{ + yaml.UseLiteralStyleIfMultiline(true), +} + func NewRunbook(desc string) *runbook { const defaultDesc = "Generated by `runn new`" if desc == "" { @@ -110,7 +120,7 @@ func parseRunbook(b []byte) (*runbook, error) { return nil, err } - if err := yaml.UnmarshalWithOptions([]byte(rep), rb, yaml.UseOrderedMap()); err != nil { + if err := yaml.UnmarshalWithOptions([]byte(rep), rb, decOpts...); err != nil { if err := parseRunbookMapped([]byte(rep), rb); err != nil { return nil, err } @@ -124,17 +134,6 @@ func parseRunbook(b []byte) (*runbook, error) { } func flattenYamlAliases(in []byte) ([]byte, error) { - decOpts := []yaml.DecodeOption{ - yaml.UseOrderedMap(), - } - - encOpts := []yaml.EncodeOption{ - yaml.Flow(false), - yaml.UseSingleQuote(true), - yaml.UseLiteralStyleIfMultiline(true), - yaml.IndentSequence(false), - } - var tmp any err := yaml.UnmarshalWithOptions(in, &tmp, decOpts...) @@ -152,7 +151,7 @@ func flattenYamlAliases(in []byte) ([]byte, error) { func parseRunbookMapped(b []byte, rb *runbook) error { m := &runbookMapped{} - if err := yaml.UnmarshalWithOptions(b, m, yaml.UseOrderedMap()); err != nil { + if err := yaml.UnmarshalWithOptions(b, m, decOpts...); err != nil { return err } rb.useMap = true @@ -218,7 +217,27 @@ func (rb *runbook) AppendStep(in ...string) error { func (rb *runbook) MarshalYAML() (any, error) { if !rb.useMap { - return rb, nil + return &runbookListed{ + Desc: rb.Desc, + Labels: rb.Labels, + Needs: rb.Needs, + Runners: rb.Runners, + Vars: rb.Vars, + Secrets: rb.Secrets, + Steps: rb.Steps, + HostRules: rb.HostRules, + Debug: rb.Debug, + Interval: rb.Interval, + If: rb.If, + SkipTest: rb.SkipTest, + Loop: rb.Loop, + Concurrency: rb.Concurrency, + Force: rb.Force, + Trace: rb.Trace, + + useMap: rb.useMap, + stepKeys: rb.stepKeys, + }, nil } if len(rb.stepKeys) != len(rb.Steps) { return nil, errors.New("invalid runbook") diff --git a/runbook_test.go b/runbook_test.go index 16e6d0b4..4f2bf9cd 100644 --- a/runbook_test.go +++ b/runbook_test.go @@ -25,6 +25,10 @@ func TestParseRunbook(t *testing.T) { if es.IsDir() || !strings.HasSuffix(es.Name(), ".yml") { continue } + if es.Name() == "include_vars_main.yml" { + // NOTICE: include_vars_main.yml is not supported. because go-yaml unwraps quotes when it determines that a tag is a string + continue + } t.Run(es.Name(), func(t *testing.T) { path := filepath.Join("testdata", "book", es.Name()) f, err := os.Open(path) @@ -43,7 +47,7 @@ func TestParseRunbook(t *testing.T) { if len(rb.Vars) == 0 && len(rb.Runners) == 0 && len(rb.Steps) == 0 { t.Error("want vars or runners or steps") } - b, err := yaml.Marshal(rb) + b, err := yaml.MarshalWithOptions(rb, encOpts...) if err != nil { t.Error(err) } @@ -51,6 +55,7 @@ func TestParseRunbook(t *testing.T) { if err != nil { t.Error(err) } + if diff := cmp.Diff(rb, rb2, cmp.AllowUnexported(runbook{})); diff != "" { t.Error(diff) } @@ -99,7 +104,7 @@ func TestAppendStep(t *testing.T) { } got := new(bytes.Buffer) - enc := yaml.NewEncoder(got) + enc := yaml.NewEncoder(got, encOpts...) if err := enc.Encode(rb); err != nil { t.Error(err) } diff --git a/testdata/book/grpc.yml b/testdata/book/grpc.yml index 56170354..36a364e1 100644 --- a/testdata/book/grpc.yml +++ b/testdata/book/grpc.yml @@ -34,7 +34,7 @@ steps: message: field_bytes: "{{ toBase64('hello') }}" # Base64 encoding to pass bytes type test: | - steps.field_types.res.status == 0 && fromBase64(steps.field_types.res.message.field_bytes) == 'hello' + steps.field_types.res.status == 0 && fromBase64(steps.field_types.res.message.field_bytes) == 'hello' error_message: desc: Get gRPC error message greq: diff --git a/testdata/book/only_if_included.yml b/testdata/book/only_if_included.yml index 3b044eaa..827eaa4a 100644 --- a/testdata/book/only_if_included.yml +++ b/testdata/book/only_if_included.yml @@ -5,4 +5,4 @@ steps: test: 'false' bindvars: bind: - run: '"ok"' + run: '{{ "ok" }}' diff --git a/testdata/grpc_command.append_step.golden b/testdata/grpc_command.append_step.golden index 97a654ea..b2092992 100644 --- a/testdata/grpc_command.append_step.golden +++ b/testdata/grpc_command.append_step.golden @@ -5,7 +5,7 @@ steps: - greq: my.custom.server.Service/Method: message: - id: 1234 + id: 1234.0 tags: - foo - bar diff --git a/testdata/pick_step.single_step_map.yml.0.golden b/testdata/pick_step.single_step_map.yml.0.golden index e0712f32..04966ef6 100644 --- a/testdata/pick_step.single_step_map.yml.0.golden +++ b/testdata/pick_step.single_step_map.yml.0.golden @@ -1,2 +1,3 @@ +7 the_only_step: 8 test: | 9 vars.hello == "hello" \ No newline at end of file diff --git a/testdata/pick_step.yaml_anchor_alias.yml.0.golden b/testdata/pick_step.yaml_anchor_alias.yml.0.golden index 4531e320..c28db1c6 100644 --- a/testdata/pick_step.yaml_anchor_alias.yml.0.golden +++ b/testdata/pick_step.yaml_anchor_alias.yml.0.golden @@ -1,4 +1,3 @@ 34 check_string_alias_in_vars: 35 test: | -36 vars.string_alias == "abcdefg" -37 \ No newline at end of file +36 vars.string_alias == "abcdefg" \ No newline at end of file diff --git a/testdata/pick_step.yaml_anchor_alias_always_failure.yml.0.golden b/testdata/pick_step.yaml_anchor_alias_always_failure.yml.0.golden index 1e199462..9df7dab2 100644 --- a/testdata/pick_step.yaml_anchor_alias_always_failure.yml.0.golden +++ b/testdata/pick_step.yaml_anchor_alias_always_failure.yml.0.golden @@ -1,5 +1,4 @@ 24 check_1: 25 desc: 'with anchor & alias' 26 test: | -27 compare(vars.my_hash_anchor_merged, {a: 1, b: 2, c: 3, d: 100}) -28 \ No newline at end of file +27 compare(vars.my_hash_anchor_merged, {a: 1, b: 2, c: 3, d: 100}) \ No newline at end of file From f509741d2abca1a005863c284ed57b8d05a71714 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sat, 30 Nov 2024 18:49:10 +0900 Subject: [PATCH 3/9] bonsai --- runbook.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runbook.go b/runbook.go index e9afae41..e48f7888 100644 --- a/runbook.go +++ b/runbook.go @@ -121,7 +121,7 @@ func parseRunbook(b []byte) (*runbook, error) { } if err := yaml.UnmarshalWithOptions([]byte(rep), rb, decOpts...); err != nil { - if err := parseRunbookMapped([]byte(rep), rb); err != nil { + if err := parseRunbookAsMapped([]byte(rep), rb); err != nil { return nil, err } } @@ -149,7 +149,7 @@ func flattenYamlAliases(in []byte) ([]byte, error) { return flattened, nil } -func parseRunbookMapped(b []byte, rb *runbook) error { +func parseRunbookAsMapped(b []byte, rb *runbook) error { m := &runbookMapped{} if err := yaml.UnmarshalWithOptions(b, m, decOpts...); err != nil { return err From 1d5a8b8840965102c7922e0c5c63c2d61a23f678 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sat, 30 Nov 2024 19:33:54 +0900 Subject: [PATCH 4/9] bonsai --- http.go | 5 ++++- runbook.go | 16 ---------------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/http.go b/http.go index 3e1ac0cf..4489ddf3 100644 --- a/http.go +++ b/http.go @@ -6,6 +6,7 @@ import ( "context" "crypto/tls" "crypto/x509" + "encoding/base64" "errors" "fmt" "io" @@ -170,11 +171,13 @@ func (r *httpRequest) encodeBody() (io.Reader, error) { } return bytes.NewBuffer(b), nil case string: + if b, err := base64.StdEncoding.DecodeString(v); err == nil { + return bytes.NewBuffer(b), nil + } return strings.NewReader(v), nil case []byte: return bytes.NewBuffer(r.body.([]byte)), nil case []any: - // NOTE: flattenYamlAliases converts !!binary base64 data into array arr, ok := r.body.([]any) if !ok { return nil, fmt.Errorf("invalid body: %v", r.body) diff --git a/runbook.go b/runbook.go index e48f7888..ca206ee3 100644 --- a/runbook.go +++ b/runbook.go @@ -133,22 +133,6 @@ func parseRunbook(b []byte) (*runbook, error) { return rb, nil } -func flattenYamlAliases(in []byte) ([]byte, error) { - var tmp any - - err := yaml.UnmarshalWithOptions(in, &tmp, decOpts...) - if err != nil { - return nil, err - } - - flattened, err := yaml.MarshalWithOptions(tmp, encOpts...) - if err != nil { - return nil, err - } - - return flattened, nil -} - func parseRunbookAsMapped(b []byte, rb *runbook) error { m := &runbookMapped{} if err := yaml.UnmarshalWithOptions(b, m, decOpts...); err != nil { From 34816ea1f159a90bd08e3b377c1ad969e2d8a50d Mon Sep 17 00:00:00 2001 From: k1LoW Date: Mon, 2 Dec 2024 11:35:03 +0900 Subject: [PATCH 5/9] Update k1LoW/expand --- go.mod | 4 ++-- go.sum | 8 ++++---- runbook_test.go | 4 ---- testdata/book/only_if_included.yml | 2 +- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 5d66e3f4..67060b99 100644 --- a/go.mod +++ b/go.mod @@ -32,14 +32,14 @@ require ( github.com/itchyny/gojq v0.12.16 github.com/jhump/protoreflect/v2 v2.0.0-beta.2 github.com/juliangruber/go-intersect v1.1.0 - github.com/k0kubun/pp/v3 v3.3.0 + github.com/k0kubun/pp/v3 v3.4.1 github.com/k1LoW/bufresolv v0.7.4 github.com/k1LoW/concgroup v1.1.0 github.com/k1LoW/curlreq v0.3.3 github.com/k1LoW/donegroup v1.10.2 github.com/k1LoW/duration v1.2.0 github.com/k1LoW/exec v0.3.0 - github.com/k1LoW/expand v0.14.0 + github.com/k1LoW/expand v0.15.0 github.com/k1LoW/ghfs v1.3.1 github.com/k1LoW/go-github-client/v58 v58.0.12 github.com/k1LoW/grpcstub v0.25.0 diff --git a/go.sum b/go.sum index 0e0c617d..07744833 100644 --- a/go.sum +++ b/go.sum @@ -1002,8 +1002,8 @@ github.com/juliangruber/go-intersect v1.1.0 h1:sc+y5dCjMMx0pAdYk/N6KBm00tD/f3tq+ github.com/juliangruber/go-intersect v1.1.0/go.mod h1:WMau+1kAmnlQnKiikekNJbtGtfmILU/mMU6H7AgKbWQ= github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= -github.com/k0kubun/pp/v3 v3.3.0 h1:/Unrck5tDGUSjsUJsmx9GUL64pNKOY5UEdoP1F7FBq8= -github.com/k0kubun/pp/v3 v3.3.0/go.mod h1:wJadGBvcY6JKaiUkB89VzUACKDmTX1r4aQTPERpZc6w= +github.com/k0kubun/pp/v3 v3.4.1 h1:1WdFZDRRqe8UsR61N/2RoOZ3ziTEqgTPVqKrHeb779Y= +github.com/k0kubun/pp/v3 v3.4.1/go.mod h1:+SiNiqKnBfw1Nkj82Lh5bIeKQOAkPy6Xw9CAZUZ8npI= github.com/k1LoW/bufresolv v0.7.4 h1:k9Wx3YjWMSK87IqDKEhNTMOGgq+aXWJJIdgRXRUu7Ic= github.com/k1LoW/bufresolv v0.7.4/go.mod h1:YAD7UyqFU2+hyrUot6HZtVCD2Hjk7PkO62id7GjCdn4= github.com/k1LoW/concgroup v1.1.0 h1:yf7JEnvXPNlRdBw184mFaLz6w/anJMpe8fg4nJ8s4II= @@ -1016,8 +1016,8 @@ github.com/k1LoW/duration v1.2.0 h1:qq1gWtPh7YROFyerBufVP+ATR11mOOHDInrcC/Xe/6A= github.com/k1LoW/duration v1.2.0/go.mod h1:qUa0NptIiUl5EUsCc8wIiSaHuNjS4wmpYNMHp0l6pos= github.com/k1LoW/exec v0.3.0 h1:lNUqhF5IXhm2aDKcaWxhGwYC/WLcCbLOqoe97oxW1XQ= github.com/k1LoW/exec v0.3.0/go.mod h1:LSd4t5/1qGJHUdB2RUtoHuHfaZ3ks+BfQ+sGHzvwhnE= -github.com/k1LoW/expand v0.14.0 h1:jmGOxEC/qmB+0oT44H4S9AA5UpJes4SZ/cguVbnNzGc= -github.com/k1LoW/expand v0.14.0/go.mod h1:dTRPh19IUlp+whqWo3kZRWO+qf/6/EMbJyS98AuUDE4= +github.com/k1LoW/expand v0.15.0 h1:p8MAJ/BFp7etawqsnu0oy1fPd5mwHZGq2bw0Dru8JnA= +github.com/k1LoW/expand v0.15.0/go.mod h1:V6uYRemnl93BcHVOJhL1OMf2CEVLNFjZhjxvymFpdMA= github.com/k1LoW/ghfs v1.3.1 h1:xq/VenCDhSUDf1IsL0LBfWIn3QTCOOd8+pE0SNZF6wM= github.com/k1LoW/ghfs v1.3.1/go.mod h1:IaAooDKpTdx/I0lSethYNMowo0rs0h/+btPPHdZlnE4= github.com/k1LoW/go-github-client/v58 v58.0.12 h1:rIK+H2cSpK0ptOxNq0jITGu3ffRjLTe5ITCz1jik5c0= diff --git a/runbook_test.go b/runbook_test.go index 4f2bf9cd..edea72a8 100644 --- a/runbook_test.go +++ b/runbook_test.go @@ -25,10 +25,6 @@ func TestParseRunbook(t *testing.T) { if es.IsDir() || !strings.HasSuffix(es.Name(), ".yml") { continue } - if es.Name() == "include_vars_main.yml" { - // NOTICE: include_vars_main.yml is not supported. because go-yaml unwraps quotes when it determines that a tag is a string - continue - } t.Run(es.Name(), func(t *testing.T) { path := filepath.Join("testdata", "book", es.Name()) f, err := os.Open(path) diff --git a/testdata/book/only_if_included.yml b/testdata/book/only_if_included.yml index 827eaa4a..3b044eaa 100644 --- a/testdata/book/only_if_included.yml +++ b/testdata/book/only_if_included.yml @@ -5,4 +5,4 @@ steps: test: 'false' bindvars: bind: - run: '{{ "ok" }}' + run: '"ok"' From f6a901401d34d6273947d1efba1556ee135ac9a4 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Mon, 2 Dec 2024 12:18:55 +0900 Subject: [PATCH 6/9] Use yml.RegisterCustomMarshaler for binary --- http.go | 4 ---- testdata/http.yml.runbook.golden | 2 +- yaml.go | 8 +++++++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/http.go b/http.go index 4489ddf3..732c497e 100644 --- a/http.go +++ b/http.go @@ -6,7 +6,6 @@ import ( "context" "crypto/tls" "crypto/x509" - "encoding/base64" "errors" "fmt" "io" @@ -171,9 +170,6 @@ func (r *httpRequest) encodeBody() (io.Reader, error) { } return bytes.NewBuffer(b), nil case string: - if b, err := base64.StdEncoding.DecodeString(v); err == nil { - return bytes.NewBuffer(b), nil - } return strings.NewReader(v), nil case []byte: return bytes.NewBuffer(r.body.([]byte)), nil diff --git a/testdata/http.yml.runbook.golden b/testdata/http.yml.runbook.golden index 499ec4d5..9aa00d25 100644 --- a/testdata/http.yml.runbook.golden +++ b/testdata/http.yml.runbook.golden @@ -115,7 +115,7 @@ steps: /upload: post: body: - application/octet-stream: /9j/4AAQSkZJRgABAQEAYABgAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBkZWZhdWx0IHF1YWxpdHkK/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgAKAA8AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8A+f6KKKACiiigAooooAKKKKACiiigDp5UtWgf7ELASJKiQGeSAh4cHLEEDB/1ed2W5bGMNWWkOmyG8YyFArsbVS2PMHOA3HHY579O+5cyigDes005dOQedaST+YrASAht5R8AkgDYGCZ5x1J4PGbqQUTRf6nzvLHneTt2bsnGNvy/d25x3zVOigAooooAKKKKACiiigAooooAKKKKAP/Z + application/octet-stream: !!binary /9j/4AAQSkZJRgABAQEAYABgAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBkZWZhdWx0IHF1YWxpdHkK/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgAKAA8AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8A+f6KKKACiiigAooooAKKKKACiiigDp5UtWgf7ELASJKiQGeSAh4cHLEEDB/1ed2W5bGMNWWkOmyG8YyFArsbVS2PMHOA3HHY579O+5cyigDes005dOQedaST+YrASAht5R8AkgDYGCZ5x1J4PGbqQUTRf6nzvLHneTt2bsnGNvy/d25x3zVOigAooooAKKKKACiiigAooooAKKKKAP/Z test: | current.res.status == 201 && current.res.headers["Content-Length"][0] == "15" diff --git a/yaml.go b/yaml.go index 0869ad9d..1b6fcb2a 100644 --- a/yaml.go +++ b/yaml.go @@ -145,7 +145,7 @@ func CreateHTTPStepMapSlice(key string, req *http.Request) (yaml.MapSlice, error return nil, fmt.Errorf("failed to io.ReadAll: %w", err) } bd = yaml.MapSlice{ - {Key: contentType, Value: base64.StdEncoding.EncodeToString(b)}, + {Key: contentType, Value: b}, } default: // case contentType == runn.MediaTypeTextPlain: @@ -200,3 +200,9 @@ func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) { } return io.NopCloser(&buf), io.NopCloser(bytes.NewReader(buf.Bytes())), nil } + +func init() { + yaml.RegisterCustomMarshaler(func(v []byte) ([]byte, error) { + return []byte(fmt.Sprintf("!!binary %s", base64.StdEncoding.EncodeToString(v))), nil + }) +} From 87f1799d11bcadd72371623c08ea34338c84ddca Mon Sep 17 00:00:00 2001 From: k1LoW Date: Mon, 2 Dec 2024 15:45:29 +0900 Subject: [PATCH 7/9] Update goccy/go-yaml --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 67060b99..8f6cb9b8 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/gliderlabs/ssh v0.3.7 github.com/go-sql-driver/mysql v1.8.1 github.com/goccy/go-json v0.10.3 - github.com/goccy/go-yaml v1.15.4 + github.com/goccy/go-yaml v1.15.5 github.com/golang-sql/sqlexp v0.1.0 github.com/google/go-cmp v0.6.0 github.com/google/go-github/v58 v58.0.0 diff --git a/go.sum b/go.sum index 07744833..34c40977 100644 --- a/go.sum +++ b/go.sum @@ -829,8 +829,8 @@ github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakr github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= -github.com/goccy/go-yaml v1.15.4 h1:Xk5iHVvnsv6KD0HrMuZ0CiJbfdR9KUYQw7U/t9binGQ= -github.com/goccy/go-yaml v1.15.4/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/goccy/go-yaml v1.15.5 h1:3XvnIQra8CW/7TPV2zks7zR9zUCrPTtW17HSptwXWis= +github.com/goccy/go-yaml v1.15.5/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= From 5bd024aca895d89ff76dc730297ad1d615db2be7 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Tue, 3 Dec 2024 16:21:43 +0900 Subject: [PATCH 8/9] Trim .0 suffix --- testdata/grpc_command.append_step.golden | 2 +- yaml.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/testdata/grpc_command.append_step.golden b/testdata/grpc_command.append_step.golden index b2092992..97a654ea 100644 --- a/testdata/grpc_command.append_step.golden +++ b/testdata/grpc_command.append_step.golden @@ -5,7 +5,7 @@ steps: - greq: my.custom.server.Service/Method: message: - id: 1234.0 + id: 1234 tags: - foo - bar diff --git a/yaml.go b/yaml.go index 1b6fcb2a..8cd6648b 100644 --- a/yaml.go +++ b/yaml.go @@ -10,6 +10,7 @@ import ( "mime" "net/http" "net/url" + "strconv" "strings" "github.com/goccy/go-yaml" @@ -205,4 +206,8 @@ func init() { yaml.RegisterCustomMarshaler(func(v []byte) ([]byte, error) { return []byte(fmt.Sprintf("!!binary %s", base64.StdEncoding.EncodeToString(v))), nil }) + yaml.RegisterCustomMarshaler(func(v float64) ([]byte, error) { + s := strconv.FormatFloat(v, 'f', -1, 64) + return []byte(s), nil + }) } From 4f7feda4c196247bee384c351d59d3d053a550cb Mon Sep 17 00:00:00 2001 From: k1LoW Date: Fri, 13 Dec 2024 06:58:52 +0900 Subject: [PATCH 9/9] Update goccy/go-yaml to v1.15.9 --- go.mod | 2 +- go.sum | 4 ++-- testdata/grpc.yml.runbook.golden | 16 ++++++++-------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index dc76899e..f963a83e 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/gliderlabs/ssh v0.3.7 github.com/go-sql-driver/mysql v1.8.1 github.com/goccy/go-json v0.10.3 - github.com/goccy/go-yaml v1.15.5 + github.com/goccy/go-yaml v1.15.9 github.com/golang-sql/sqlexp v0.1.0 github.com/google/go-cmp v0.6.0 github.com/google/go-github/v58 v58.0.0 diff --git a/go.sum b/go.sum index 48ca49b0..835c79d5 100644 --- a/go.sum +++ b/go.sum @@ -829,8 +829,8 @@ github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakr github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= -github.com/goccy/go-yaml v1.15.5 h1:3XvnIQra8CW/7TPV2zks7zR9zUCrPTtW17HSptwXWis= -github.com/goccy/go-yaml v1.15.5/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/goccy/go-yaml v1.15.9 h1:500CYajdgpK4Smqrf86u7VMZuj/bFt2ghdff9D/nQYc= +github.com/goccy/go-yaml v1.15.9/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= diff --git a/testdata/grpc.yml.runbook.golden b/testdata/grpc.yml.runbook.golden index c7aa328c..64b2b6e5 100644 --- a/testdata/grpc.yml.runbook.golden +++ b/testdata/grpc.yml.runbook.golden @@ -18,7 +18,7 @@ steps: message: name: alice num: 3 - request_time: 2022-06-25T05:24:43.861872Z + request_time: "2022-06-25T05:24:43.861872Z" test: | current.res.headers["content-type"][0] == "application/grpc" && current.res.headers["hello"][0] == "header" @@ -40,7 +40,7 @@ steps: message: name: alice num: 3 - request_time: 2022-06-25T05:24:43.861872Z + request_time: "2022-06-25T05:24:43.861872Z" test: | current.res.trailers["content-type"][0] == "application/grpc" && current.res.status == 1 @@ -51,7 +51,7 @@ steps: message: name: bob num: 4 - request_time: 2022-06-25T05:24:43.861872Z + request_time: "2022-06-25T05:24:43.861872Z" test: | compare(current.res.messages[0], {"create_time":"2022-06-25T05:24:43.861872Z","message":"hello","num":33}) && compare(current.res.messages[1], {"create_time":"2022-06-25T05:24:44.382783Z","message":"hello","num":34}) @@ -66,10 +66,10 @@ steps: messages: - name: alice num: 5 - request_time: 2022-06-25T05:24:43.861872Z + request_time: "2022-06-25T05:24:43.861872Z" - name: bob num: 6 - request_time: 2022-06-25T05:24:43.861872Z + request_time: "2022-06-25T05:24:43.861872Z" test: | compare(current.res.message, {"create_time":"2022-06-25T05:24:45.382783Z","message":"hello","num":35}) && current.res.headers["content-type"][0] == "application/grpc" @@ -83,14 +83,14 @@ steps: messages: - name: alice num: 7 - request_time: 2022-06-25T05:24:43.861872Z + request_time: "2022-06-25T05:24:43.861872Z" - receive - name: bob num: 8 - request_time: 2022-06-25T05:24:43.861872Z + request_time: "2022-06-25T05:24:43.861872Z" - name: charlie num: 9 - request_time: 2022-06-25T05:24:43.861872Z + request_time: "2022-06-25T05:24:43.861872Z" - close test: | current.res.headers["content-type"][0] == "application/grpc"