Skip to content

Commit

Permalink
chore: migrate to single ffi lib
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Jul 21, 2021
1 parent 9599373 commit bd9e897
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 157 deletions.
2 changes: 1 addition & 1 deletion consumer/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"unicode"
"unicode/utf8"

native "github.com/pact-foundation/pact-go/v2/internal/native/mockserver"
"github.com/pact-foundation/pact-go/v2/internal/native"
logging "github.com/pact-foundation/pact-go/v2/log"
"github.com/pact-foundation/pact-go/v2/models"
"github.com/pact-foundation/pact-go/v2/utils"
Expand Down
2 changes: 1 addition & 1 deletion consumer/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"strings"

"github.com/pact-foundation/pact-go/v2/internal/native/mockserver"
mockserver "github.com/pact-foundation/pact-go/v2/internal/native"
"github.com/pact-foundation/pact-go/v2/matchers"
"github.com/pact-foundation/pact-go/v2/models"
"github.com/pact-foundation/pact-go/v2/utils"
Expand Down
26 changes: 10 additions & 16 deletions installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,25 +266,19 @@ type packageInfo struct {
}

const (
VerifierPackage = "pact_verifier_ffi"
MockServerPackage = "libpact_mock_server_ffi"
downloadEnvVar = "PACT_GO_LIB_DOWNLOAD_PATH"
linux = "linux"
windows = "windows"
osx = "osx"
x86_64 = "x86_64"
FFIPackage = "libpact_ffi"
downloadEnvVar = "PACT_GO_LIB_DOWNLOAD_PATH"
linux = "linux"
windows = "windows"
osx = "osx"
x86_64 = "x86_64"
)

