Skip to content

Commit

Permalink
fix: update golang generic and kafka three_primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
samwang0723 committed Sep 20, 2024
1 parent 3bea195 commit 1f9131d
Show file tree
Hide file tree
Showing 25 changed files with 60 additions and 87 deletions.
4 changes: 2 additions & 2 deletions internal/app/crawler/broadcastor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (b *broadcastor) Process(_ context.Context, pipe pipeline.Payload) (pipelin
if payload.Strategy == convert.StakeConcentration {
if st := b.cacheInMemory(payload.ParsedContent); st != nil {
intercept = convert.InterceptData{
Data: &[]interface{}{st},
Data: &[]any{st},
Type: payload.Strategy,
}
}
Expand All @@ -57,7 +57,7 @@ func (b *broadcastor) Process(_ context.Context, pipe pipeline.Payload) (pipelin
return pipe, nil
}

func (b *broadcastor) cacheInMemory(data *[]interface{}) *entity.StakeConcentration {
func (b *broadcastor) cacheInMemory(data *[]any) *entity.StakeConcentration {
for _, v := range *data {
if val, ok := v.(*entity.StakeConcentration); ok {
b.memCache[val.StockID] = append(b.memCache[val.StockID], val)
Expand Down
4 changes: 2 additions & 2 deletions internal/app/crawler/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ var (
_ pipeline.Payload = (*crawlerPayload)(nil)

payloadPool = sync.Pool{
New: func() interface{} { return new(crawlerPayload) },
New: func() any { return new(crawlerPayload) },
}
)

type crawlerPayload struct {
RetrievedAt time.Time
ParsedContent *[]interface{}
ParsedContent *[]any
URL string
Date string
RawContent bytes.Buffer
Expand Down
2 changes: 1 addition & 1 deletion internal/app/entity/convert/broadcast.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package convert

type InterceptData struct {
Data *[]interface{}
Data *[]any
Type Source
}
2 changes: 1 addition & 1 deletion internal/app/entity/convert/concentration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Concentration() IConvert {
return &concentrationImpl{}
}

func (c *concentrationImpl) Execute(data *Data) interface{} {
func (c *concentrationImpl) Execute(data *Data) any {
var output *entity.StakeConcentration
if data == nil || len(data.RawData) < 7 {
return output
Expand Down
2 changes: 1 addition & 1 deletion internal/app/entity/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ type Data struct {

// Use strategy pattern to convert entities from parser
type IConvert interface {
Execute(data *Data) interface{}
Execute(data *Data) any
}
2 changes: 1 addition & 1 deletion internal/app/entity/convert/dailyclose.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func DailyClose() IConvert {
return &dailyCloseImpl{}
}

func (c *dailyCloseImpl) Execute(data *Data) interface{} {
func (c *dailyCloseImpl) Execute(data *Data) any {
var output *entity.DailyClose
if data == nil || len(data.RawData) < 17 {
return output
Expand Down
2 changes: 1 addition & 1 deletion internal/app/entity/convert/stock.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Stock() IConvert {
return &stockImpl{}
}

func (c *stockImpl) Execute(data *Data) interface{} {
func (c *stockImpl) Execute(data *Data) any {
var output *entity.Stock
if data == nil || len(data.RawData) < maxLength {
return output
Expand Down
2 changes: 1 addition & 1 deletion internal/app/entity/convert/threeprimary.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func ThreePrimary() IConvert {
return &threePrimaryImpl{}
}

func (c *threePrimaryImpl) Execute(data *Data) interface{} {
func (c *threePrimaryImpl) Execute(data *Data) any {
var output *entity.ThreePrimary
if data == nil || len(data.RawData) < 19 {
return output
Expand Down
2 changes: 1 addition & 1 deletion internal/app/entity/stakeconcentration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

//nolint:nolintlint, gochecknoglobals
var concentrationPool = sync.Pool{
New: func() interface{} { return new(StakeConcentration) },
New: func() any { return new(StakeConcentration) },
}

type StakeConcentration struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/app/entity/threeprimary.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package entity

type ThreePrimary struct {
StockID string `json:"stockId"`
Date string `json:"exchangeDate"`
Date string `json:"date"`
ForeignTradeShares int64 `json:"foreignTradeShares"`
TrustTradeShares int64 `json:"trustTradeShares"`
DealerTradeShares int64 `json:"dealerTradeShares"`
Expand Down
4 changes: 2 additions & 2 deletions internal/app/parser/concentration.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ type concentrationStrategy struct {
func (s *concentrationStrategy) Parse(
input io.Reader,
additional ...string,
) ([]interface{}, error) {
) ([]any, error) {
var records []string

var output []interface{}
var output []any

var isColumn, isTitle, startParsing bool

Expand Down
2 changes: 1 addition & 1 deletion internal/app/parser/concentration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestParseConcentration(t *testing.T) {
t.Parallel()

res := &parserImpl{
result: &[]interface{}{},
result: &[]any{},
}
res.SetStrategy(convert.StakeConcentration, "2023-01-10")
res.Execute(*bytes.NewBuffer([]byte(tt.content)), "https://stockchannelnew.sinotrade.com.tw/z/zc/zco/zco_3704_1.djhtm")
Expand Down
4 changes: 2 additions & 2 deletions internal/app/parser/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ type csvStrategy struct {
}

//nolint:nolintlint, cyclop
func (s *csvStrategy) Parse(input io.Reader, _ ...string) ([]interface{}, error) {
func (s *csvStrategy) Parse(input io.Reader, _ ...string) ([]any, error) {
if s.date == "" {
return nil, ErrParseDayMissing
}

var output []interface{}
var output []any

reader := csv.NewReader(input)
reader.Comma = ','
Expand Down
2 changes: 1 addition & 1 deletion internal/app/parser/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestParseCsv(t *testing.T) {
t.Parallel()

res := &parserImpl{
result: &[]interface{}{},
result: &[]any{},
}

res.SetStrategy(tt.target, "20211130")
Expand Down
12 changes: 6 additions & 6 deletions internal/app/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ const (
type Parser interface {
SetStrategy(source convert.Source, additional ...string)
Execute(in bytes.Buffer, additional ...string) error
Flush() *[]interface{}
Flush() *[]any
}

type Strategy interface {
Parse(in io.Reader, additional ...string) ([]interface{}, error)
Parse(in io.Reader, additional ...string) ([]any, error)
}

type Config struct {
Expand All @@ -52,13 +52,13 @@ type Config struct {
type parserImpl struct {
cfg Config
strategy Strategy
result *[]interface{}
result *[]any
}

func New(cfg Config) Parser {
res := &parserImpl{
cfg: cfg,
result: &[]interface{}{},
result: &[]any{},
}

return res
Expand Down Expand Up @@ -122,9 +122,9 @@ func (p *parserImpl) Execute(in bytes.Buffer, additional ...string) error {
return nil
}

func (p *parserImpl) Flush() *[]interface{} {
func (p *parserImpl) Flush() *[]any {
res := *p.result
p.result = &[]interface{}{}
p.result = &[]any{}

return &res
}
4 changes: 2 additions & 2 deletions internal/app/parser/stocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type htmlStrategy struct {
}

//nolint:nolintlint, cyclop, gocognit
func (s *htmlStrategy) Parse(input io.Reader, _ ...string) ([]interface{}, error) {
var output []interface{}
func (s *htmlStrategy) Parse(input io.Reader, _ ...string) ([]any, error) {
var output []any

var records []string

Expand Down
2 changes: 1 addition & 1 deletion internal/app/parser/stocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestParseHtml(t *testing.T) {
t.Parallel()

res := &parserImpl{
result: &[]interface{}{},
result: &[]any{},
}
res.SetStrategy(convert.TwseStockList)
err := res.Execute(*bytes.NewBuffer([]byte(tt.content)))
Expand Down
27 changes: 0 additions & 27 deletions internal/app/server/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,33 +216,6 @@ func (s *server) Run(ctx context.Context) error {
},
})

//nolint:nolintlint, errcheck
svc.Handler().CronDownload(ctx, &dto.StartCronjobRequest{
Schedule: "30 15 * * 1-5",
Types: []convert.Source{
convert.TwseDailyClose,
convert.TpexDailyClose,
},
})

//nolint:nolintlint, errcheck
svc.Handler().CronDownload(ctx, &dto.StartCronjobRequest{
Schedule: "30 17 * * 1-5",
Types: []convert.Source{
convert.TwseThreePrimary,
convert.TpexThreePrimary,
},
})

//nolint:nolintlint, errcheck
svc.Handler().CronDownload(ctx, &dto.StartCronjobRequest{
Schedule: "40 18 * * 1-5",
Types: []convert.Source{
convert.TwseThreePrimary,
convert.TpexThreePrimary,
},
})

requestChan := make(chan *dto.StartCronjobRequest)
svc.Handler().ListeningDownloadRequest(ctx, requestChan)

Expand Down
8 changes: 4 additions & 4 deletions internal/app/services/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (
//nolint:nolintlint, gochecknoglobals
var jsoni = jsoniter.ConfigCompatibleWithStandardLibrary

func (s *serviceImpl) DailyCloseThroughKafka(ctx context.Context, objs *[]interface{}) error {
func (s *serviceImpl) DailyCloseThroughKafka(ctx context.Context, objs *[]any) error {
for _, val := range *objs {
if res, ok := val.(*entity.DailyClose); ok {
b, err := jsoni.Marshal(res)
Expand Down Expand Up @@ -67,7 +67,7 @@ func (s *serviceImpl) DailyCloseThroughKafka(ctx context.Context, objs *[]interf
return nil
}

func (s *serviceImpl) StockThroughKafka(ctx context.Context, objs *[]interface{}) error {
func (s *serviceImpl) StockThroughKafka(ctx context.Context, objs *[]any) error {
for _, val := range *objs {
if res, ok := val.(*entity.Stock); ok {
b, err := jsoni.Marshal(res)
Expand Down Expand Up @@ -96,7 +96,7 @@ func (s *serviceImpl) StockThroughKafka(ctx context.Context, objs *[]interface{}
return nil
}

func (s *serviceImpl) ThreePrimaryThroughKafka(ctx context.Context, objs *[]interface{}) error {
func (s *serviceImpl) ThreePrimaryThroughKafka(ctx context.Context, objs *[]any) error {
for _, val := range *objs {
if res, ok := val.(*entity.ThreePrimary); ok {
b, err := jsoni.Marshal(res)
Expand Down Expand Up @@ -127,7 +127,7 @@ func (s *serviceImpl) ThreePrimaryThroughKafka(ctx context.Context, objs *[]inte

func (s *serviceImpl) StakeConcentrationThroughKafka(
ctx context.Context,
objs *[]interface{},
objs *[]any,
) error {
for _, val := range *objs {
if res, ok := val.(*entity.StakeConcentration); ok {
Expand Down
Loading

0 comments on commit 1f9131d

Please sign in to comment.