From ee940fd0d0a9f86c7eefed5190b1d55ae5634d7c Mon Sep 17 00:00:00 2001 From: Alex Nuka Date: Mon, 4 Dec 2023 21:14:23 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .CreateFile(j.FileOutput) | 68 +++++++++++++++++++ .DecodeJSON(inputJSON, &j.DockerCompose) | 68 +++++++++++++++++++ .ReadFile(j.FileInput) | 68 +++++++++++++++++++ e() | 68 +++++++++++++++++++ encoding/encoding.go | 84 ++++++++++++++++++++++-- jsonInput.json | 1 + jsonOutput.json | 1 + yamlInput.yml | 19 ++++++ yamlOutput.yml | 19 ++++++ 9 files changed, 392 insertions(+), 4 deletions(-) create mode 100644 .CreateFile(j.FileOutput) create mode 100644 .DecodeJSON(inputJSON, &j.DockerCompose) create mode 100644 .ReadFile(j.FileInput) create mode 100644 e() create mode 100644 jsonInput.json create mode 100644 jsonOutput.json create mode 100644 yamlInput.yml create mode 100644 yamlOutput.yml diff --git a/.CreateFile(j.FileOutput) b/.CreateFile(j.FileOutput) new file mode 100644 index 0000000..d636880 --- /dev/null +++ b/.CreateFile(j.FileOutput) @@ -0,0 +1,68 @@ +commit d1816fc2b3d88cc668123e7af2151c0a0ce39638 (HEAD -> main, origin/main, origin/HEAD) +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:36:03 2023 +0300 + + Update encoding.go + +commit 02513509f43783ad431670400d095c8691f00998 +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:35:16 2023 +0300 + + Delete precode directory + +commit 1aa823fde11a7293c54ab999e26a0903c9d359ec +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:34:29 2023 +0300 + + Update encoding.go + +commit d08c877b39da5c385ccd31f1992d7482c1f2fe1c +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:32:42 2023 +0300 + + Delete encoding/encoding_test.go + +commit b5618c94a0ce72a6f4785d35dc833a15bd34b26b +Merge: 3054315 4ba88b5 +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:30:35 2023 +0300 + + Merge pull request #1 from Yandex-Practicum/prototype + + prototype + +commit 3054315f478524943e0a4f75bd2ad99a50c92323 +Author: bbrodriges +Date: Wed Nov 22 16:27:41 2023 +0300 + + add autotests + +commit 4ba88b56f7b67cfe1dfb9c8d2d26583bf2ce9b27 +Author: Владислав Попов +Date: Thu Nov 2 18:15:01 2023 +0300 + + test and precode added + +commit 4b3729fe73dd1852872b8315fa53c05ff5975171 +Author: Владислав Попов +Date: Wed Nov 1 19:37:42 2023 +0300 + + changing example data + +commit 1d65c20524dd92f164bc6953079798e1877ecdec +Author: Владислав Попов +Date: Wed Nov 1 19:36:53 2023 +0300 + + changing data + +commit b2df37c98e02a93dc7052c00ce8baf377543023a +Author: Владислав Попов +Date: Tue Oct 31 21:11:43 2023 +0300 + + prototype + +commit 3f1c4d6fa9b36fd888eaa3515d558e591731012a +Author: Владислав Попов +Date: Tue Oct 31 20:54:27 2023 +0300 + + init commit diff --git a/.DecodeJSON(inputJSON, &j.DockerCompose) b/.DecodeJSON(inputJSON, &j.DockerCompose) new file mode 100644 index 0000000..d636880 --- /dev/null +++ b/.DecodeJSON(inputJSON, &j.DockerCompose) @@ -0,0 +1,68 @@ +commit d1816fc2b3d88cc668123e7af2151c0a0ce39638 (HEAD -> main, origin/main, origin/HEAD) +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:36:03 2023 +0300 + + Update encoding.go + +commit 02513509f43783ad431670400d095c8691f00998 +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:35:16 2023 +0300 + + Delete precode directory + +commit 1aa823fde11a7293c54ab999e26a0903c9d359ec +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:34:29 2023 +0300 + + Update encoding.go + +commit d08c877b39da5c385ccd31f1992d7482c1f2fe1c +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:32:42 2023 +0300 + + Delete encoding/encoding_test.go + +commit b5618c94a0ce72a6f4785d35dc833a15bd34b26b +Merge: 3054315 4ba88b5 +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:30:35 2023 +0300 + + Merge pull request #1 from Yandex-Practicum/prototype + + prototype + +commit 3054315f478524943e0a4f75bd2ad99a50c92323 +Author: bbrodriges +Date: Wed Nov 22 16:27:41 2023 +0300 + + add autotests + +commit 4ba88b56f7b67cfe1dfb9c8d2d26583bf2ce9b27 +Author: Владислав Попов +Date: Thu Nov 2 18:15:01 2023 +0300 + + test and precode added + +commit 4b3729fe73dd1852872b8315fa53c05ff5975171 +Author: Владислав Попов +Date: Wed Nov 1 19:37:42 2023 +0300 + + changing example data + +commit 1d65c20524dd92f164bc6953079798e1877ecdec +Author: Владислав Попов +Date: Wed Nov 1 19:36:53 2023 +0300 + + changing data + +commit b2df37c98e02a93dc7052c00ce8baf377543023a +Author: Владислав Попов +Date: Tue Oct 31 21:11:43 2023 +0300 + + prototype + +commit 3f1c4d6fa9b36fd888eaa3515d558e591731012a +Author: Владислав Попов +Date: Tue Oct 31 20:54:27 2023 +0300 + + init commit diff --git a/.ReadFile(j.FileInput) b/.ReadFile(j.FileInput) new file mode 100644 index 0000000..d636880 --- /dev/null +++ b/.ReadFile(j.FileInput) @@ -0,0 +1,68 @@ +commit d1816fc2b3d88cc668123e7af2151c0a0ce39638 (HEAD -> main, origin/main, origin/HEAD) +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:36:03 2023 +0300 + + Update encoding.go + +commit 02513509f43783ad431670400d095c8691f00998 +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:35:16 2023 +0300 + + Delete precode directory + +commit 1aa823fde11a7293c54ab999e26a0903c9d359ec +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:34:29 2023 +0300 + + Update encoding.go + +commit d08c877b39da5c385ccd31f1992d7482c1f2fe1c +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:32:42 2023 +0300 + + Delete encoding/encoding_test.go + +commit b5618c94a0ce72a6f4785d35dc833a15bd34b26b +Merge: 3054315 4ba88b5 +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:30:35 2023 +0300 + + Merge pull request #1 from Yandex-Practicum/prototype + + prototype + +commit 3054315f478524943e0a4f75bd2ad99a50c92323 +Author: bbrodriges +Date: Wed Nov 22 16:27:41 2023 +0300 + + add autotests + +commit 4ba88b56f7b67cfe1dfb9c8d2d26583bf2ce9b27 +Author: Владислав Попов +Date: Thu Nov 2 18:15:01 2023 +0300 + + test and precode added + +commit 4b3729fe73dd1852872b8315fa53c05ff5975171 +Author: Владислав Попов +Date: Wed Nov 1 19:37:42 2023 +0300 + + changing example data + +commit 1d65c20524dd92f164bc6953079798e1877ecdec +Author: Владислав Попов +Date: Wed Nov 1 19:36:53 2023 +0300 + + changing data + +commit b2df37c98e02a93dc7052c00ce8baf377543023a +Author: Владислав Попов +Date: Tue Oct 31 21:11:43 2023 +0300 + + prototype + +commit 3f1c4d6fa9b36fd888eaa3515d558e591731012a +Author: Владислав Попов +Date: Tue Oct 31 20:54:27 2023 +0300 + + init commit diff --git a/e() b/e() new file mode 100644 index 0000000..d636880 --- /dev/null +++ b/e() @@ -0,0 +1,68 @@ +commit d1816fc2b3d88cc668123e7af2151c0a0ce39638 (HEAD -> main, origin/main, origin/HEAD) +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:36:03 2023 +0300 + + Update encoding.go + +commit 02513509f43783ad431670400d095c8691f00998 +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:35:16 2023 +0300 + + Delete precode directory + +commit 1aa823fde11a7293c54ab999e26a0903c9d359ec +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:34:29 2023 +0300 + + Update encoding.go + +commit d08c877b39da5c385ccd31f1992d7482c1f2fe1c +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:32:42 2023 +0300 + + Delete encoding/encoding_test.go + +commit b5618c94a0ce72a6f4785d35dc833a15bd34b26b +Merge: 3054315 4ba88b5 +Author: supppppppbro <113025477+supppppppbro@users.noreply.github.com> +Date: Wed Nov 22 17:30:35 2023 +0300 + + Merge pull request #1 from Yandex-Practicum/prototype + + prototype + +commit 3054315f478524943e0a4f75bd2ad99a50c92323 +Author: bbrodriges +Date: Wed Nov 22 16:27:41 2023 +0300 + + add autotests + +commit 4ba88b56f7b67cfe1dfb9c8d2d26583bf2ce9b27 +Author: Владислав Попов +Date: Thu Nov 2 18:15:01 2023 +0300 + + test and precode added + +commit 4b3729fe73dd1852872b8315fa53c05ff5975171 +Author: Владислав Попов +Date: Wed Nov 1 19:37:42 2023 +0300 + + changing example data + +commit 1d65c20524dd92f164bc6953079798e1877ecdec +Author: Владислав Попов +Date: Wed Nov 1 19:36:53 2023 +0300 + + changing data + +commit b2df37c98e02a93dc7052c00ce8baf377543023a +Author: Владислав Попов +Date: Tue Oct 31 21:11:43 2023 +0300 + + prototype + +commit 3f1c4d6fa9b36fd888eaa3515d558e591731012a +Author: Владислав Попов +Date: Tue Oct 31 20:54:27 2023 +0300 + + init commit diff --git a/encoding/encoding.go b/encoding/encoding.go index 7592e24..6bcdbcc 100644 --- a/encoding/encoding.go +++ b/encoding/encoding.go @@ -1,7 +1,13 @@ package encoding import ( + "encoding/json" + "fmt" + "io" + "os" + "github.com/Yandex-Practicum/final-project-encoding-go/models" + "gopkg.in/yaml.v3" ) // JSONData тип для перекодирования из JSON в YAML @@ -25,16 +31,86 @@ type MyEncoder interface { // Encoding перекодирует файл из JSON в YAML func (j *JSONData) Encoding() error { - // ниже реализуйте метод - // ... + var dockerCompose models.DockerCompose + + jsonFile, err := os.Open(j.FileInput) + + if err != nil { + return fmt.Errorf("open file: %w", err) + } + + jsonData, err := io.ReadAll(jsonFile) + + jsonFile.Close() + + if err != nil { + return fmt.Errorf("read file: %w", err) + } + + if err = json.Unmarshal(jsonData, &dockerCompose); err != nil { + return fmt.Errorf("unmarshal: %w", err) + } + + yamlData, err := yaml.Marshal(&dockerCompose) + if err != nil { + return fmt.Errorf("marshal: %w", err) + } + + yamlFile, err := os.Create(j.FileOutput) + if err != nil { + return fmt.Errorf("create file: %w", err) + } + + _, err = yamlFile.Write(yamlData) + + yamlFile.Close() + + if err != nil { + return fmt.Errorf("write file: %w", err) + } return nil } // Encoding перекодирует файл из YAML в JSON func (y *YAMLData) Encoding() error { - // Ниже реализуйте метод - // ... + var dockerCompose models.DockerCompose + + yamlFile, err := os.Open(y.FileInput) + + if err != nil { + return fmt.Errorf("open file: %w", err) + } + + yamlData, err := io.ReadAll(yamlFile) + + yamlFile.Close() + + if err != nil { + return fmt.Errorf("read file: %w", err) + } + + if err = yaml.Unmarshal(yamlData, &dockerCompose); err != nil { + return fmt.Errorf("unmarshal: %w", err) + } + + jsonData, err := json.Marshal(&dockerCompose) + if err != nil { + return fmt.Errorf("marshal: %w", err) + } + + jsonFile, err := os.Create(y.FileOutput) + if err != nil { + return fmt.Errorf("create file: %w", err) + } + + _, err = jsonFile.Write(jsonData) + + jsonFile.Close() + + if err != nil { + return fmt.Errorf("write file: %w", err) + } return nil } diff --git a/jsonInput.json b/jsonInput.json new file mode 100644 index 0000000..8b6012d --- /dev/null +++ b/jsonInput.json @@ -0,0 +1 @@ +{"version":"3","services":{"web":{"build":".","ports":["5000:5000"],"volumes":["/usercode/:/code"],"links":["database:backenddb"]},"database":{"image":"mysql/mysql-server:5.7","environment":["MYSQL_ROOT_PASSWORD=root","MYSQL_USER=testuser","MYSQL_PASSWORD=admin123","MYSQL_DATABASE=backend"],"volumes":["/usercode/db/init.sql:/docker-entrypoint-initdb.d/init.sql"]}}} \ No newline at end of file diff --git a/jsonOutput.json b/jsonOutput.json new file mode 100644 index 0000000..8b6012d --- /dev/null +++ b/jsonOutput.json @@ -0,0 +1 @@ +{"version":"3","services":{"web":{"build":".","ports":["5000:5000"],"volumes":["/usercode/:/code"],"links":["database:backenddb"]},"database":{"image":"mysql/mysql-server:5.7","environment":["MYSQL_ROOT_PASSWORD=root","MYSQL_USER=testuser","MYSQL_PASSWORD=admin123","MYSQL_DATABASE=backend"],"volumes":["/usercode/db/init.sql:/docker-entrypoint-initdb.d/init.sql"]}}} \ No newline at end of file diff --git a/yamlInput.yml b/yamlInput.yml new file mode 100644 index 0000000..1c33c8e --- /dev/null +++ b/yamlInput.yml @@ -0,0 +1,19 @@ +version: "3" +services: + web: + build: . + ports: + - 5000:5000 + volumes: + - /usercode/:/code + links: + - database:backenddb + database: + image: mysql/mysql-server:5.7 + environment: + - MYSQL_ROOT_PASSWORD=root + - MYSQL_USER=testuser + - MYSQL_PASSWORD=admin123 + - MYSQL_DATABASE=backend + volumes: + - /usercode/db/init.sql:/docker-entrypoint-initdb.d/init.sql diff --git a/yamlOutput.yml b/yamlOutput.yml new file mode 100644 index 0000000..1c33c8e --- /dev/null +++ b/yamlOutput.yml @@ -0,0 +1,19 @@ +version: "3" +services: + web: + build: . + ports: + - 5000:5000 + volumes: + - /usercode/:/code + links: + - database:backenddb + database: + image: mysql/mysql-server:5.7 + environment: + - MYSQL_ROOT_PASSWORD=root + - MYSQL_USER=testuser + - MYSQL_PASSWORD=admin123 + - MYSQL_DATABASE=backend + volumes: + - /usercode/db/init.sql:/docker-entrypoint-initdb.d/init.sql From 528a0f8f57683a707a1323cc3e643841619e3c45 Mon Sep 17 00:00:00 2001 From: Alex Nuka Date: Fri, 8 Dec 2023 21:42:39 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=84=D0=B0=D0=B9=D0=BB=20.gitignore=20=D0=B8=20?= =?UTF-8?q?=D0=B4=D1=80=D1=83=D0=B3=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- encoding/encoding.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/encoding/encoding.go b/encoding/encoding.go index 6bcdbcc..6ea4287 100644 --- a/encoding/encoding.go +++ b/encoding/encoding.go @@ -41,13 +41,13 @@ func (j *JSONData) Encoding() error { jsonData, err := io.ReadAll(jsonFile) - jsonFile.Close() + defer jsonFile.Close() if err != nil { return fmt.Errorf("read file: %w", err) } - if err = json.Unmarshal(jsonData, &dockerCompose); err != nil { + if err = json.Unmarshal(jsonData, &j.DockerCompose); err != nil { return fmt.Errorf("unmarshal: %w", err) } @@ -63,7 +63,7 @@ func (j *JSONData) Encoding() error { _, err = yamlFile.Write(yamlData) - yamlFile.Close() + defer yamlFile.Close() if err != nil { return fmt.Errorf("write file: %w", err) @@ -84,13 +84,13 @@ func (y *YAMLData) Encoding() error { yamlData, err := io.ReadAll(yamlFile) - yamlFile.Close() + defer yamlFile.Close() if err != nil { return fmt.Errorf("read file: %w", err) } - if err = yaml.Unmarshal(yamlData, &dockerCompose); err != nil { + if err = yaml.Unmarshal(yamlData, &y.DockerCompose); err != nil { return fmt.Errorf("unmarshal: %w", err) } @@ -106,7 +106,7 @@ func (y *YAMLData) Encoding() error { _, err = jsonFile.Write(jsonData) - jsonFile.Close() + defer jsonFile.Close() if err != nil { return fmt.Errorf("write file: %w", err) From 3f10b32eefdd714fe9e230aa61d01c512380438b Mon Sep 17 00:00:00 2001 From: Alex Nuka Date: Fri, 8 Dec 2023 22:22:20 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B8=20=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- encoding/encoding.go | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/encoding/encoding.go b/encoding/encoding.go index 6ea4287..033b40d 100644 --- a/encoding/encoding.go +++ b/encoding/encoding.go @@ -31,18 +31,14 @@ type MyEncoder interface { // Encoding перекодирует файл из JSON в YAML func (j *JSONData) Encoding() error { - var dockerCompose models.DockerCompose jsonFile, err := os.Open(j.FileInput) - if err != nil { return fmt.Errorf("open file: %w", err) } - - jsonData, err := io.ReadAll(jsonFile) - defer jsonFile.Close() + jsonData, err := io.ReadAll(jsonFile) if err != nil { return fmt.Errorf("read file: %w", err) } @@ -51,7 +47,7 @@ func (j *JSONData) Encoding() error { return fmt.Errorf("unmarshal: %w", err) } - yamlData, err := yaml.Marshal(&dockerCompose) + yamlData, err := yaml.Marshal(&j.DockerCompose) if err != nil { return fmt.Errorf("marshal: %w", err) } @@ -60,11 +56,9 @@ func (j *JSONData) Encoding() error { if err != nil { return fmt.Errorf("create file: %w", err) } - - _, err = yamlFile.Write(yamlData) - defer yamlFile.Close() + _, err = yamlFile.Write(yamlData) if err != nil { return fmt.Errorf("write file: %w", err) } @@ -74,18 +68,13 @@ func (j *JSONData) Encoding() error { // Encoding перекодирует файл из YAML в JSON func (y *YAMLData) Encoding() error { - var dockerCompose models.DockerCompose yamlFile, err := os.Open(y.FileInput) - if err != nil { return fmt.Errorf("open file: %w", err) } - - yamlData, err := io.ReadAll(yamlFile) - defer yamlFile.Close() - + yamlData, err := io.ReadAll(yamlFile) if err != nil { return fmt.Errorf("read file: %w", err) } @@ -94,7 +83,7 @@ func (y *YAMLData) Encoding() error { return fmt.Errorf("unmarshal: %w", err) } - jsonData, err := json.Marshal(&dockerCompose) + jsonData, err := json.Marshal(&y.DockerCompose) if err != nil { return fmt.Errorf("marshal: %w", err) } @@ -103,11 +92,9 @@ func (y *YAMLData) Encoding() error { if err != nil { return fmt.Errorf("create file: %w", err) } - - _, err = jsonFile.Write(jsonData) - defer jsonFile.Close() + _, err = jsonFile.Write(jsonData) if err != nil { return fmt.Errorf("write file: %w", err) }