Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions encoding/encoding.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package encoding

import (
"encoding/json"
"fmt"
"github.com/Yandex-Practicum/final-project-encoding-go/models"
"gopkg.in/yaml.v3"
"os"
)

// JSONData тип для перекодирования из JSON в YAML
Expand All @@ -26,15 +30,66 @@ type MyEncoder interface {
// Encoding перекодирует файл из JSON в YAML
func (j *JSONData) Encoding() error {
// ниже реализуйте метод
// ...
jsonFile, err := os.ReadFile(j.FileInput)
if err != nil {
fmt.Printf("ошибка при чтении файла: %s", err.Error())
}

var dockerCompose models.DockerCompose
AlexanderTyamakov marked this conversation as resolved.
Show resolved Hide resolved

err = json.Unmarshal(jsonFile, &dockerCompose)
if err != nil {
fmt.Printf("ошибка при десериализации из json: %s\n", err.Error())
}

out, err := yaml.Marshal(dockerCompose)
if err != nil {
fmt.Printf("ошибка при сериализации в yaml: %s\n", err.Error())
}

f, err := os.Create("yamlOutput.yaml")
AlexanderTyamakov marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Printf("ошибка при создании файла: %s", err.Error())
}

_, err = f.Write(out)
if err != nil {
fmt.Printf("ошибка при записи данных в файл: %s", err.Error())
}

return nil
}

// Encoding перекодирует файл из YAML в JSON
func (y *YAMLData) Encoding() error {
// Ниже реализуйте метод
// ...

yamlFile, err := os.ReadFile(y.FileInput)
if err != nil {
fmt.Printf("ошибка при чтении файла: %s", err.Error())
}

var dockerCompose models.DockerCompose
AlexanderTyamakov marked this conversation as resolved.
Show resolved Hide resolved

err = yaml.Unmarshal(yamlFile, &dockerCompose)
if err != nil {
fmt.Printf("ошибка при десериализации из yaml: %s\n", err.Error())
}

out, err := json.Marshal(dockerCompose)
if err != nil {
fmt.Printf("ошибка при сериализации в json: %s\n", err.Error())
}

f, err := os.Create("jsonOutput.json")
AlexanderTyamakov marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Printf("ошибка при создании файла: %s", err.Error())
}

_, err = f.Write(out)
if err != nil {
fmt.Printf("ошибка при записи данных в файл: %s", err.Error())
}

return nil
}