Skip to content

hw02_my_fix #44

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions hw02_fix_app/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/fixme_my_friend/hw02_fix_app

go 1.20

require golang.org/x/example v0.0.0-20230714141244-83a29069fa80 // indirect
2 changes: 0 additions & 2 deletions hw02_fix_app/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
golang.org/x/example v0.0.0-20230714141244-83a29069fa80 h1:dIO+42L/3wms+HG8CICCnwjAoQ9K7FD3JodZPpG5ymY=
golang.org/x/example v0.0.0-20230714141244-83a29069fa80/go.mod h1:DJ5Pz7jIW3ezl8PmYmOf5B1CZ98NJv/GaEcafLB3Lzk=
40 changes: 35 additions & 5 deletions hw02_fix_app/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
package init
package main

import (
"fmt"

"github.com/fixme_my_friend/hw02_fix_app/printer"
"github.com/fixme_my_friend/hw02_fix_app/reader"
"github.com/fixme_my_friend/hw02_fix_app/types"
)

func main() {
path := "data.json"

fmt.Printf("Enter data file path (press Enter to use default): ")
fmt.Scanln(&path)

if len(path) == 0 {
path = "data.json"
}

staff, err := reader.ReadJSON(path, -1)
if err != nil {
fmt.Printf("Error reading JSON: %v\n", err)
return
}

printer.PrintStaff(staff)
}

/*
package main

import (
"fmt"

"github.com/fixme_my_friend/hw02_fix_app/printer"
"github.com/fixme_my_friend/hw02_fix_app/reader"
"github.com/fixme_my_friend/hw02_fix_app/types"
)

func init() {
var path string = "data.json"
func main() {
var path = "data.json"

fmt.Printf("Enter data file path: ")
fmt.Scanln(&path)
Expand All @@ -18,7 +48,6 @@ func init() {

if len(path) == 0 {
path = "data.json"
} else {
}

staff, err = reader.ReadJSON(path, -1)
Expand All @@ -27,3 +56,4 @@ func init() {

printer.PrintStaff(staff)
}
*/
23 changes: 21 additions & 2 deletions hw02_fix_app/printer/pkg.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
package printer

import (
"fmt"

"github.com/fixme_my_friend/hw02_fix_app/types"
)

func PrintStaff(staff []types.Employee) {
for _, s := range staff {
fmt.Println(s.String())
}
}

/*
package printer

import (
"fmt"

"github.com/fixme_my_friend/hw02_fix_app/types"
)

func PrintStaff(staff types.Employee) {
func PrintStaff(staff []types.Employee) {
var str string
for i := 0; i < len(staff); i++ {
str := fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", staff[i].UserID, staff[i].Age, staff[i].Name, staff[i].DepartmentID)
str := fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ",
staff[i].UserID, staff[i].Age, staff[i].Name, staff[i].DepartmentID)
fmt.Println(str)
}

fmt.Println(str)
}
*/
52 changes: 46 additions & 6 deletions hw02_fix_app/reader/pkg.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,59 @@
package reader

import "encoding/json"
import "fmt"
import "io"
import "os"
import (
"encoding/json"
"fmt"
"io"
"os"

import "github.com/fixme_my_friend/hw02_fix_app/types"
"github.com/fixme_my_friend/hw02_fix_app/types"
)

func ReadJSON(filePath string, limit int) ([]types.Employee, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()

bytes, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}

var data []types.Employee

err = json.Unmarshal(bytes, &data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON: %w", err)
}

// если limit >= 0, обрезаем
if limit >= 0 && limit < len(data) {
data = data[:limit]
}

return data, nil
}

/*package reader

import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/fixme_my_friend/hw02_fix_app/types"
)

func ReadJSON(filePath string, limit int) ([]types.Employee, error) {
f, err := os.Open(filePath)
if err != nil {
fmt.Printf("Error: %v", err)
}

byte, err := io.ReadAll(f)
bytes, err := io.ReadAll(f)
if err != nil {
fmt.Printf("Error: %v", err)
return nil, nil
Expand All @@ -28,3 +67,4 @@ func ReadJSON(filePath string, limit int) ([]types.Employee, error) {

return res, nil
}
*/
23 changes: 20 additions & 3 deletions hw02_fix_app/types/employee.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@ package types
import "fmt"

type Employee struct {
UserID int `json:"user_id"`
UserID int `json:"userId"`
Age int `json:"age"`
Name string `json:"name"`
DepartmentID int `json:"department_id"`
DepartmentID int `json:"departmentId"`
}

func (e Employee) String() string {
return fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", e.UserID, e.Age, e.FirstName, e.DepartmentID)
return fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d", e.UserID, e.Age, e.Name, e.DepartmentID)
}

/*
package types

import "fmt"

type Employee struct {
UserID int `json:"userId"`
Age int `json:"age"`
Name string `json:"name"`
DepartmentID int `json:"departmentId"`
}

func (e Employee) String() string {
return fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", e.UserID, e.Age, e.Name, e.DepartmentID)
}
*/