From dc43ecad2c13f051080a5bae3f0d97ca077a57d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20G=C3=BCney?= Date: Fri, 23 Jun 2023 15:14:11 +0300 Subject: [PATCH] json convert --- brokers/garanti/garanti.go | 6 +++--- brokers/ncm/ncm.go | 6 +++--- entity.go | 34 ++++++++++++++++++++++++++++++++++ entity/broker.go | 20 ++++++++++++++------ module.go | 14 -------------- module_test.go | 16 +++++++++++++--- 6 files changed, 67 insertions(+), 29 deletions(-) create mode 100644 entity.go diff --git a/brokers/garanti/garanti.go b/brokers/garanti/garanti.go index 6bfd567..e4c7b6d 100644 --- a/brokers/garanti/garanti.go +++ b/brokers/garanti/garanti.go @@ -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.Ş.", @@ -33,7 +33,7 @@ func New() *Garanti { }} } -func (b Garanti) Info() entity.Info { +func (b Garanti) Info() entity.BrokerInfo { return b.info } diff --git a/brokers/ncm/ncm.go b/brokers/ncm/ncm.go index 3999e48..765090b 100644 --- a/brokers/ncm/ncm.go +++ b/brokers/ncm/ncm.go @@ -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.Ş.", @@ -19,7 +19,7 @@ func New() *NCM { }} } -func (b NCM) Info() entity.Info { +func (b NCM) Info() entity.BrokerInfo { return b.info } diff --git a/entity.go b/entity.go new file mode 100644 index 0000000..69d3fed --- /dev/null +++ b/entity.go @@ -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) +} diff --git a/entity/broker.go b/entity/broker.go index 4b3df25..ef281a6 100644 --- a/entity/broker.go +++ b/entity/broker.go @@ -1,5 +1,7 @@ package entity +import "encoding/json" + type EnumBroker string const ( @@ -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) } diff --git a/module.go b/module.go index c3a2ea7..92d2269 100644 --- a/module.go +++ b/module.go @@ -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{} diff --git a/module_test.go b/module_test.go index b4ad708..c4ff48c 100644 --- a/module_test.go +++ b/module_test.go @@ -9,7 +9,7 @@ import ( "testing" ) -func TestImporter(t *testing.T) { +func TestBrokers(t *testing.T) { brokers := broker.GetBrokers() assertNotNil(t, brokers) @@ -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")