From 581789d82c0c9e3e1a403cb34e3e2d804a12473d Mon Sep 17 00:00:00 2001 From: metalblueberry <metalblueberry@gmail.com> Date: Sun, 28 Apr 2024 17:35:09 +0200 Subject: [PATCH 1/5] add CI --- .github/workflows/test.yaml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/test.yaml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..28aa22a --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,22 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + push: + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: "1.22" + + - name: Test + run: go test -v ./... From 40b2cc3ad7624099c9927498b12a3145c5d44dc0 Mon Sep 17 00:00:00 2001 From: metalblueberry <metalblueberry@gmail.com> Date: Sun, 28 Apr 2024 17:40:11 +0200 Subject: [PATCH 2/5] Check schema --- .github/workflows/test.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 28aa22a..e82d663 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -20,3 +20,8 @@ jobs: - name: Test run: go test -v ./... + + - name: Check Generated Schema + run: -| + go generate ./... + git diff --exit-code From b3e7a13dacf70f5864073cee91fa1b6b52765754 Mon Sep 17 00:00:00 2001 From: metalblueberry <metalblueberry@gmail.com> Date: Sun, 28 Apr 2024 17:44:08 +0200 Subject: [PATCH 3/5] try another approach --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e82d663..a6c42fa 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -22,6 +22,6 @@ jobs: run: go test -v ./... - name: Check Generated Schema - run: -| + run: | go generate ./... git diff --exit-code From c662074d9284eee8e3ab98c4ecc10ace231dc9b1 Mon Sep 17 00:00:00 2001 From: Victor <metalblueberry@gmail.com> Date: Wed, 1 May 2024 16:49:53 +0200 Subject: [PATCH 4/5] Add tests for consistency (#19) * Implement test for consistency * update schema --- generator/generator_suite_test.go | 2 +- generator/parser_test.go | 2 +- generator/renderer.go | 3 +- generator/renderer_test.go | 106 ++++++++++++++++++++++-------- generator/typefile.go | 2 +- go.mod | 18 ++++- go.sum | 105 ++++++++--------------------- graph_objects/layout_gen.go | 10 +-- 8 files changed, 129 insertions(+), 119 deletions(-) diff --git a/generator/generator_suite_test.go b/generator/generator_suite_test.go index d53fec8..23e617d 100644 --- a/generator/generator_suite_test.go +++ b/generator/generator_suite_test.go @@ -3,7 +3,7 @@ package generator_test import ( "testing" - . "github.com/onsi/ginkgo" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" ) diff --git a/generator/parser_test.go b/generator/parser_test.go index 9729f79..9747843 100644 --- a/generator/parser_test.go +++ b/generator/parser_test.go @@ -5,7 +5,7 @@ import ( "os" "github.com/MetalBlueberry/go-plotly/generator" - . "github.com/onsi/ginkgo" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" ) diff --git a/generator/renderer.go b/generator/renderer.go index ebdda5c..a0a3840 100644 --- a/generator/renderer.go +++ b/generator/renderer.go @@ -204,7 +204,8 @@ func (r *Renderer) WriteLayout(w io.Writer) error { } traceFile.MainType.Fields = append(traceFile.MainType.Fields, fields...) - for name, trace := range r.root.Schema.Traces { + for _, name := range sortKeys(r.root.Schema.Traces) { + trace := r.root.Schema.Traces[name] fields, err := traceFile.parseAttributes(xstrings.ToCamelCase(name), "Layout", trace.LayoutAttributes.Names) if err != nil { return fmt.Errorf("cannot parse attributes, %w", err) diff --git a/generator/renderer_test.go b/generator/renderer_test.go index 1c85116..ee1fa6f 100644 --- a/generator/renderer_test.go +++ b/generator/renderer_test.go @@ -7,9 +7,11 @@ import ( _ "embed" "github.com/golang/mock/gomock" - . "github.com/onsi/ginkgo" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + // . "github.com/onsi/gomega/format" + "github.com/MetalBlueberry/go-plotly/generator" "github.com/MetalBlueberry/go-plotly/generator/mocks" ) @@ -19,41 +21,89 @@ var schema []byte var _ = Describe("Renderer", func() { - var ( - ctrl *gomock.Controller - mockCreator *mocks.MockCreator - ) - BeforeEach(func() { - ctrl = gomock.NewController(GinkgoT()) - mockCreator = mocks.NewMockCreator(ctrl) - }) - AfterEach(func() { - ctrl.Finish() - }) + Describe("A single trace", func() { + var ( + ctrl *gomock.Controller + mockCreator *mocks.MockCreator + ) + BeforeEach(func() { + ctrl = gomock.NewController(GinkgoT()) + mockCreator = mocks.NewMockCreator(ctrl) + }) + AfterEach(func() { + ctrl.Finish() + }) - It("Should create package", func() { - buf := NopWriterCloser{&bytes.Buffer{}} + It("Should create package", func() { + buf := NopWriterCloser{&bytes.Buffer{}} - mockCreator.EXPECT().Create(gomock.Eq("scatter_gen.go")).Return(buf, nil).Times(1) + mockCreator.EXPECT().Create(gomock.Eq("scatter_gen.go")).Return(buf, nil).Times(1) - root, err := generator.LoadSchema(bytes.NewReader(schema)) - Expect(err).To(BeNil()) + root, err := generator.LoadSchema(bytes.NewReader(schema)) + Expect(err).To(BeNil()) - r, err := generator.NewRenderer(mockCreator, root) - Expect(err).To(BeNil()) + r, err := generator.NewRenderer(mockCreator, root) + Expect(err).To(BeNil()) - err = r.CreateTrace(".", "scatter") - Expect(err).To(BeNil()) + err = r.CreateTrace(".", "scatter") + Expect(err).To(BeNil()) - formatted, err := format.Source(buf.Bytes()) - Expect(err).To(BeNil()) + formatted, err := format.Source(buf.Bytes()) + Expect(err).To(BeNil()) - Expect(string(formatted)).To(ContainSubstring(`package grob`)) - // Type is defined - Expect(string(formatted)).To(ContainSubstring(`type Scatter struct`)) - // Implements interface GetType() - Expect(string(formatted)).To(ContainSubstring(`func (trace *Scatter) GetType() TraceType`)) + Expect(string(formatted)).To(ContainSubstring(`package grob`)) + // Type is defined + Expect(string(formatted)).To(ContainSubstring(`type Scatter struct`)) + // Implements interface GetType() + Expect(string(formatted)).To(ContainSubstring(`func (trace *Scatter) GetType() TraceType`)) + }) + }) + Describe("When writing", func() { + var r *generator.Renderer + + BeforeEach(func() { + root, err := generator.LoadSchema(bytes.NewReader(schema)) + Expect(err).To(BeNil()) + + r, err = generator.NewRenderer(nil, root) + Expect(err).To(BeNil()) + }) + + Describe("The config", func() { + It("Should be consistent", func() { + + original := &bytes.Buffer{} + err := r.WriteConfig(original) + Expect(err).To(BeNil()) + + for i := 0; i < 10; i++ { + attempt := &bytes.Buffer{} + err = r.WriteConfig(attempt) + Expect(err).To(BeNil()) + Expect(attempt).To(Equal(original)) + } + + }) + }) + Describe("The Layout", func() { + // TruncatedDiff = false + // MaxLength = 0 + It("Should be consistent", func() { + + original := &bytes.Buffer{} + err := r.WriteLayout(original) + Expect(err).To(BeNil()) + + for i := 0; i < 10; i++ { + attempt := &bytes.Buffer{} + err = r.WriteLayout(attempt) + Expect(err).To(BeNil()) + Expect(attempt.String()).To(Equal(original.String())) + } + + }) + }) }) }) diff --git a/generator/typefile.go b/generator/typefile.go index 4700027..f5ee3d6 100644 --- a/generator/typefile.go +++ b/generator/typefile.go @@ -360,7 +360,7 @@ func (file *typeFile) parseFlaglist(name string, attr *Attribute) error { return nil } -func sortKeys(attr map[string]*Attribute) []string { +func sortKeys[T any](attr map[string]*T) []string { keys := make([]string, 0, len(attr)) for k := range attr { keys = append(keys, k) diff --git a/go.mod b/go.mod index 16fca8b..24c6246 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,23 @@ module github.com/MetalBlueberry/go-plotly -go 1.16 +go 1.18 require ( github.com/golang/mock v1.5.0 github.com/huandu/xstrings v1.3.2 - github.com/onsi/ginkgo v1.16.2 - github.com/onsi/gomega v1.12.0 + github.com/onsi/ginkgo/v2 v2.17.2 + github.com/onsi/gomega v1.33.0 github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 ) + +require ( + github.com/go-logr/logr v1.4.1 // indirect + github.com/go-task/slim-sprig/v3 v3.0.0 // indirect + github.com/google/go-cmp v0.6.0 // indirect + github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect + golang.org/x/net v0.24.0 // indirect + golang.org/x/sys v0.19.0 // indirect + golang.org/x/text v0.14.0 // indirect + golang.org/x/tools v0.20.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index fbe14ff..9f3488b 100644 --- a/go.sum +++ b/go.sum @@ -1,99 +1,46 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= -github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= -github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI= +github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8= github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g= github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= -github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= -github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= -github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= -github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 h1:k7nVchz72niMH6YLQNvHSdIE7iqsQxK1P41mySCvssg= +github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw= github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw= github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= -github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= -github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= -github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= -github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= -github.com/onsi/ginkgo v1.16.2 h1:HFB2fbVIlhIfCfOW81bZFbiC/RvnpXSdhbF2/DJr134= -github.com/onsi/ginkgo v1.16.2/go.mod h1:CObGmKUOKaSC0RjmoAK7tKyn4Azo5P2IWuoMnvwxz1E= -github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= -github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= -github.com/onsi/gomega v1.12.0 h1:p4oGGk2M2UJc0wWN4lHFvIB71lxsh0T/UiKCCgFADY8= -github.com/onsi/gomega v1.12.0/go.mod h1:lRk9szgn8TxENtWd0Tp4c3wjlRfMTMH27I+3Je41yGY= +github.com/onsi/ginkgo/v2 v2.17.2 h1:7eMhcy3GimbsA3hEnVKdw/PQM9XN9krpKVXsZdph0/g= +github.com/onsi/ginkgo/v2 v2.17.2/go.mod h1:nP2DPOQoNsQmsVyv5rDA8JkXQoCs6goXIvr/PRJ1eCc= +github.com/onsi/gomega v1.33.0 h1:snPCflnZrpMsy94p4lXVEkHo12lmPnc3vY5XBbreexE= +github.com/onsi/gomega v1.33.0/go.mod h1:+925n5YtiFsLzzafLUHzVMBpvvRAzrydIBiSIxjX3wY= github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98= github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0= -golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= +golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY= +golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= -google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= -google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= -google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= -google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/graph_objects/layout_gen.go b/graph_objects/layout_gen.go index d085352..d629aac 100644 --- a/graph_objects/layout_gen.go +++ b/graph_objects/layout_gen.go @@ -32,7 +32,7 @@ type Layout struct { // Bargap // arrayOK: false // type: number - // Sets the gap between bars of adjacent location coordinates. Values are unitless, they represent fractions of the minimum difference in bar positions in the data. + // Sets the gap (in plot fraction) between bars of adjacent location coordinates. Bargap float64 `json:"bargap,omitempty"` // Bargroupgap @@ -42,9 +42,9 @@ type Layout struct { Bargroupgap float64 `json:"bargroupgap,omitempty"` // Barmode - // default: stack + // default: group // type: enumerated - // Determines how bars at the same location coordinate are displayed on the graph. With *stack*, the bars are stacked on top of one another With *overlay*, the bars are plotted over one another, you might need to an *opacity* to see multiple bars. + // Determines how bars at the same location coordinate are displayed on the graph. With *stack*, the bars are stacked on top of one another With *relative*, the bars are stacked on top of one another, with negative values below the axis, positive values above With *group*, the bars are plotted next to one another centered around the shared location. With *overlay*, the bars are plotted over one another, you might need to an *opacity* to see multiple bars. Barmode LayoutBarmode `json:"barmode,omitempty"` // Barnorm @@ -6328,10 +6328,10 @@ const ( type LayoutBoxmode string const ( - CandlestickBoxmodeGroup LayoutBoxmode = "group" - CandlestickBoxmodeOverlay LayoutBoxmode = "overlay" BoxBoxmodeGroup LayoutBoxmode = "group" BoxBoxmodeOverlay LayoutBoxmode = "overlay" + CandlestickBoxmodeGroup LayoutBoxmode = "group" + CandlestickBoxmodeOverlay LayoutBoxmode = "overlay" ) // LayoutCalendar Sets the default calendar system to use for interpreting and displaying dates throughout the plot. From bf9776c7076a15e58f195964117a4b3a1b9269a5 Mon Sep 17 00:00:00 2001 From: Victor <metalblueberry@gmail.com> Date: Wed, 1 May 2024 16:53:53 +0200 Subject: [PATCH 5/5] Always generate on CI (#20) --- generator/cmd/generator/main.go | 38 ++++++-- generator/renderer.go | 30 ++++++ generator/templates/plotly.tmpl | 102 +++++++++++++++++++++ graph_objects/{plotly.go => plotly_gen.go} | 0 4 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 generator/templates/plotly.tmpl rename graph_objects/{plotly.go => plotly_gen.go} (100%) diff --git a/generator/cmd/generator/main.go b/generator/cmd/generator/main.go index 5c20222..3ef0634 100644 --- a/generator/cmd/generator/main.go +++ b/generator/cmd/generator/main.go @@ -5,6 +5,7 @@ import ( "io" "log" "os" + "path/filepath" "github.com/MetalBlueberry/go-plotly/generator" ) @@ -12,10 +13,18 @@ import ( type Creator struct{} func (c Creator) Create(name string) (io.WriteCloser, error) { - return os.Create(name) + abs, err := filepath.Abs(name) + if err != nil { + return nil, err + } + + return os.Create(abs) } +//go:generate go run main.go --clean --schema ../../schema.json --output-directory ../../../graph_objects + func main() { + clean := flag.Bool("clean", false, "clean the output directory first. Mandatory on CI") schema := flag.String("schema", "schema.json", "plotly schema") outputDirectory := flag.String("output-directory", "gen/", "output directory, must exist before generation") @@ -34,28 +43,45 @@ func main() { r, err := generator.NewRenderer(Creator{}, root) if err != nil { log.Fatalf("unable to create a new renderer, %s", err) - panic(err) } output := *outputDirectory + if *clean { + err = os.RemoveAll(output) + if err != nil { + log.Fatalf("Failed to clean output directory, %s", err) + } + } + + err = os.MkdirAll(output, os.ModePerm) + if err != nil { + log.Fatalf("Failed to create output dir %s, %s", *outputDirectory, err) + + } + + err = r.CreatePlotly(output) + if err != nil { + log.Fatalf("unable to write plotly, %s", err) + } + err = r.CreateTraces(output) if err != nil { - log.Fatal("unable to write traces, %w", err) + log.Fatalf("unable to write traces, %s", err) } err = r.CreateLayout(output) if err != nil { - log.Fatal("unable to write layout, %w", err) + log.Fatalf("unable to write layout, %s", err) } err = r.CreateConfig(output) if err != nil { - log.Fatal("unable to write config, %w", err) + log.Fatalf("unable to write config, %s", err) } err = r.CreateUnmarshal(output) if err != nil { - log.Fatal("unable to write unmarshal, %w", err) + log.Fatalf("unable to write unmarshal, %s", err) } } diff --git a/generator/renderer.go b/generator/renderer.go index a0a3840..9eeae5a 100644 --- a/generator/renderer.go +++ b/generator/renderer.go @@ -46,6 +46,36 @@ func NewRenderer(fs Creator, root *Root) (*Renderer, error) { var doNotEdit = "// Code generated by go-plotly/generator. DO NOT EDIT." +func (r *Renderer) CreatePlotly(dir string) error { + src := &bytes.Buffer{} + err := r.WritePlotly(src) + if err != nil { + return err + } + + fmtsrc, err := format.Source(src.Bytes()) + if err != nil { + return fmt.Errorf("cannot format source, %w", err) + } + + file, err := r.fs.Create(path.Join(dir, "plotly_gen.go")) + if err != nil { + return fmt.Errorf("Path %s, error %s", dir, err) + } + defer file.Close() + _, err = file.Write(fmtsrc) + if err != nil { + return fmt.Errorf("cannot write source, %w", err) + } + + return nil +} + +// WritePlotly writes the base plotly file +func (r *Renderer) WritePlotly(w io.Writer) error { + return r.tmpl.ExecuteTemplate(w, "plotly.tmpl", w) +} + // CreateTrace creates a file with the content of a trace by name func (r *Renderer) CreateTrace(dir string, name string) error { src := &bytes.Buffer{} diff --git a/generator/templates/plotly.tmpl b/generator/templates/plotly.tmpl new file mode 100644 index 0000000..854c212 --- /dev/null +++ b/generator/templates/plotly.tmpl @@ -0,0 +1,102 @@ + +package grob + +import ( + "encoding/json" +) + +// Generate the files +//go:generate go run ../generator/cmd/generator/main.go --schema ../generator/schema.json --output-directory . + +// TraceType is the type for the TraceType field on every trace +type TraceType string + +// Trace Every trace implements this interface +// It is useful for autocompletion, it is a better idea to use +// type assertions/switches to identify trace types +type Trace interface { + GetType() TraceType +} + +// Traces is a slice of Traces +type Traces []Trace + +// Fig is the base type for figures. +type Fig struct { + // Data The data to be plotted is described in an array usually called data, whose elements are trace objects of various types (e.g. scatter, bar etc) as documented in the Full Reference. + // https://plotly.com/javascript/reference + Data Traces `json:"data,omitempty"` + + // Layout The layout of the plot – non-data-related visual attributes such as the title, annotations etc – is described in an object usually called layout, as documented in/ the Full Reference. + // https://plotly.com/javascript/reference/layout + Layout *Layout `json:"layout,omitempty"` + + // Config High-level configuration options for the plot, such as the scroll/zoom/hover behaviour, is described in an object usually called config, as documented here. The difference between config and layout is that layout relates to the content of the plot, whereas config relates to the context in which the plot is being shown. + // https://plotly.com/javascript/configuration-options + Config *Config `json:"config,omitempty"` + + // Animation is not yet implemented, feel free to insert custom a struct + Animation interface{} `json:"animation,omitempty"` +} + +// AddTraces Is a shorthand to add figures to a given figure. It handles the case where the Traces value is nil. +func (fig *Fig) AddTraces(traces ...Trace) { + if fig.Data == nil { + fig.Data = make(Traces, 0) + } + fig.Data = append(fig.Data, traces...) +} + +// UnmarshalJSON is a custom unmarshal function to properly handle special cases. +func (fig *Fig) UnmarshalJSON(data []byte) error { + var err error + tmp := unmarshalFig{} + err = json.Unmarshal(data, &tmp) + if err != nil { + return err + } + + fig.Layout = tmp.Layout + fig.Config = tmp.Config + + for i := range tmp.Data { + trace, err := UnmarshalTrace(tmp.Data[i]) + if err != nil { + return err + } + fig.AddTraces(trace) + } + return nil +} + +type unmarshalFig struct { + Data []json.RawMessage `json:"data,omitempty"` + Layout *Layout `json:"layout,omitempty"` + Config *Config `json:"config,omitempty"` +} + +// Bool represents a *bool value. Needed to tell the differenc between false and nil. +type Bool *bool + +var ( + trueValue bool = true + falseValue bool = false + + // True is a *bool with true value + True Bool = &trueValue + // False is a *bool with false value + False Bool = &falseValue +) + +// String is a string value, can be a []string if arrayOK is true. +// numeric values are converted to string by plotly, so []<number> can work +type String interface{} + +// Color A string describing color. Supported formats: - hex (e.g. '#d3d3d3') - rgb (e.g. 'rgb(255, 0, 0)') - rgba (e.g. 'rgb(255, 0, 0, 0.5)') - hsl (e.g. 'hsl(0, 100%, 50%)') - hsv (e.g. 'hsv(0, 100%, 100%)') - named colors (full list: http://www.w3.org/TR/css3-color/#svg-color)", +type Color interface{} + +// ColorList A list of colors. Must be an {array} containing valid colors. +type ColorList []Color + +// ColorScale A Plotly colorscale either picked by a name: (any of Greys, YlGnBu, Greens, YlOrRd, Bluered, RdBu, Reds, Blues, Picnic, Rainbow, Portland, Jet, Hot, Blackbody, Earth, Electric, Viridis, Cividis ) customized as an {array} of 2-element {arrays} where the first element is the normalized color level value (starting at *0* and ending at *1*), and the second item is a valid color string. +type ColorScale interface{} \ No newline at end of file diff --git a/graph_objects/plotly.go b/graph_objects/plotly_gen.go similarity index 100% rename from graph_objects/plotly.go rename to graph_objects/plotly_gen.go