Skip to content

Commit 40b16fc

Browse files
- Heavy refactor.
1 parent baa0dad commit 40b16fc

File tree

2 files changed

+384
-379
lines changed

2 files changed

+384
-379
lines changed

internal/stackql/primitivebuilder/mono_valent_execution.go

Lines changed: 138 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,6 @@ func newMonoValentExecutorFactory(
7979
}
8080
}
8181

82-
func (mv *monoValentExecution) GetRoot() primitivegraph.PrimitiveNode {
83-
return mv.root
84-
}
85-
86-
func (mv *monoValentExecution) GetTail() primitivegraph.PrimitiveNode {
87-
return mv.root
88-
}
89-
9082
type standardMethodElider struct {
9183
elisionFunc func(string, ...any) bool
9284
}
@@ -720,129 +712,160 @@ func agnosticate(
720712
reqParams := armoury.GetRequestParams()
721713
logging.GetLogger().Infof("monoValentExecution.Execute() req param count = %d", len(reqParams))
722714
for _, rc := range reqParams {
723-
reqCtx := rc
724-
paramsUsed, paramErr := reqCtx.ToFlatMap()
725-
if paramErr != nil {
726-
return paramErr
727-
}
728-
reqEncoding := reqCtx.Encode()
729-
elideOk := elider.IsElide(reqEncoding)
730-
if elideOk {
731-
return nil
732-
}
733-
// TODO: fix cloning ops
734-
cc := anysdk.NewAnySdkClientConfigurator(runtimeCtx, provider.GetName())
735-
response, apiErr := anysdk.CallFromSignature(
736-
cc,
715+
processErr := process(
716+
rc,
717+
elider,
718+
provider,
719+
method,
720+
tableName,
737721
runtimeCtx,
738722
authCtx,
739-
authCtx.Type,
740-
false,
741723
outErrFile,
742-
provider,
743-
anysdk.NewAnySdkOpStoreDesignation(method),
744-
reqCtx.GetArgList(),
724+
polyHandler,
725+
selectItemsKey,
726+
insertPreparator,
745727
)
746-
if response == nil {
747-
if apiErr != nil {
748-
return apiErr
749-
}
750-
return fmt.Errorf("unacceptable nil response from HTTP call")
728+
if processErr != nil {
729+
return processErr
751730
}
752-
httpResponse, httpResponseErr := response.GetHttpResponse()
753-
if httpResponse != nil && httpResponse.Body != nil {
754-
defer httpResponse.Body.Close()
731+
}
732+
return nil
733+
}
734+
735+
func process(
736+
armouryParams anysdk.HTTPArmouryParameters,
737+
elider methodElider,
738+
provider anysdk.Provider,
739+
method anysdk.OperationStore,
740+
tableName string,
741+
runtimeCtx dto.RuntimeCtx,
742+
authCtx *dto.AuthCtx,
743+
outErrFile io.Writer,
744+
polyHandler PolyHandler,
745+
selectItemsKey string,
746+
insertPreparator InsertPreparator,
747+
) error {
748+
reqCtx := armouryParams
749+
paramsUsed, paramErr := reqCtx.ToFlatMap()
750+
if paramErr != nil {
751+
return paramErr
752+
}
753+
reqEncoding := reqCtx.Encode()
754+
elideOk := elider.IsElide(reqEncoding)
755+
if elideOk {
756+
return nil
757+
}
758+
// TODO: fix cloning ops
759+
cc := anysdk.NewAnySdkClientConfigurator(runtimeCtx, provider.GetName())
760+
response, apiErr := anysdk.CallFromSignature(
761+
cc,
762+
runtimeCtx,
763+
authCtx,
764+
authCtx.Type,
765+
false,
766+
outErrFile,
767+
provider,
768+
anysdk.NewAnySdkOpStoreDesignation(method),
769+
reqCtx.GetArgList(),
770+
)
771+
if response == nil {
772+
if apiErr != nil {
773+
return apiErr
755774
}
756-
if httpResponse != nil && httpResponse.StatusCode >= 400 {
757-
continue
775+
return fmt.Errorf("unacceptable nil response from HTTP call")
776+
}
777+
httpResponse, httpResponseErr := response.GetHttpResponse()
778+
if httpResponse != nil && httpResponse.Body != nil {
779+
defer httpResponse.Body.Close()
780+
}
781+
if httpResponse != nil && httpResponse.StatusCode >= 400 {
782+
return nil
783+
}
784+
// TODO: refactor into package !!TECH_DEBT!!
785+
housekeepingDone := false
786+
nptRequest := inferNextPageRequestElement(provider, method)
787+
pageCount := 1
788+
for {
789+
if apiErr != nil {
790+
return apiErr
758791
}
759-
// TODO: refactor into package !!TECH_DEBT!!
760-
housekeepingDone := false
761-
nptRequest := inferNextPageRequestElement(provider, method)
762-
pageCount := 1
763-
for {
764-
if apiErr != nil {
765-
return apiErr
766-
}
767-
if httpResponseErr != nil {
768-
return httpResponseErr
769-
}
770-
processed, resErr := method.ProcessResponse(httpResponse)
771-
if resErr != nil {
772-
//nolint:errcheck // TODO: fix
773-
outErrFile.Write(
774-
[]byte(fmt.Sprintf("error processing response: %s\n", resErr.Error())),
775-
)
776-
if processed == nil {
777-
return resErr
778-
}
779-
}
780-
res, respOk := processed.GetResponse()
781-
if !respOk {
782-
return fmt.Errorf("response is not a valid response")
783-
}
784-
if res.HasError() {
785-
polyHandler.MessageHandler([]string{res.Error()})
786-
return nil
787-
}
788-
polyHandler.LogHTTPResponseMap(res.GetProcessedBody())
789-
logging.GetLogger().Infoln(fmt.Sprintf("monoValentExecution.Execute() response = %v", res))
790-
791-
itemisationResult := itemise(res.GetProcessedBody(), resErr, selectItemsKey)
792-
793-
if itemisationResult.IsNilPayload() {
794-
break
795-
}
796-
797-
insertPrepResult := insertPreparator.ActionInsertPreparation(
798-
newHTTPActionInsertPayload(
799-
itemisationResult,
800-
housekeepingDone,
801-
tableName,
802-
paramsUsed,
803-
reqEncoding,
804-
),
792+
if httpResponseErr != nil {
793+
return httpResponseErr
794+
}
795+
processed, resErr := method.ProcessResponse(httpResponse)
796+
if resErr != nil {
797+
//nolint:errcheck // TODO: fix
798+
outErrFile.Write(
799+
[]byte(fmt.Sprintf("error processing response: %s\n", resErr.Error())),
805800
)
806-
housekeepingDone = insertPrepResult.IsHousekeepingDone()
807-
insertPrepErr, hasInsertPrepErr := insertPrepResult.GetError()
808-
if hasInsertPrepErr {
809-
return insertPrepErr
801+
if processed == nil {
802+
return resErr
810803
}
804+
}
805+
res, respOk := processed.GetResponse()
806+
if !respOk {
807+
return fmt.Errorf("response is not a valid response")
808+
}
809+
if res.HasError() {
810+
polyHandler.MessageHandler([]string{res.Error()})
811+
return nil
812+
}
813+
polyHandler.LogHTTPResponseMap(res.GetProcessedBody())
814+
logging.GetLogger().Infoln(fmt.Sprintf("monoValentExecution.Execute() response = %v", res))
811815

812-
pageResult := page(
813-
res,
814-
method,
815-
provider,
816-
reqCtx,
817-
pageCount,
818-
runtimeCtx,
819-
authCtx,
820-
outErrFile,
821-
)
822-
httpResponse, httpResponseErr = pageResult.GetHTTPResponse()
823-
// if httpResponse != nil && httpResponse.Body != nil {
824-
// defer httpResponse.Body.Close()
825-
// }
826-
if httpResponseErr != nil {
827-
break
828-
// return internaldto.NewErroneousExecutorOutput(httpResponseErr)
829-
}
816+
itemisationResult := itemise(res.GetProcessedBody(), resErr, selectItemsKey)
830817

831-
if pageResult.IsFinished() {
832-
break
833-
}
818+
if itemisationResult.IsNilPayload() {
819+
break
820+
}
834821

835-
pageCount = pageResult.GetPageCount()
822+
insertPrepResult := insertPreparator.ActionInsertPreparation(
823+
newHTTPActionInsertPayload(
824+
itemisationResult,
825+
housekeepingDone,
826+
tableName,
827+
paramsUsed,
828+
reqEncoding,
829+
),
830+
)
831+
housekeepingDone = insertPrepResult.IsHousekeepingDone()
832+
insertPrepErr, hasInsertPrepErr := insertPrepResult.GetError()
833+
if hasInsertPrepErr {
834+
return insertPrepErr
835+
}
836836

837-
apiErr = pageResult.GetAPIError()
837+
pageResult := page(
838+
res,
839+
method,
840+
provider,
841+
reqCtx,
842+
pageCount,
843+
runtimeCtx,
844+
authCtx,
845+
outErrFile,
846+
)
847+
httpResponse, httpResponseErr = pageResult.GetHTTPResponse()
848+
// if httpResponse != nil && httpResponse.Body != nil {
849+
// defer httpResponse.Body.Close()
850+
// }
851+
if httpResponseErr != nil {
852+
return nil
853+
// return internaldto.NewErroneousExecutorOutput(httpResponseErr)
838854
}
839-
if reqCtx.GetRequest() != nil {
840-
q := reqCtx.GetRequest().URL.Query()
841-
q.Del(nptRequest.GetName())
842-
reqCtx.SetRawQuery(q.Encode())
855+
856+
if pageResult.IsFinished() {
857+
return nil
843858
}
859+
860+
pageCount = pageResult.GetPageCount()
861+
862+
apiErr = pageResult.GetAPIError()
863+
}
864+
if reqCtx.GetRequest() != nil {
865+
q := reqCtx.GetRequest().URL.Query()
866+
q.Del(nptRequest.GetName())
867+
reqCtx.SetRawQuery(q.Encode())
844868
}
845-
logging.GetLogger().Infof("monoValentExecution.Execute() returning empty for table %s", tableName)
846869
return nil
847870
}
848871

0 commit comments

Comments
 (0)