Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions internal/anysdk/http_armoury.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,23 @@ type HTTPArmoury interface {
SetRequestParams([]HTTPArmouryParameters)
SetRequestSchema(Schema)
SetResponseSchema(Schema)
MergeLateBindingMaps(map[int]map[string]any) (HTTPArmoury, error)
}

type standardHTTPArmoury struct {
RequestParams []HTTPArmouryParameters
RequestSchema Schema
ResponseSchema Schema
RequestParams []HTTPArmouryParameters
RequestSchema Schema
ResponseSchema Schema
parentPreparator HTTPPreparator
prepcfg HTTPPreparatorConfig // memory of how it was prepared
}

func (ih *standardHTTPArmoury) MergeLateBindingMaps(m map[int]map[string]any) (HTTPArmoury, error) {
clonedParent, err := ih.parentPreparator.MergeParams(m)
if err != nil {
return nil, err
}
return clonedParent.BuildHTTPRequestCtx(ih.prepcfg)
}

func (ih *standardHTTPArmoury) GetRequestParams() []HTTPArmouryParameters {
Expand Down Expand Up @@ -48,6 +59,9 @@ func (ih *standardHTTPArmoury) GetResponseSchema() Schema {
return ih.ResponseSchema
}

func NewHTTPArmoury() HTTPArmoury {
return &standardHTTPArmoury{}
func NewHTTPArmoury(parentPreparator HTTPPreparator, prepCfg HTTPPreparatorConfig) HTTPArmoury {
return &standardHTTPArmoury{
parentPreparator: parentPreparator,
prepcfg: prepCfg,
}
}
65 changes: 51 additions & 14 deletions internal/anysdk/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ func NewHTTPPreparatorConfig(isFromAnnotation bool) HTTPPreparatorConfig {

type HTTPPreparator interface {
BuildHTTPRequestCtx(HTTPPreparatorConfig) (HTTPArmoury, error)
MergeParams(map[int]map[string]any) (HTTPPreparator, error)
}

type standardHTTPPreparator struct {
prov Provider
m OperationStore
svc Service
paramMap map[int]map[string]interface{}
paramMap map[int]map[string]any
execContext ExecContext
logger *logrus.Logger
parameters streaming.MapStream
Expand All @@ -52,7 +53,7 @@ func NewHTTPPreparator(
prov Provider,
svc Service,
m OperationStore,
paramMap map[int]map[string]interface{},
paramMap map[int]map[string]any,
parameters streaming.MapStream,
execContext ExecContext,
logger *logrus.Logger,
Expand All @@ -64,7 +65,7 @@ func newHTTPPreparator(
prov Provider,
svc Service,
m OperationStore,
paramMap map[int]map[string]interface{},
paramMap map[int]map[string]any,
parameters streaming.MapStream,
execContext ExecContext,
logger *logrus.Logger,
Expand All @@ -84,17 +85,53 @@ func newHTTPPreparator(
}
}

func (pr *standardHTTPPreparator) clone() *standardHTTPPreparator {
newParamMap := make(map[int]map[string]any)
for k, v := range pr.paramMap {
cloneSubMap := make(map[string]any)
for subK, subV := range v {
cloneSubMap[subK] = subV
}
newParamMap[k] = cloneSubMap
}
return &standardHTTPPreparator{
prov: pr.prov,
m: pr.m,
svc: pr.svc,
paramMap: newParamMap,
parameters: pr.parameters,
execContext: pr.execContext,
logger: pr.logger,
}
}

func (pr *standardHTTPPreparator) MergeParams(maps map[int]map[string]any) (HTTPPreparator, error) {
rv := pr.clone()
if len(maps) > 1 {
return nil, fmt.Errorf("multiple maps provided for merging, cartesian product not yet supported")
}
for k, v := range maps {
if _, exists := rv.paramMap[k]; !exists {
rv.paramMap[k] = make(map[string]any)
}
for subK, subV := range v {
rv.paramMap[k][subK] = subV
}
}
return rv, nil
}

//nolint:funlen,gocognit // TODO: review
func (pr *standardHTTPPreparator) BuildHTTPRequestCtx(cfg HTTPPreparatorConfig) (HTTPArmoury, error) {
if cfg.IsFromAnnotation() {
return pr.buildHTTPRequestCtxFromAnnotation()
return pr.buildHTTPRequestCtxFromAnnotation(cfg)
}
method, methodOk := pr.m.(StandardOperationStore)
if !methodOk {
return nil, fmt.Errorf("operation store is not a standard operation store")
}
var err error
httpArmoury := NewHTTPArmoury()
httpArmoury := NewHTTPArmoury(pr, cfg)
var requestSchema, responseSchema Schema
req, reqExists := pr.m.GetRequest()
if reqExists && req.GetSchema() != nil {
Expand Down Expand Up @@ -133,7 +170,7 @@ func (pr *standardHTTPPreparator) BuildHTTPRequestCtx(cfg HTTPPreparatorConfig)
params.SetRequestBody(bm)
}
} else if params.GetRequestBody() != nil && len(params.GetRequestBody()) != 0 {
m := make(map[string]interface{})
m := make(map[string]any)
baseRequestBytes := method.getBaseRequestBodyBytes()
if len(baseRequestBytes) > 0 {
mapErr := json.Unmarshal(baseRequestBytes, &m)
Expand Down Expand Up @@ -175,7 +212,7 @@ func (pr *standardHTTPPreparator) BuildHTTPRequestCtx(cfg HTTPPreparatorConfig)
if len(p.GetParameters().GetRequestBody()) == 0 && len(method.getDefaultRequestBodyBytes()) == 0 {
p.SetRequestBodyMap(nil)
} else if len(method.getDefaultRequestBodyBytes()) > 0 && len(p.GetParameters().GetRequestBody()) == 0 {
bm := make(map[string]interface{})
bm := make(map[string]any)
// TODO: support types other than json
err := json.Unmarshal(method.getDefaultRequestBodyBytes(), &bm)
if err == nil {
Expand Down Expand Up @@ -211,8 +248,8 @@ func (pr *standardHTTPPreparator) BuildHTTPRequestCtx(cfg HTTPPreparatorConfig)
func awsContextHousekeeping(
ctx context.Context,
method OperationStore,
contextParams map[string]interface{},
parameters map[string]interface{},
contextParams map[string]any,
parameters map[string]any,
) context.Context {
svcName := method.getServiceNameForProvider()
ctx = context.WithValue(ctx, "service", svcName) //nolint:revive,staticcheck // TODO: add custom context type
Expand Down Expand Up @@ -253,9 +290,9 @@ func getRequest(
}

//nolint:funlen,gocognit // acceptable
func (pr *standardHTTPPreparator) buildHTTPRequestCtxFromAnnotation() (HTTPArmoury, error) {
func (pr *standardHTTPPreparator) buildHTTPRequestCtxFromAnnotation(cfg HTTPPreparatorConfig) (HTTPArmoury, error) {
var err error
httpArmoury := NewHTTPArmoury()
httpArmoury := NewHTTPArmoury(pr, cfg)
var requestSchema, responseSchema Schema
httpMethod, httpMethodOk := pr.m.(StandardOperationStore)
if !httpMethodOk {
Expand All @@ -272,7 +309,7 @@ func (pr *standardHTTPPreparator) buildHTTPRequestCtxFromAnnotation() (HTTPArmou
httpArmoury.SetRequestSchema(requestSchema)
httpArmoury.SetResponseSchema(responseSchema)

paramMap := make(map[int]map[string]interface{})
paramMap := make(map[int]map[string]any)
i := 0
for {
out, oErr := pr.parameters.Read()
Expand Down Expand Up @@ -306,7 +343,7 @@ func (pr *standardHTTPPreparator) buildHTTPRequestCtxFromAnnotation() (HTTPArmou
}
params.SetRequestBody(pr.execContext.GetExecPayload().GetPayloadMap())
} else if params.GetRequestBody() != nil && len(params.GetRequestBody()) != 0 {
m := make(map[string]interface{})
m := make(map[string]any)
baseRequestBytes := httpMethod.getBaseRequestBodyBytes()
if len(baseRequestBytes) > 0 {
mapErr := json.Unmarshal(baseRequestBytes, &m)
Expand Down Expand Up @@ -348,7 +385,7 @@ func (pr *standardHTTPPreparator) buildHTTPRequestCtxFromAnnotation() (HTTPArmou
if len(p.GetParameters().GetRequestBody()) == 0 && len(httpMethod.getDefaultRequestBodyBytes()) == 0 {
p.SetRequestBodyMap(nil)
} else if len(httpMethod.getDefaultRequestBodyBytes()) > 0 {
bm := make(map[string]interface{})
bm := make(map[string]any)
// TODO: support types other than json
err := json.Unmarshal(httpMethod.getDefaultRequestBodyBytes(), &bm)
if err == nil {
Expand Down
2 changes: 2 additions & 0 deletions public/formulation/interfaces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions public/formulation/wrappers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading