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

Okhrimenko - Homework2 with fixes #8

Open
wants to merge 5 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
76 changes: 76 additions & 0 deletions homeworks/Taras.Okhrimenko-OkhTar/Homework1/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"fmt"
"math"
"sort"
)

// Figures interface for volume
type Figures interface {
volume() float64
}

// Sphere struct create
type Sphere struct {
radius float64
}

func (s *Sphere) volume() float64 {
return (4.0 * math.Pi * math.Pow(s.radius, 3)) / 3
}

// Сone struct create
type Сone struct {
radius, height float64
}

func (c *Сone) volume() float64 {
return (math.Pi * math.Pow(c.radius, 2) * c.height) / 3
}

// Parallelepiped struct create
type Parallelepiped struct {
a, b, height float64
}

func (p *Parallelepiped) volume() float64 {
return p.a * p.b * p.height
}

// ArrFig type create
type ArrFig []Figures

func (a ArrFig) Len() int { return len(a) }
func (a ArrFig) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ArrFig) Less(i, j int) bool { return a[i].volume() < a[j].volume() }

// OutRes output array with figure volume
func OutRes(arr ArrFig) {
for _, el := range arr {
fmt.Printf("%.4v \t||| %v (%T)\n", el.volume(), el, el)
}
fmt.Printf("\n")
}

func main() {
sph1 := &Sphere{9.3}
sph2 := &Sphere{3.4}
cone1 := &Сone{7.3, 2.2}
cone2 := &Сone{3, 8.1}
paral1 := &Parallelepiped{5, 3.1, 3.3}
paral2 := &Parallelepiped{3.9, 3.7, 4}
paral3 := &Parallelepiped{0.4, 6.4, 5}

arr := make(ArrFig, 0, 7)

arr = append(arr, sph1, sph2, cone1, cone2, paral1, paral2, paral3)

fmt.Println("Input data (before sort):")
OutRes(arr)

sort.Sort(arr)

fmt.Println("Sorted figure volume values:")
OutRes(arr)
}
3 changes: 3 additions & 0 deletions homeworks/Taras.Okhrimenko-OkhTar/Homework1/task.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1. Описать обємні фігури: куля, конус, паралелепіпед використовуючи структури
2. реалізувать список/контейнер для фігур (додавать можна різні фігури).
він повинен сортувати свій вміст за обємом (для реалізації використовать інтерфейси)
91 changes: 91 additions & 0 deletions homeworks/Taras.Okhrimenko-OkhTar/Homework2/jsonurl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// OLD version

package main

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"reflect"
"time"
)

// NameReader -
// source can be local file or url of JSON file
// read this file, parse and return "key" field value
type NameReader interface {
Read(source string, key string) []string
}

type User struct {
ID int `json:"id"`
Name string `json:"username"`
Email string `json:"email"`
Addr Addr `json:"address"`
Phone string `json:"phone"`
WebSite string `json:"website"`
Company Company `json:"company"`
}

type Addr struct {
Street string `json:"street"`
Suite string `json:"suite"`
City string `json:"city"`
Zipcode string `json:"zipcode"`
Geo map[string]string `json:"geo"`
}

type Company struct {
Name string `json:"name"`
CatchPhrase string `json:"catchPhrase"`
Bs string `json:"bs"`
}

var myClient = &http.Client{Timeout: 10 * time.Second}

func Read(source string, key string) []string {
r, err := myClient.Get(source)
if err != nil {
panic(err)
}
defer r.Body.Close()

var usersList []User
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
respByte := buf.Bytes()
if err := json.Unmarshal(respByte, &usersList); err != nil {
panic(err)
}

var valArr = make([]string, 0, len(usersList))

for _, user := range usersList {
e := reflect.ValueOf(&user).Elem()

var resArr = make([]string, 0, e.NumField())

for i := 0; i < e.NumField(); i++ {
varName := e.Type().Field(i).Name
resArr = append(resArr, varName)
}

for i, v := range resArr {
if key == v {
varValue := e.Field(i).Interface()
valArr = append(valArr, fmt.Sprintf("%v", varValue))
}
}

}

return valArr
}