var packages = map[string]packageInfo{
VerifierPackage: {
libName: "libpact_verifier_ffi",
version: "0.0.5",
semverRange: ">= 0.0.2, < 1.0.0",
},
MockServerPackage: {
libName: "libpact_mock_server_ffi",
version: "0.0.17",
semverRange: ">= 0.0.15, < 1.0.0",
FFIPackage: {
libName: "libpact_ffi",
version: "0.0.0",
semverRange: ">= 0.0.0, < 1.0.0",
},
}

Expand Down
7 changes: 3 additions & 4 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package checker

import (
"github.com/pact-foundation/pact-go/v2/installer"
"github.com/pact-foundation/pact-go/v2/internal/native/mockserver"
"github.com/pact-foundation/pact-go/v2/internal/native/verifier"
"github.com/pact-foundation/pact-go/v2/internal/native"
)

func CheckInstall() error {
// initialised the lib registry
installer.LibRegistry[installer.MockServerPackage] = &mockserver.MockServer{}
installer.LibRegistry[installer.VerifierPackage] = &verifier.Verifier{}
installer.LibRegistry[installer.FFIPackage] = &native.MockServer{}
installer.LibRegistry[installer.FFIPackage] = &native.Verifier{}

i, err := installer.NewInstaller()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Package native contains the c bindings into the Pact Reference types.
package mockserver
package native

/*
#cgo darwin,amd64 LDFLAGS: -L/tmp -L/opt/pact/lib -L/usr/local/lib -Wl,-rpath -Wl,/opt/pact/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_mock_server_ffi
#cgo windows,amd64 LDFLAGS: -lpact_mock_server_ffi
#cgo linux,amd64 LDFLAGS: -L/tmp -L/opt/pact/lib -L/usr/local/lib -Wl,-rpath -Wl,/opt/pact/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_mock_server_ffi
#cgo darwin,amd64 LDFLAGS: -L/tmp -L/opt/pact/lib -L/usr/local/lib -Wl,-rpath -Wl,/opt/pact/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi
#cgo windows,amd64 LDFLAGS: -lpact_ffi
#cgo linux,amd64 LDFLAGS: -L/tmp -L/opt/pact/lib -L/usr/local/lib -Wl,-rpath -Wl,/opt/pact/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi
*/
import "C"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mockserver
package native

/*
// Library headers
Expand All @@ -20,16 +20,16 @@ struct MessagePactHandle {
uintptr_t pact;
};
MessagePactHandle new_message_pact(const char *consumer_name, const char *provider_name);
MessageHandle new_message(MessagePactHandle pact, const char *description);
void message_expects_to_receive(MessageHandle message, const char *description);
void message_given(MessageHandle message, const char *description);
void message_given_with_param(MessageHandle message, const char *description, const char *name, const char *value);
void message_with_contents(MessageHandle message, const char *content_type, const char *body, int size);
void message_with_metadata(MessageHandle message, const char *key, const char *value);
char* message_reify(MessageHandle message);
int write_message_pact_file(MessagePactHandle pact, const char *directory, bool overwrite);
void with_message_pact_metadata(MessagePactHandle pact, const char *namespace, const char *name, const char *value);
MessagePactHandle pactffi_new_message_pact(const char *consumer_name, const char *provider_name);
MessageHandle pactffi_new_message(MessagePactHandle pact, const char *description);
void pactffi_message_expects_to_receive(MessageHandle message, const char *description);
void pactffi_message_given(MessageHandle message, const char *description);
void pactffi_message_given_with_param(MessageHandle message, const char *description, const char *name, const char *value);
void pactffi_message_with_contents(MessageHandle message, const char *content_type, const char *body, int size);
void pactffi_message_with_metadata(MessageHandle message, const char *key, const char *value);
char* pactffi_message_reify(MessageHandle message);
int pactffi_write_message_pact_file(MessagePactHandle pact, const char *directory, bool overwrite);
void pactffi_with_message_pact_metadata(MessagePactHandle pact, const char *namespace, const char *name, const char *value);
*/
import "C"

Expand Down Expand Up @@ -60,7 +60,7 @@ func NewMessageServer(consumer string, provider string) *MessageServer {
defer free(cConsumer)
defer free(cProvider)

return &MessageServer{messagePact: &MessagePact{handle: C.new_message_pact(cConsumer, cProvider)}}
return &MessageServer{messagePact: &MessagePact{handle: C.pactffi_new_message_pact(cConsumer, cProvider)}}
}

// Sets the additional metadata on the Pact file. Common uses are to add the client library details such as the name and version
Expand All @@ -72,7 +72,7 @@ func (m *MessageServer) WithMetadata(namespace, k, v string) *MessageServer {
cValue := C.CString(v)
defer free(cValue)

C.with_message_pact_metadata(m.messagePact.handle, cNamespace, cName, cValue)
C.pactffi_with_message_pact_metadata(m.messagePact.handle, cNamespace, cName, cValue)

return m
}
Expand All @@ -83,7 +83,7 @@ func (m *MessageServer) NewMessage() *Message {
defer free(cDescription)

i := &Message{
handle: C.new_message(m.messagePact.handle, cDescription),
handle: C.pactffi_new_message(m.messagePact.handle, cDescription),
}
m.messages = append(m.messages, i)

Expand All @@ -94,7 +94,7 @@ func (i *Message) Given(state string) *Message {
cState := C.CString(state)
defer free(cState)

C.message_given(i.handle, cState)
C.pactffi_message_given(i.handle, cState)

return i
}
Expand All @@ -110,7 +110,7 @@ func (i *Message) GivenWithParameter(state string, params map[string]interface{}
cValue := C.CString(param)
defer free(cValue)

C.message_given_with_param(i.handle, cState, cKey, cValue)
C.pactffi_message_given_with_param(i.handle, cState, cKey, cValue)

}

Expand All @@ -121,7 +121,7 @@ func (i *Message) ExpectsToReceive(description string) *Message {
cDescription := C.CString(description)
defer free(cDescription)

C.message_expects_to_receive(i.handle, cDescription)
C.pactffi_message_expects_to_receive(i.handle, cDescription)

return i
}
Expand All @@ -139,7 +139,7 @@ func (i *Message) WithMetadata(valueOrMatcher map[string]string) *Message {
cValue := C.CString(v)
defer free(cValue)

C.message_with_metadata(i.handle, cName, cValue)
C.pactffi_message_with_metadata(i.handle, cName, cValue)
}

return i
Expand All @@ -163,13 +163,13 @@ func (i *Message) WithContents(contentType string, body []byte) *Message {

cBytes := C.CString(string(body))
defer free(cBytes)
C.message_with_contents(i.handle, cHeader, (*C.char)(unsafe.Pointer(&body[0])), C.int(len(body)))
C.pactffi_message_with_contents(i.handle, cHeader, (*C.char)(unsafe.Pointer(&body[0])), C.int(len(body)))

return i
}

func (i *Message) ReifyMessage() string {
return C.GoString(C.message_reify(i.handle))
return C.GoString(C.pactffi_message_reify(i.handle))
}

// WritePactFile writes the Pact to file.
Expand All @@ -183,7 +183,7 @@ func (m *MessageServer) WritePactFile(dir string, overwrite bool) error {
overwritePact = 1
}

res := int(C.write_message_pact_file(m.messagePact.handle, cDir, C.int(overwritePact)))
res := int(C.pactffi_write_message_pact_file(m.messagePact.handle, cDir, C.int(overwritePact)))

/// | Error | Description |
/// |-------|-------------|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mockserver
package native

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mockserver
package native

// Request is the sub-struct of Mismatch
type Request struct {
Expand Down
Loading

0 comments on commit bd9e897

Please sign in to comment.