Skip to content

Commit

Permalink
fix: don't use defer in loop
Browse files Browse the repository at this point in the history
PingWriter committed Jul 12, 2024
1 parent a396cac commit ac7f9cf
Showing 2 changed files with 16 additions and 17 deletions.
16 changes: 7 additions & 9 deletions internal/native/message_server.go
Original file line number Diff line number Diff line change
@@ -121,20 +121,18 @@ func (m *Message) GivenWithParameter(state string, params map[string]interface{}
defer free(cState)

if len(params) == 0 {
cState := C.CString(state)
defer free(cState)

C.pactffi_given(m.handle, cState)
} else {
for k, v := range params {
cKey := C.CString(k)
defer free(cKey)

param := stringFromInterface(v)
cValue := C.CString(param)
defer free(cValue)

C.pactffi_given_with_param(m.handle, cState, cKey, cValue)

free(cValue)
free(cKey)
}
}

@@ -152,18 +150,18 @@ func (m *Message) ExpectsToReceive(description string) *Message {

func (m *Message) WithMetadata(valueOrMatcher map[string]string) *Message {
for k, v := range valueOrMatcher {

cName := C.CString(k)
defer free(cName)

// TODO: check if matching rules allowed here
// value := stringFromInterface(v)
// fmt.Printf("withheaders, sending: %+v \n\n", value)
// cValue := C.CString(value)
cValue := C.CString(v)
defer free(cValue)

C.pactffi_message_with_metadata(m.handle, cName, cValue)

free(cValue)
free(cName)
}

return m
@@ -237,7 +235,7 @@ func (m *MessageServer) UsingPlugin(pluginName string, pluginVersion string) err
defer free(cPluginName)
cPluginVersion := C.CString(pluginVersion)
defer free(cPluginVersion)

r := C.pactffi_using_plugin(m.messagePact.handle, cPluginName, cPluginVersion)
InstallSignalHandlers()

17 changes: 9 additions & 8 deletions internal/native/mock_server.go
Original file line number Diff line number Diff line change
@@ -500,13 +500,13 @@ func (i *Interaction) GivenWithParameter(state string, params map[string]interfa

for k, v := range params {
cKey := C.CString(k)
defer free(cKey)
param := stringFromInterface(v)
cValue := C.CString(param)
defer free(cValue)

C.pactffi_given_with_param(i.handle, cState, cKey, cValue)

free(cValue)
free(cKey)
}

return i
@@ -535,36 +535,37 @@ func (i *Interaction) WithResponseHeaders(valueOrMatcher map[string][]interface{

func (i *Interaction) withHeaders(part interactionPart, valueOrMatcher map[string][]interface{}) *Interaction {
for k, v := range valueOrMatcher {

cName := C.CString(k)
defer free(cName)

for _, header := range v {
value := stringFromInterface(header)
cValue := C.CString(value)
defer free(cValue)

C.pactffi_with_header_v2(i.handle, C.int(part), cName, C.ulong(0), cValue)

free(cValue)
}

free(cName)
}

return i
}

func (i *Interaction) WithQuery(valueOrMatcher map[string][]interface{}) *Interaction {
for k, values := range valueOrMatcher {

cName := C.CString(k)
defer free(cName)

for idx, v := range values {
value := stringFromInterface(v)
cValue := C.CString(value)
defer free(cValue)

C.pactffi_with_query_parameter_v2(i.handle, cName, C.ulong(idx), cValue)

free(cValue)
}

free(cName)
}

return i

0 comments on commit ac7f9cf

Please sign in to comment.