func main() {
url := "https://jsonplaceholder.typicode.com/users"
res := Read(url, "Name")

fmt.Println(res)
}
91 changes: 91 additions & 0 deletions homeworks/Taras.Okhrimenko-OkhTar/Homework2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// NEW version

package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)

// NameReader -
// source can be local file or url of JSON file
// read this file, parse and return "key" field value
type NameReader interface {
Read(source string, key string) (string, error)
}

type User struct {
ID int `json:"id"`
Name string `json:"name"`
Username string `json:"username"`
Email string `json:"email"`
Address struct {
Street string `json:"street"`
Suite string `json:"suite"`
City string `json:"city"`
Zipcode string `json:"zipcode"`
Geo struct {
Lat string `json:"lat"`
Lng string `json:"lng"`
} `json:"geo"`
} `json:"address"`
Phone string `json:"phone"`
Website string `json:"website"`
Company struct {
Name string `json:"name"`
CatchPhrase string `json:"catchPhrase"`
Bs string `json:"bs"`
} `json:"company"`
}

// getJSONFromURLByte func for getting json from url
func getJSONFromURLByte(url string) ([]byte, error) {
var myClient = &http.Client{Timeout: 10 * time.Second}

r, err := myClient.Get(url)
if err != nil {
return nil, err
}
defer r.Body.Close()

buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
respByte := buf.Bytes()

return respByte, nil
}

func (u *User) Read(source string, key string) (string, error) {
var v map[string]interface{}

if err := json.Unmarshal([]byte(source), &v); err != nil {
return "", err
}

if val, ok := v[key]; ok {
return fmt.Sprintf("%v", val), nil
}
return "No key", nil
}

func main() {
url := "https://jsonplaceholder.typicode.com/users/3"

jsonStream, err := getJSONFromURLByte(url)
if err != nil {
log.Println(err)
}
fmt.Printf("Json:\n %v\n", string(jsonStream))

var usr User
res, err := usr.Read(string(jsonStream), "name")
if err != nil {
log.Println(err)
}
fmt.Println(res)

}
17 changes: 17 additions & 0 deletions homeworks/Taras.Okhrimenko-OkhTar/simpleAPI/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module simpleAPI

go 1.13

require (
github.com/gin-gonic/gin v1.5.0
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/google/uuid v1.1.1
github.com/json-iterator/go v1.1.8 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.11 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
golang.org/x/sys v0.0.0-20191210023423-ac6580df4449 // indirect
gopkg.in/go-playground/validator.v9 v9.30.2 // indirect
gopkg.in/yaml.v2 v2.2.7 // indirect
)
64 changes: 64 additions & 0 deletions homeworks/Taras.Okhrimenko-OkhTar/simpleAPI/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc=
github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do=
github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc=
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM=
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.8 h1:QiWkFLKq0T7mpzwOTu6BzNDbfTE8OLrYhVKYMLF46Ok=
github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
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.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191210023423-ac6580df4449 h1:gSbV7h1NRL2G1xTg/owz62CST1oJBmxy4QpMMregXVQ=
golang.org/x/sys v0.0.0-20191210023423-ac6580df4449/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc=
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/go-playground/validator.v9 v9.30.2 h1:icxYLlYflpazIV3ufMoNB9h9SYMQ37DZ8CTwkU4pnOs=
gopkg.in/go-playground/validator.v9 v9.30.2/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package handler

import (
"net/http"
"simpleAPI/platform/order"

"github.com/gin-gonic/gin"
)

func AllOrdersGet(order *order.AllOrders) gin.HandlerFunc {
return func(c *gin.Context) {
res := order.GetAll()
c.JSON(http.StatusOK, res)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package handler

import (
"net/http"
"simpleAPI/platform/order"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
)

type allOrdersPostRequest struct {
ID string `json:"id"`
Products []int `json:"products"`
}

func AllOrdersPost(o *order.AllOrders) gin.HandlerFunc {
return func(c *gin.Context) {
reqBody := allOrdersPostRequest{}
c.Bind(&reqBody)

item := order.Order{
ID: uuid.New().String(),
Products: reqBody.Products,
}

if (item.ID != "") || (item.Products != nil) {
o.Add(item)
}

c.JSON(http.StatusOK, item.ID)

c.Status(http.StatusNoContent)
}
}
Loading