Skip to content

Commit

Permalink
json convert
Browse files Browse the repository at this point in the history
  • Loading branch information
guneyin committed Jun 23, 2023
1 parent 4dd1c3f commit dc43eca
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
6 changes: 3 additions & 3 deletions brokers/garanti/garanti.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const (
)

type Garanti struct {
info entity.Info
info entity.BrokerInfo
}

func New() *Garanti {
return &Garanti{
info: entity.Info{
info: entity.BrokerInfo{
Name: entity.Garanti.String(),
Title: "Garanti Yatırım",
TitleLong: "Garanti Yatırım Menkul Kıymetler A.Ş.",
Expand All @@ -33,7 +33,7 @@ func New() *Garanti {
}}
}

func (b Garanti) Info() entity.Info {
func (b Garanti) Info() entity.BrokerInfo {
return b.info
}

Expand Down
6 changes: 3 additions & 3 deletions brokers/ncm/ncm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
)

type NCM struct {
info entity.Info
info entity.BrokerInfo
}

func New() *NCM {
return &NCM{
info: entity.Info{
info: entity.BrokerInfo{
Name: entity.NCM.String(),
Title: "NCM Investment",
TitleLong: "NCM Investment Menkul Değerler A.Ş.",
Expand All @@ -19,7 +19,7 @@ func New() *NCM {
}}
}

func (b NCM) Info() entity.Info {
func (b NCM) Info() entity.BrokerInfo {
return b.info
}

Expand Down
34 changes: 34 additions & 0 deletions entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package broker

import (
"encoding/json"
"github.com/guneyin/gobist-broker/brokers/garanti"
"github.com/guneyin/gobist-broker/brokers/ncm"
"github.com/guneyin/gobist-broker/entity"
)

var (
_ Broker = (*garanti.Garanti)(nil)
_ Broker = (*ncm.NCM)(nil)
)

type Broker interface {
Info() entity.BrokerInfo
Parse(content []byte) (*entity.Transactions, error)
}

type Brokers map[entity.EnumBroker]Broker

var brokers Brokers

func (b *Brokers) ToJSON() string {
var bl []entity.BrokerInfo

for _, item := range brokers {
bl = append(bl, item.Info())
}

d, _ := json.MarshalIndent(bl, "", " ")

return string(d)
}
20 changes: 14 additions & 6 deletions entity/broker.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package entity

import "encoding/json"

type EnumBroker string

const (
Expand All @@ -11,10 +13,16 @@ func (t EnumBroker) String() string {
return string(t)
}

type Info struct {
Name string
Title string
TitleLong string
Url string
Logo string
type BrokerInfo struct {
Name string `json:"name"`
Title string `json:"title"`
TitleLong string `json:"title_long"`
Url string `json:"url"`
Logo string `json:"logo"`
}

func (bi BrokerInfo) ToJSON() string {
b, _ := json.MarshalIndent(&bi, "", " ")

return string(b)
}
14 changes: 0 additions & 14 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ import (
"sync"
)

var (
_ Broker = (*garanti.Garanti)(nil)
_ Broker = (*ncm.NCM)(nil)
)

type Broker interface {
Info() entity.Info
Parse(content []byte) (*entity.Transactions, error)
}

type Brokers map[entity.EnumBroker]Broker

var brokers Brokers

func init() {
once := &sync.Once{}

Expand Down
16 changes: 13 additions & 3 deletions module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
)

func TestImporter(t *testing.T) {
func TestBrokers(t *testing.T) {
brokers := broker.GetBrokers()
assertNotNil(t, brokers)

Expand All @@ -22,9 +22,19 @@ func TestImporter(t *testing.T) {
}

fmt.Println()
fmt.Printf("%s\n", brokers.ToJSON())
}

b, err := broker.GetBrokerByName("garanti")
assertError(t, err)
func TestBroker(t *testing.T) {
b := broker.GetBroker(entity.Garanti)
assertNotNil(t, b)

fmt.Println("Broker Info:")
fmt.Printf("%s\n", b.Info().ToJSON())
}

func TestImporter(t *testing.T) {
b := broker.GetBroker(entity.Garanti)
assertNotNil(t, b)

ts, err := importFile(b, "single")
Expand Down

0 comments on commit dc43eca

Please sign in to comment.