From 5d8b8cf1ea44177ec38fc186713f97cc88c472c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?walker-sheng=E2=80=9D?= Date: Mon, 24 Jun 2024 18:20:11 +0800 Subject: [PATCH] =?UTF-8?q?example=20repository=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/build_repo_example/main.go | 25 +++++++++-------- example/model_impl/section.go | 2 +- example/repository/repo.go | 44 ++++++++++++++++++++++-------- model/link.go | 3 ++ model/section.go | 4 ++- model/station.go | 2 ++ model/turnout.go | 4 ++- 7 files changed, 58 insertions(+), 26 deletions(-) diff --git a/example/build_repo_example/main.go b/example/build_repo_example/main.go index 8cabc95..30d7500 100644 --- a/example/build_repo_example/main.go +++ b/example/build_repo_example/main.go @@ -2,32 +2,33 @@ package main import ( "joylink.club/rtss-core/example/data_proto" + modelimpl "joylink.club/rtss-core/example/model_impl" + "joylink.club/rtss-core/example/repository" "joylink.club/rtss-core/model" ) -var ( - _stationMap = make(map[uint32]*data_proto.Station) - _sectionMap = make(map[uint32]*data_proto.Section) - _turnoutMap = make(map[uint32]*data_proto.Turnout) -) - func main() { + repo := repository.NewRepository("test1").(*repository.Repository) rtssGraphicStorage := data_proto.GetLineSytle1() // log.Println(rtssGraphicStorage.Stations) // log.Println(rtssGraphicStorage.Section) // log.Println(rtssGraphicStorage.Turnouts) for _, station := range rtssGraphicStorage.Stations { - _stationMap[station.Common.Id] = station + repo.StationDataMap[station.Common.Id] = station } for _, section := range rtssGraphicStorage.Section { - if section.CentralizedStations == nil || len(section.CentralizedStations) == 0 || _stationMap[section.CentralizedStations[0]] == nil { + belongStation := repo.SectionDataMap[section.CentralizedStations[0]] + if section.CentralizedStations == nil || len(section.CentralizedStations) == 0 || repo.SectionDataMap[section.CentralizedStations[0]] == nil { continue } - _sectionMap[section.Common.Id] = section - model.NewSection(getSectionUid(section)) + repo.SectionDataMap[section.Common.Id] = section + uid := getSectionUid(section, belongStation) + repo.IpMapping = append(repo.IpMapping, repository.NewIdMapping(section.Common.Id, uid)) + sectionModel := model.NewSection(uid) + repo.SectionMap[uid] = &modelimpl.Section{SectionImpl: sectionModel} } } -func getSectionUid(section *data_proto.Section) string { - return _stationMap[section.CentralizedStations[0]].Code + "-" + section.Code +func getSectionUid(section *data_proto.Section, belongStation *data_proto.Section) string { + return belongStation.Code + "-" + section.Code } diff --git a/example/model_impl/section.go b/example/model_impl/section.go index 1283b3b..4ee0c0c 100644 --- a/example/model_impl/section.go +++ b/example/model_impl/section.go @@ -3,5 +3,5 @@ package modelimpl import "joylink.club/rtss-core/model" type Section struct { - model.SectionImpl + *model.SectionImpl } diff --git a/example/repository/repo.go b/example/repository/repo.go index 52c2dee..27d7a8b 100644 --- a/example/repository/repo.go +++ b/example/repository/repo.go @@ -1,24 +1,46 @@ package repository import ( + "joylink.club/rtss-core/example/data_proto" modelimpl "joylink.club/rtss-core/example/model_impl" "joylink.club/rtss-core/model" "joylink.club/rtss-core/repo" ) +// Id映射 +type IdMapping struct { + GraphicId uint32 + Uid string +} + +func NewIdMapping(graphicId uint32, uid string) *IdMapping { + return &IdMapping{ + GraphicId: graphicId, + Uid: uid, + } +} + type Repository struct { - id string - stationMap map[string]*modelimpl.Station - sectionMap map[string]*modelimpl.Section - turnoutMap map[string]*modelimpl.Turnout + id string + StationDataMap map[uint32]*data_proto.Station + SectionDataMap map[uint32]*data_proto.Section + TurnoutDataMap map[uint32]*data_proto.Turnout + IpMapping []*IdMapping + StationMap map[string]*modelimpl.Station + SectionMap map[string]*modelimpl.Section + TurnoutMap map[string]*modelimpl.Turnout } func NewRepository(id string) repo.Repo { return &Repository{ - id: id, - stationMap: make(map[string]*modelimpl.Station), - sectionMap: make(map[string]*modelimpl.Section), - turnoutMap: make(map[string]*modelimpl.Turnout), + id: id, + StationDataMap: make(map[uint32]*data_proto.Station), + SectionDataMap: make(map[uint32]*data_proto.Section), + TurnoutDataMap: make(map[uint32]*data_proto.Turnout), + IpMapping: make([]*IdMapping, 0), + StationMap: make(map[string]*modelimpl.Station), + SectionMap: make(map[string]*modelimpl.Section), + TurnoutMap: make(map[string]*modelimpl.Turnout), } } @@ -27,13 +49,13 @@ func (r *Repository) Id() string { } func (r *Repository) GetStationByUid(uid string) model.Station { - return r.stationMap[uid] + return r.StationMap[uid] } func (r *Repository) GetSectionByUid(uid string) model.Section { - return r.sectionMap[uid] + return r.SectionMap[uid] } func (r *Repository) GetTurnoutByUid(uid string) model.Turnout { - return r.turnoutMap[uid] + return r.TurnoutMap[uid] } diff --git a/model/link.go b/model/link.go index 84867c5..b87f036 100644 --- a/model/link.go +++ b/model/link.go @@ -16,6 +16,9 @@ type LinkNode interface { PipeElement } +var _ Link = (*LinkImpl)(nil) +var _ LinkNode = (*LinkNodeImpl)(nil) + type LinkImpl struct { uid string paLinkNode *PipeLink diff --git a/model/section.go b/model/section.go index 506ec2e..563bfc9 100644 --- a/model/section.go +++ b/model/section.go @@ -12,7 +12,9 @@ type SectionImpl struct { pbPipeLink *PipeLink } -func NewSection(uid string) Section { +var _ Section = (*SectionImpl)(nil) + +func NewSection(uid string) *SectionImpl { return &SectionImpl{ uid: uid, } diff --git a/model/station.go b/model/station.go index a7dbe69..e00e576 100644 --- a/model/station.go +++ b/model/station.go @@ -5,6 +5,8 @@ type Station interface { IsCentralized() bool } +var _ Station = (*StationImpl)(nil) + type StationImpl struct { uid string centralized bool diff --git a/model/turnout.go b/model/turnout.go index c1c239b..5b14ea8 100644 --- a/model/turnout.go +++ b/model/turnout.go @@ -8,6 +8,8 @@ type Turnout interface { PipeElement } +var _ Turnout = (*TurnoutImpl)(nil) + type TurnoutImpl struct { uid string paPipeLink *PipeLink @@ -15,7 +17,7 @@ type TurnoutImpl struct { pcPipeLink *PipeLink } -func NewTurnout(uid string) Turnout { +func NewTurnout(uid string) *TurnoutImpl { if strings.Trim(uid, " ") == "" { panic("Turnout uid is empty") }