From 24faab8c18dfe11b9c09a6a62965f60cad2f5cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Budai?= Date: Thu, 5 Dec 2019 10:15:21 +0100 Subject: [PATCH] store: clean up blueprint API Prior to this commit blueprint getters looked like C-style API with output parameters. This commit refactors them to more conventional multiple return values API. --- internal/store/store.go | 26 ++++++++++---------------- internal/weldr/api.go | 34 ++++++++++++++++------------------ 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/internal/store/store.go b/internal/store/store.go index 459e1d24e..14b792859 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -256,17 +256,16 @@ func (s *Store) GetAllComposes() map[uuid.UUID]Compose { return composes } -func (s *Store) GetBlueprint(name string, bp *blueprint.Blueprint, changed *bool) bool { +func (s *Store) GetBlueprint(name string) (*blueprint.Blueprint, bool) { s.mu.RLock() defer s.mu.RUnlock() - var inWorkspace bool - *bp, inWorkspace = s.Workspace[name] + bp, inWorkspace := s.Workspace[name] if !inWorkspace { var ok bool - *bp, ok = s.Blueprints[name] + bp, ok = s.Blueprints[name] if !ok { - return false + return nil, false } } @@ -284,21 +283,16 @@ func (s *Store) GetBlueprint(name string, bp *blueprint.Blueprint, changed *bool bp.Version = "0.0.0" } - if changed != nil { - *changed = inWorkspace - } - - return true + return &bp, inWorkspace } -func (s *Store) GetBlueprintCommitted(name string, bp *blueprint.Blueprint) bool { +func (s *Store) GetBlueprintCommitted(name string) *blueprint.Blueprint { s.mu.RLock() defer s.mu.RUnlock() - var ok bool - *bp, ok = s.Blueprints[name] + bp, ok := s.Blueprints[name] if !ok { - return false + return nil } // cockpit-composer cannot deal with missing "packages" or "modules" @@ -315,7 +309,7 @@ func (s *Store) GetBlueprintCommitted(name string, bp *blueprint.Blueprint) bool bp.Version = "0.0.0" } - return true + return &bp } func (s *Store) GetBlueprintChange(name string, commit string) *blueprint.Change { @@ -354,7 +348,7 @@ func bumpVersion(str string) string { } } - return fmt.Sprintf("%d.%d.%d", v[0], v[1], v[2] + 1) + return fmt.Sprintf("%d.%d.%d", v[0], v[1], v[2]+1) } func (s *Store) PushBlueprint(bp blueprint.Blueprint, commitMsg string) { diff --git a/internal/weldr/api.go b/internal/weldr/api.go index 6486a1b86..526244d53 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -692,9 +692,9 @@ func (api *API) blueprintsInfoHandler(writer http.ResponseWriter, request *http. if i == 0 { name = name[1:] } - var blueprint blueprint.Blueprint - var changed bool - if !api.store.GetBlueprint(name, &blueprint, &changed) { + + blueprint, changed := api.store.GetBlueprint(name) + if blueprint == nil { errors := responseError{ ID: "UnknownBlueprint", Msg: fmt.Sprintf("%s: ", name), @@ -702,7 +702,7 @@ func (api *API) blueprintsInfoHandler(writer http.ResponseWriter, request *http. statusResponseError(writer, http.StatusBadRequest, errors) return } - blueprints = append(blueprints, blueprint) + blueprints = append(blueprints, *blueprint) changes = append(changes, change{changed, blueprint.Name}) } @@ -779,8 +779,8 @@ func (api *API) blueprintsDepsolveHandler(writer http.ResponseWriter, request *h if i == 0 { name = name[1:] } - var blueprint blueprint.Blueprint - if !api.store.GetBlueprint(name, &blueprint, nil) { + blueprint, _ := api.store.GetBlueprint(name) + if blueprint == nil { errors := responseError{ ID: "UnknownBlueprint", Msg: fmt.Sprintf("%s: blueprint not found", name), @@ -820,7 +820,7 @@ func (api *API) blueprintsDepsolveHandler(writer http.ResponseWriter, request *h return } - blueprints = append(blueprints, entry{blueprint, dependencies}) + blueprints = append(blueprints, entry{*blueprint, dependencies}) } json.NewEncoder(writer).Encode(reply{ @@ -861,9 +861,8 @@ func (api *API) blueprintsFreezeHandler(writer http.ResponseWriter, request *htt if i == 0 { name = name[1:] } - var blueprint blueprint.Blueprint - var changed bool - if !api.store.GetBlueprint(name, &blueprint, &changed) { + blueprint, _ := api.store.GetBlueprint(name) + if blueprint == nil { err := responseError{ ID: "UnknownBlueprint", Msg: fmt.Sprintf("%s: blueprint_not_found", name), @@ -903,7 +902,7 @@ func (api *API) blueprintsFreezeHandler(writer http.ResponseWriter, request *htt } } - blueprints = append(blueprints, blueprintFrozen{blueprint}) + blueprints = append(blueprints, blueprintFrozen{*blueprint}) } json.NewEncoder(writer).Encode(reply{ @@ -961,8 +960,9 @@ func (api *API) blueprintsDiffHandler(writer http.ResponseWriter, request *http. } // Fetch old and new blueprint details from store and return error if not found - var oldBlueprint, newBlueprint blueprint.Blueprint - if !api.store.GetBlueprintCommitted(name, &oldBlueprint) || !api.store.GetBlueprint(name, &newBlueprint, nil) { + oldBlueprint := api.store.GetBlueprintCommitted(name) + newBlueprint, _ := api.store.GetBlueprint(name) + if oldBlueprint == nil || newBlueprint == nil { errors := responseError{ ID: "UnknownBlueprint", Msg: fmt.Sprintf("Unknown blueprint name: %s", name), @@ -1237,12 +1237,10 @@ func (api *API) composeHandler(writer http.ResponseWriter, request *http.Request } } - bp := blueprint.Blueprint{} - changed := false - found := api.store.GetBlueprint(cr.BlueprintName, &bp, &changed) // TODO: what to do with changed? + bp, _ := api.store.GetBlueprint(cr.BlueprintName) - if found { - err := api.store.PushCompose(reply.BuildID, &bp, cr.ComposeType, uploadTarget) + if bp != nil { + err := api.store.PushCompose(reply.BuildID, bp, cr.ComposeType, uploadTarget) // TODO: we should probably do some kind of blueprint validation in future // for now, let's just 500 and bail out