From 7a014c8f0858703ea4f70d1201976ae8c2b45d73 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 20 Dec 2015 02:29:13 +0100 Subject: improved type safety diff --git a/fetcher.go b/fetcher.go index 300abef..a8ac22c 100644 --- a/fetcher.go +++ b/fetcher.go @@ -101,7 +101,7 @@ func FetchFileCurl(ctx *ImportContext, res *FetchResult, uri *url.URL) (err erro cbdata := &FetcherCurlCBData{remotename: path.Base(uri.Path)} defer cbdata.Cleanup() - if cbdata.basepath, err = ioutil.TempDir(ctx.Config.TempDir, "rhimportd-"); err != nil { + if cbdata.basepath, err = ioutil.TempDir(ctx.conf.TempDir, "rhimportd-"); err != nil { return } @@ -142,7 +142,7 @@ func FetchFileLocal(ctx *ImportContext, res *FetchResult, uri *url.URL) (err err ctx.ProgressCallBack(1, "fetching", 1.0, ctx.ProgressCallBackData) } - ctx.SourceFile = filepath.Join(ctx.Config.LocalFetchDir, path.Clean("/"+uri.Path)) + ctx.SourceFile = filepath.Join(ctx.conf.LocalFetchDir, path.Clean("/"+uri.Path)) var src *os.File if src, err = os.Open(ctx.SourceFile); err != nil { res.ResponseCode = http.StatusBadRequest @@ -194,13 +194,14 @@ func checkPassword(ctx *ImportContext, result *FetchResult) (err error) { cached := true for { + res_ch := make(chan getPasswordResult) req := getPasswordRequest{} req.username = ctx.UserName req.cached = cached - req.response = make(chan getPasswordResult) - ctx.RdDb.getPasswordChan <- req + req.response = res_ch + ctx.rddb.getPasswordChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { return res.err } diff --git a/importer.go b/importer.go index 1404259..f65515b 100644 --- a/importer.go +++ b/importer.go @@ -40,8 +40,8 @@ var ( ) type ImportContext struct { - *Config - *RdDb + conf *Config + rddb *RdDbChan UserName string Password string Trusted bool @@ -63,10 +63,10 @@ type ImportContext struct { ProgressCallBackData interface{} } -func NewImportContext(conf *Config, rddb *RdDb, user string) *ImportContext { +func NewImportContext(conf *Config, rddb *RdDbChan, user string) *ImportContext { ctx := new(ImportContext) - ctx.Config = conf - ctx.RdDb = rddb + ctx.conf = conf + ctx.rddb = rddb ctx.UserName = user ctx.Password = "" ctx.Trusted = false @@ -121,13 +121,14 @@ func (ctx *ImportContext) SanityCheck() error { } func (ctx *ImportContext) getPassword(cached bool) (err error) { + res_ch := make(chan getPasswordResult) req := getPasswordRequest{} req.username = ctx.UserName req.cached = cached - req.response = make(chan getPasswordResult) - ctx.RdDb.getPasswordChan <- req + req.response = res_ch + ctx.rddb.getPasswordChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { return res.err } @@ -136,12 +137,13 @@ func (ctx *ImportContext) getPassword(cached bool) (err error) { } func (ctx *ImportContext) getGroupOfCart() error { + res_ch := make(chan getGroupOfCartResult) req := getGroupOfCartRequest{} req.cart = ctx.Cart - req.response = make(chan getGroupOfCartResult) - ctx.RdDb.getGroupOfCartChan <- req + req.response = res_ch + ctx.rddb.getGroupOfCartChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { return res.err } @@ -150,12 +152,13 @@ func (ctx *ImportContext) getGroupOfCart() error { } func (ctx *ImportContext) getShowInfo() (carts []uint, err error) { + res_ch := make(chan getShowInfoResult) req := getShowInfoRequest{} req.showid = ctx.ShowId - req.response = make(chan getShowInfoResult) - ctx.RdDb.getShowInfoChan <- req + req.response = res_ch + ctx.rddb.getShowInfoChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { err = res.err return @@ -170,12 +173,13 @@ func (ctx *ImportContext) getShowInfo() (carts []uint, err error) { } func (ctx *ImportContext) checkMusicGroup() (bool, error) { + res_ch := make(chan checkMusicGroupResult) req := checkMusicGroupRequest{} req.group = ctx.GroupName - req.response = make(chan checkMusicGroupResult) - ctx.RdDb.checkMusicGroupChan <- req + req.response = res_ch + ctx.rddb.checkMusicGroupChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { return false, res.err } @@ -183,12 +187,13 @@ func (ctx *ImportContext) checkMusicGroup() (bool, error) { } func (ctx *ImportContext) getMusicInfo() (err error) { + res_ch := make(chan getMusicInfoResult) req := getMusicInfoRequest{} req.group = ctx.GroupName - req.response = make(chan getMusicInfoResult) - ctx.RdDb.getMusicInfoChan <- req + req.response = res_ch + ctx.rddb.getMusicInfoChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { return res.err } @@ -251,7 +256,7 @@ func add_cart(ctx *ImportContext, res *ImportResult) (err error) { w.Close() var resp *http.Response - if resp, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { + if resp, err = send_post_request(ctx.conf.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { return } defer resp.Body.Close() @@ -296,7 +301,7 @@ func add_cut(ctx *ImportContext, res *ImportResult) (err error) { w.Close() var resp *http.Response - if resp, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { + if resp, err = send_post_request(ctx.conf.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { return } defer resp.Body.Close() @@ -343,7 +348,7 @@ func remove_cart(ctx *ImportContext, res *ImportResult) (err error) { w.Close() var resp *http.Response - if resp, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { + if resp, err = send_post_request(ctx.conf.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { return } defer resp.Body.Close() @@ -380,7 +385,7 @@ func remove_cut(ctx *ImportContext, res *ImportResult) (err error) { w.Close() var resp *http.Response - if resp, err = send_post_request(ctx.Config.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { + if resp, err = send_post_request(ctx.conf.RDXportEndpoint, &b, w.FormDataContentType()); err != nil { return } defer resp.Body.Close() @@ -455,7 +460,7 @@ func import_audio(ctx *ImportContext, res *ImportResult) (err error) { if easy != nil { defer easy.Cleanup() - easy.Setopt(curl.OPT_URL, ctx.Config.RDXportEndpoint) + easy.Setopt(curl.OPT_URL, ctx.conf.RDXportEndpoint) easy.Setopt(curl.OPT_POST, true) var form *curl.Form diff --git a/rddb.go b/rddb.go index 25ef876..f596171 100644 --- a/rddb.go +++ b/rddb.go @@ -49,7 +49,7 @@ type getPasswordResult struct { type getPasswordRequest struct { username string cached bool - response chan getPasswordResult + response chan<- getPasswordResult } type getGroupOfCartResult struct { @@ -59,7 +59,7 @@ type getGroupOfCartResult struct { type getGroupOfCartRequest struct { cart uint - response chan getGroupOfCartResult + response chan<- getGroupOfCartResult } type getShowInfoResult struct { @@ -73,7 +73,7 @@ type getShowInfoResult struct { type getShowInfoRequest struct { showid uint - response chan getShowInfoResult + response chan<- getShowInfoResult } type checkMusicGroupResult struct { @@ -83,7 +83,7 @@ type checkMusicGroupResult struct { type checkMusicGroupRequest struct { group string - response chan checkMusicGroupResult + response chan<- checkMusicGroupResult } type getMusicInfoResult struct { @@ -94,7 +94,15 @@ type getMusicInfoResult struct { type getMusicInfoRequest struct { group string - response chan getMusicInfoResult + response chan<- getMusicInfoResult +} + +type RdDbChan struct { + getPasswordChan chan<- getPasswordRequest + getGroupOfCartChan chan<- getGroupOfCartRequest + getShowInfoChan chan<- getShowInfoRequest + checkMusicGroupChan chan<- checkMusicGroupRequest + getMusicInfoChan chan<- getMusicInfoRequest } type RdDb struct { @@ -289,6 +297,16 @@ func (self *RdDb) dispatchRequests() { } } +func (self *RdDb) GetChannels() *RdDbChan { + ch := &RdDbChan{} + ch.getPasswordChan = self.getPasswordChan + ch.getGroupOfCartChan = self.getGroupOfCartChan + ch.getShowInfoChan = self.getShowInfoChan + ch.checkMusicGroupChan = self.checkMusicGroupChan + ch.getMusicInfoChan = self.getMusicInfoChan + return ch +} + func (self *RdDb) Cleanup() { self.quit <- true <-self.done diff --git a/session.go b/session.go index 219c233..718d13e 100644 --- a/session.go +++ b/session.go @@ -61,7 +61,7 @@ type sessionRunResponse struct { type sessionRunRequest struct { user string id string - response chan sessionRunResponse + response chan<- sessionRunResponse } type sessionAddProgressHandlerResponse struct { @@ -72,8 +72,8 @@ type sessionAddProgressHandlerRequest struct { user string id string userdata interface{} - handler chan ProgressData - response chan sessionAddProgressHandlerResponse + handler chan<- ProgressData + response chan<- sessionAddProgressHandlerResponse } type sessionAddDoneHandlerResponse struct { @@ -84,8 +84,8 @@ type sessionAddDoneHandlerRequest struct { user string id string userdata interface{} - handler chan ImportResult - response chan sessionAddDoneHandlerResponse + handler chan<- ImportResult + response chan<- sessionAddDoneHandlerResponse } type sessionRemoveResponse struct { @@ -125,12 +125,13 @@ func (self *SessionStore) newSession(ctx ImportContext) (resp newSessionResponse } func (self *SessionStore) NewSession(ctx ImportContext) (string, error) { + res_ch := make(chan newSessionResponse) req := newSessionRequest{} req.ctx = ctx - req.response = make(chan newSessionResponse) + req.response = res_ch self.newChan <- req - res := <-req.response + res := <-res_ch if res.err != nil { return "", res.err } @@ -147,17 +148,18 @@ func (self *SessionStore) run(user, id string) (resp sessionRunResponse) { } func (self *SessionStore) Run(user, id string) error { + res_ch := make(chan sessionRunResponse) req := sessionRunRequest{} req.user = user req.id = id - req.response = make(chan sessionRunResponse) + req.response = res_ch self.runChan <- req - res := <-req.response + res := <-res_ch return res.err } -func (self *SessionStore) addProgressHandler(user, id string, userdata interface{}, handler chan ProgressData) (resp sessionAddProgressHandlerResponse) { +func (self *SessionStore) addProgressHandler(user, id string, userdata interface{}, handler chan<- ProgressData) (resp sessionAddProgressHandlerResponse) { if _, exists := self.store[user][id]; exists { rhdl.Printf("SessionStore: adding progress handler to '%s/%s'", user, id) } else { @@ -166,20 +168,21 @@ func (self *SessionStore) addProgressHandler(user, id string, userdata interface return } -func (self *SessionStore) AddProgressHandler(user, id string, userdata interface{}, handler chan ProgressData) error { +func (self *SessionStore) AddProgressHandler(user, id string, userdata interface{}, handler chan<- ProgressData) error { + res_ch := make(chan sessionAddProgressHandlerResponse) req := sessionAddProgressHandlerRequest{} req.user = user req.id = id req.userdata = userdata req.handler = handler - req.response = make(chan sessionAddProgressHandlerResponse) + req.response = res_ch self.addProgressChan <- req - res := <-req.response + res := <-res_ch return res.err } -func (self *SessionStore) addDoneHandler(user, id string, userdata interface{}, handler chan ImportResult) (resp sessionAddDoneHandlerResponse) { +func (self *SessionStore) addDoneHandler(user, id string, userdata interface{}, handler chan<- ImportResult) (resp sessionAddDoneHandlerResponse) { if _, exists := self.store[user][id]; exists { rhdl.Printf("SessionStore: adding done handler to '%s/%s'", user, id) } else { @@ -188,16 +191,17 @@ func (self *SessionStore) addDoneHandler(user, id string, userdata interface{}, return } -func (self *SessionStore) AddDoneHandler(user, id string, userdata interface{}, handler chan ImportResult) error { +func (self *SessionStore) AddDoneHandler(user, id string, userdata interface{}, handler chan<- ImportResult) error { + res_ch := make(chan sessionAddDoneHandlerResponse) req := sessionAddDoneHandlerRequest{} req.user = user req.id = id req.userdata = userdata req.handler = handler - req.response = make(chan sessionAddDoneHandlerResponse) + req.response = res_ch self.addDoneChan <- req - res := <-req.response + res := <-res_ch return res.err } @@ -218,13 +222,14 @@ func (self *SessionStore) remove(user, id string) (resp sessionRemoveResponse) { } func (self *SessionStore) Remove(user, id string) error { + res_ch := make(chan sessionRemoveResponse) req := sessionRemoveRequest{} req.user = user req.id = id - req.response = make(chan sessionRemoveResponse) + req.response = res_ch self.removeChan <- req - res := <-req.response + res := <-res_ch return res.err } -- cgit v0.10.2