From bd9e897344bde2ffce5e6715f61d45df3605dead Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Wed, 21 Jul 2021 16:48:37 +1000 Subject: [PATCH] chore: migrate to single ffi lib --- consumer/http.go | 2 +- consumer/interaction.go | 2 +- installer/installer.go | 26 ++-- internal/checker/checker.go | 7 +- .../{mockserver/mock_server_lib.go => lib.go} | 8 +- .../native/{mockserver => }/message_server.go | 42 +++--- .../{mockserver => }/message_server_test.go | 2 +- internal/native/{mockserver => }/mismatch.go | 2 +- .../native/{mockserver => }/mock_server.go | 129 ++++++++++-------- .../{mockserver => }/mock_server_test.go | 2 +- internal/native/{verifier => }/verifier.go | 47 ++----- internal/native/verifier/verifier_lib.go | 8 -- .../native/{verifier => }/verifier_test.go | 2 +- message/message.go | 2 +- message/message_v3.go | 2 +- provider/verify_request.go | 2 +- 16 files changed, 128 insertions(+), 157 deletions(-) rename internal/native/{mockserver/mock_server_lib.go => lib.go} (69%) rename internal/native/{mockserver => }/message_server.go (68%) rename internal/native/{mockserver => }/message_server_test.go (99%) rename internal/native/{mockserver => }/mismatch.go (98%) rename internal/native/{mockserver => }/mock_server.go (82%) rename internal/native/{mockserver => }/mock_server_test.go (99%) rename internal/native/{verifier => }/verifier.go (72%) delete mode 100644 internal/native/verifier/verifier_lib.go rename internal/native/{verifier => }/verifier_test.go (97%) diff --git a/consumer/http.go b/consumer/http.go index 226e8018c..8423814be 100644 --- a/consumer/http.go +++ b/consumer/http.go @@ -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" diff --git a/consumer/interaction.go b/consumer/interaction.go index e2f6f1243..201b0072a 100644 --- a/consumer/interaction.go +++ b/consumer/interaction.go @@ -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" diff --git a/installer/installer.go b/installer/installer.go index 1d5bc5438..998199c0a 100644 --- a/installer/installer.go +++ b/installer/installer.go @@ -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", }, } diff --git a/internal/checker/checker.go b/internal/checker/checker.go index fa4239a90..edd42b63c 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -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 { diff --git a/internal/native/mockserver/mock_server_lib.go b/internal/native/lib.go similarity index 69% rename from internal/native/mockserver/mock_server_lib.go rename to internal/native/lib.go index f29d4866b..f5fa80db9 100644 --- a/internal/native/mockserver/mock_server_lib.go +++ b/internal/native/lib.go @@ -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" diff --git a/internal/native/mockserver/message_server.go b/internal/native/message_server.go similarity index 68% rename from internal/native/mockserver/message_server.go rename to internal/native/message_server.go index b9fb0522d..f1535165f 100644 --- a/internal/native/mockserver/message_server.go +++ b/internal/native/message_server.go @@ -1,4 +1,4 @@ -package mockserver +package native /* // Library headers @@ -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" @@ -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 @@ -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 } @@ -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) @@ -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 } @@ -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) } @@ -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 } @@ -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 @@ -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. @@ -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 | /// |-------|-------------| diff --git a/internal/native/mockserver/message_server_test.go b/internal/native/message_server_test.go similarity index 99% rename from internal/native/mockserver/message_server_test.go rename to internal/native/message_server_test.go index fbb3b501b..113f18bbe 100644 --- a/internal/native/mockserver/message_server_test.go +++ b/internal/native/message_server_test.go @@ -1,4 +1,4 @@ -package mockserver +package native import ( "bytes" diff --git a/internal/native/mockserver/mismatch.go b/internal/native/mismatch.go similarity index 98% rename from internal/native/mockserver/mismatch.go rename to internal/native/mismatch.go index 80c675057..7d59bad38 100644 --- a/internal/native/mockserver/mismatch.go +++ b/internal/native/mismatch.go @@ -1,4 +1,4 @@ -package mockserver +package native // Request is the sub-struct of Mismatch type Request struct { diff --git a/internal/native/mockserver/mock_server.go b/internal/native/mock_server.go similarity index 82% rename from internal/native/mockserver/mock_server.go rename to internal/native/mock_server.go index 012d6af95..13114dbb1 100644 --- a/internal/native/mockserver/mock_server.go +++ b/internal/native/mock_server.go @@ -1,4 +1,4 @@ -package mockserver +package native /* // Library headers @@ -8,8 +8,8 @@ typedef int bool; #define true 1 #define false 0 -void init(char* log); -char* version(); +void pactffi_init(char* log); +char* pactffi_version(); /// Wraps a Pact model struct typedef struct InteractionHandle InteractionHandle; @@ -44,7 +44,7 @@ struct MessagePactHandle { /// **NOTE:** Although `close()` on the listener for the mock server is called, this does not /// currently work and the listener will continue handling requests. In this /// case, it will always return a 404 once the mock server has been cleaned up. -bool cleanup_mock_server(int mock_server_port); +bool pactffi_cleanup_mock_server(int mock_server_port); /// External interface to create a mock server. A pointer to the pact JSON as a C string is passed in, /// as well as the port for the mock server to run on. A value of 0 for the port will result in a @@ -62,30 +62,30 @@ bool cleanup_mock_server(int mock_server_port); /// | -4 | The method panicked | /// | -5 | The address is not valid | /// -int create_mock_server(const char *pact_str, const char *addr_str, bool tls); +int pactffi_create_mock_server(const char *pact_str, const char *addr_str, bool tls); /// As above, but creates it for a PactHandle -int create_mock_server_for_pact(PactHandle pact, const char *addr_str, bool tls); +int pactffi_create_mock_server_for_pact(PactHandle pact, const char *addr_str, bool tls); -void with_specification(PactHandle pact, int specification_version); +void pactffi_with_specification(PactHandle pact, int specification_version); /// Adds a provider state to the Interaction -void given(InteractionHandle interaction, const char *description); +void pactffi_given(InteractionHandle interaction, const char *description); /// Adds a provider state with params to the Interaction -void given_with_param(InteractionHandle interaction, const char *description, const char *name, const char *value); +void pactffi_given_with_param(InteractionHandle interaction, const char *description, const char *name, const char *value); /// Get self signed certificate for TLS mode -char* get_tls_ca_certificate(); +char* pactffi_get_tls_ca_certificate(); /// Free a string allocated on the Rust heap -void free_string(const char *s); +void pactffi_free_string(const char *s); /// External interface to check if a mock server has matched all its requests. The port number is /// passed in, and if all requests have been matched, true is returned. False is returned if there /// is no mock server on the given port, or if any request has not been successfully matched, or /// the method panics. -bool mock_server_matched(int mock_server_port); +bool pactffi_mock_server_matched(int mock_server_port); /// External interface to get all the mismatches from a mock server. The port number of the mock /// server is passed in, and a pointer to a C string with the mismatches in JSON format is @@ -100,38 +100,38 @@ bool mock_server_matched(int mock_server_port); /// If there is no mock server with the provided port number, or the function panics, a NULL /// pointer will be returned. Don't try to dereference it, it will not end well for you. /// -char* mock_server_mismatches(int mock_server_port); +char* pactffi_mock_server_mismatches(int mock_server_port); /// Creates a new Interaction and returns a handle to it -InteractionHandle new_interaction(PactHandle pact, const char *description); +InteractionHandle pactffi_new_interaction(PactHandle pact, const char *description); /// Creates a new Pact model and returns a handle to it -PactHandle new_pact(const char *consumer_name, const char *provider_name); +PactHandle pactffi_new_pact(const char *consumer_name, const char *provider_name); /// Sets the description for the Interaction -void upon_receiving(InteractionHandle interaction, const char *description); +void pactffi_upon_receiving(InteractionHandle interaction, const char *description); /// Sets the description for the Interaction -void with_request(InteractionHandle interaction, const char *method, const char *path); +void pactffi_with_request(InteractionHandle interaction, const char *method, const char *path); /// Sets header expectations /// https://docs.rs/pact_mock_server_ffi/0.0.7/pact_mock_server_ffi/fn.with_header.html -void with_header(InteractionHandle interaction, int interaction_part, const char *name, int index, const char *value); +void pactffi_with_header(InteractionHandle interaction, int interaction_part, const char *name, int index, const char *value); /// Sets query string expectation /// https://docs.rs/pact_mock_server_ffi/0.0.7/pact_mock_server_ffi/fn.with_query_parameter.html -void with_query_parameter(InteractionHandle interaction, const char *name, int index, const char *value); +void pactffi_with_query_parameter(InteractionHandle interaction, const char *name, int index, const char *value); /// Sets the description for the Interaction // https://docs.rs/pact_mock_server_ffi/0.0.7/pact_mock_server_ffi/fn.with_body.html -void with_body(InteractionHandle interaction, int interaction_part, const char *content_type, const char *body); +void pactffi_with_body(InteractionHandle interaction, int interaction_part, const char *content_type, const char *body); -void with_binary_file(InteractionHandle interaction, int interaction_part, const char *content_type, const char *body, int size); +void pactffi_with_binary_file(InteractionHandle interaction, int interaction_part, const char *content_type, const char *body, int size); -int with_multipart_file(InteractionHandle interaction, int interaction_part, const char *content_type, const char *body, const char *part_name); +int pactffi_with_multipart_file(InteractionHandle interaction, int interaction_part, const char *content_type, const char *body, const char *part_name); // https://docs.rs/pact_mock_server_ffi/0.0.7/pact_mock_server_ffi/fn.response_status.html -void response_status(InteractionHandle interaction, int status); +void pactffi_response_status(InteractionHandle interaction, int status); /// External interface to trigger a mock server to write out its pact file. This function should /// be called if all the consumer tests have passed. The directory to write the file to is passed @@ -149,15 +149,16 @@ void response_status(InteractionHandle interaction, int status); /// | 1 | A general panic was caught | /// | 2 | The pact file was not able to be written | /// | 3 | A mock server with the provided port was not found | -int write_pact_file(int mock_server_port, const char *directory, bool overwrite); +int pactffi_write_pact_file(int mock_server_port, const char *directory, bool overwrite); -void with_pact_metadata(PactHandle pact, const char *namespace, const char *name, const char *value); +void pactffi_with_pact_metadata(PactHandle pact, const char *namespace, const char *name, const char *value); // Additional global logging functions -// int log_to_buffer(int level); -// int log_to_stdout(int level); -// int log_to_file(const char *file_name, int level_filter); -// char* fetch_memory_buffer(); +//void pactffi_log_message(const char *source, const char *log_level, const char *message); +//int pactffi_log_to_buffer(int level); +//int pactffi_log_to_stdout(int level); +//int pactffi_log_to_file(const char *file_name, int level_filter); +//char* pactffi_fetch_log_buffer(); */ import "C" @@ -217,7 +218,7 @@ type Interaction struct { // Version returns the current semver FFI interface version func Version() string { - v := C.version() + v := C.pactffi_version() return C.GoString(v) } @@ -228,7 +229,7 @@ func Init() { l := C.CString("LOG_LEVEL") defer free(l) - C.init(l) + C.pactffi_init(l) // Alternative log destinations // NOTE: only one can be applied @@ -250,7 +251,7 @@ func NewHTTPMockServer(consumer string, provider string) *MockServer { defer free(cConsumer) defer free(cProvider) - return &MockServer{pact: &Pact{handle: C.new_pact(cConsumer, cProvider)}} + return &MockServer{pact: &Pact{handle: C.pactffi_new_pact(cConsumer, cProvider)}} } // Version returns the current semver FFI interface version @@ -259,7 +260,7 @@ func (m *MockServer) Version() string { } func (m *MockServer) WithSpecificationVersion(version specificationVersion) { - C.with_specification(m.pact.handle, C.int(version)) + C.pactffi_with_specification(m.pact.handle, C.int(version)) } // CreateMockServer creates a new Mock Server from a given Pact file. @@ -275,7 +276,7 @@ func (m *MockServer) CreateMockServer(pact string, address string, tls bool) (in tlsEnabled = 1 } - p := C.create_mock_server(cPact, cAddress, C.int(tlsEnabled)) + p := C.pactffi_create_mock_server(cPact, cAddress, C.int(tlsEnabled)) // | Error | Description | // |-------|-------------| @@ -323,7 +324,7 @@ func (m *MockServer) MockServerMismatchedRequests(port int) []MismatchedRequest log.Println("[DEBUG] mock server determining mismatches:", port) var res []MismatchedRequest - mismatches := C.mock_server_mismatches(C.int(port)) + mismatches := C.pactffi_mock_server_mismatches(C.int(port)) // This method can return a nil pointer, in which case, it // should be considered a failure (or at least, an issue) // converting it to a string might also do nasty things here! @@ -343,7 +344,7 @@ func (m *MockServer) CleanupMockServer(port int) bool { return true } log.Println("[DEBUG] mock server cleaning up port:", port) - res := C.cleanup_mock_server(C.int(port)) + res := C.pactffi_cleanup_mock_server(C.int(port)) return int(res) == 1 } @@ -354,7 +355,7 @@ func (m *MockServer) WritePactFile(port int, dir string) error { cDir := C.CString(dir) defer free(cDir) - res := int(C.write_pact_file(C.int(port), cDir, C.int(0))) + res := int(C.pactffi_write_pact_file(C.int(port), cDir, C.int(0))) // | Error | Description | // |-------|-------------| @@ -378,7 +379,7 @@ func (m *MockServer) WritePactFile(port int, dir string) error { // GetTLSConfig returns a tls.Config compatible with the TLS // mock server func GetTLSConfig() *tls.Config { - cert := C.get_tls_ca_certificate() + cert := C.pactffi_get_tls_ca_certificate() defer libRustFree(cert) goCert := C.GoString(cert) @@ -395,7 +396,7 @@ func free(str *C.char) { } func libRustFree(str *C.char) { - C.free_string(str) + C.pactffi_free_string(str) } // Start starts up the mock HTTP server on the given address:port and TLS config @@ -413,7 +414,7 @@ func (m *MockServer) Start(address string, tls bool) (int, error) { tlsEnabled = 1 } - p := C.create_mock_server_for_pact(m.pact.handle, cAddress, C.int(tlsEnabled)) + p := C.pactffi_create_mock_server_for_pact(m.pact.handle, cAddress, C.int(tlsEnabled)) // | Error | Description | // |-------|-------------| @@ -455,7 +456,7 @@ func (m *MockServer) WithMetadata(namespace, k, v string) *MockServer { cValue := C.CString(v) defer free(cValue) - C.with_pact_metadata(m.pact.handle, cNamespace, cName, cValue) + C.pactffi_with_pact_metadata(m.pact.handle, cNamespace, cName, cValue) return m } @@ -466,7 +467,7 @@ func (m *MockServer) NewInteraction(description string) *Interaction { defer free(cDescription) i := &Interaction{ - handle: C.new_interaction(m.pact.handle, cDescription), + handle: C.pactffi_new_interaction(m.pact.handle, cDescription), } m.interactions = append(m.interactions, i) @@ -477,7 +478,7 @@ func (i *Interaction) UponReceiving(description string) *Interaction { cDescription := C.CString(description) defer free(cDescription) - C.upon_receiving(i.handle, cDescription) + C.pactffi_upon_receiving(i.handle, cDescription) return i } @@ -486,7 +487,7 @@ func (i *Interaction) Given(state string) *Interaction { cState := C.CString(state) defer free(cState) - C.given(i.handle, cState) + C.pactffi_given(i.handle, cState) return i } @@ -502,7 +503,7 @@ func (i *Interaction) GivenWithParameter(state string, params map[string]interfa cValue := C.CString(param) defer free(cValue) - C.given_with_param(i.handle, cState, cKey, cValue) + C.pactffi_given_with_param(i.handle, cState, cKey, cValue) } @@ -517,7 +518,7 @@ func (i *Interaction) WithRequest(method string, pathOrMatcher interface{}) *Int cPath := C.CString(path) defer free(cPath) - C.with_request(i.handle, cMethod, cPath) + C.pactffi_with_request(i.handle, cMethod, cPath) return i } @@ -541,7 +542,7 @@ func (i *Interaction) withHeaders(part interactionType, valueOrMatcher map[strin cValue := C.CString(value) defer free(cValue) - C.with_header(i.handle, C.int(part), cName, C.int(0), cValue) + C.pactffi_with_header(i.handle, C.int(part), cName, C.int(0), cValue) } } @@ -560,7 +561,7 @@ func (i *Interaction) WithQuery(valueOrMatcher map[string][]interface{}) *Intera cValue := C.CString(value) defer free(cValue) - C.with_query_parameter(i.handle, cName, C.int(idx), cValue) + C.pactffi_with_query_parameter(i.handle, cName, C.int(idx), cValue) } } @@ -583,7 +584,7 @@ func (i *Interaction) withJSONBody(body interface{}, part interactionType) *Inte cBody := C.CString(jsonBody) defer free(cBody) - C.with_body(i.handle, C.int(part), cHeader, cBody) + C.pactffi_with_body(i.handle, C.int(part), cHeader, cBody) return i } @@ -603,7 +604,7 @@ func (i *Interaction) withBody(contentType string, body []byte, part interaction cBody := C.CString(string(body)) defer free(cBody) - C.with_body(i.handle, C.int(part), cHeader, cBody) + C.pactffi_with_body(i.handle, C.int(part), cHeader, cBody) return i } @@ -612,7 +613,7 @@ func (i *Interaction) withBinaryBody(contentType string, body []byte, part inter cHeader := C.CString(contentType) defer free(cHeader) - C.with_binary_file(i.handle, C.int(part), cHeader, (*C.char)(unsafe.Pointer(&body[0])), C.int(len(body))) + C.pactffi_with_binary_file(i.handle, C.int(part), cHeader, (*C.char)(unsafe.Pointer(&body[0])), C.int(len(body))) return i } @@ -643,14 +644,14 @@ func (i *Interaction) withMultipartFile(contentType string, filename string, mim cFilename := C.CString(filename) defer free(cFilename) - C.with_multipart_file(i.handle, C.int(part), cHeader, cFilename, cPartName) + C.pactffi_with_multipart_file(i.handle, C.int(part), cHeader, cFilename, cPartName) return i } // Set the expected HTTTP response status func (i *Interaction) WithStatus(status int) *Interaction { - C.response_status(i.handle, C.int(status)) + C.pactffi_response_status(i.handle, C.int(status)) return i } @@ -673,15 +674,29 @@ func stringFromInterface(obj interface{}) string { } // Experimental logging options +// func LogMessage(pkg, level, message string) { +// cPkg := C.CString(pkg) +// defer free(cPkg) + +// cLevel := C.CString(level) +// defer free(cLevel) + +// cMessage := C.CString(message) +// defer free(cMessage) + +// res := C.pactffi_log_message(cPkg, cLevel, cMessage) +// log.Println("[DEBUG] log_to_buffer res", res) +// } + // func logToBuffer(level logLevel) error { -// res := C.log_to_buffer(C.int(level)) +// res := C.pactffi_log_to_buffer(C.int(level)) // log.Println("[DEBUG] log_to_buffer res", res) // return logResultToError(int(res)) // } // func logToStdout(level logLevel) error { -// res := C.log_to_stdout(C.int(level)) +// res := C.pactffi_log_to_stdout(C.int(level)) // log.Println("[DEBUG] log_to_stdout res", res) // return logResultToError(int(res)) @@ -691,14 +706,14 @@ func stringFromInterface(obj interface{}) string { // cFile := C.CString(file) // defer free(cFile) -// res := C.log_to_file(cFile, C.int(level)) +// res := C.pactffi_log_to_file(cFile, C.int(level)) // log.Println("[DEBUG] log_to_file res", res) // return logResultToError(int(res)) // } // func getLogBuffer() string { -// buf := C.fetch_memory_buffer() +// buf := C.pactffi_fetch_log_buffer() // defer free(buf) // return C.GoString(buf) diff --git a/internal/native/mockserver/mock_server_test.go b/internal/native/mock_server_test.go similarity index 99% rename from internal/native/mockserver/mock_server_test.go rename to internal/native/mock_server_test.go index 0717a2564..831dacf1b 100644 --- a/internal/native/mockserver/mock_server_test.go +++ b/internal/native/mock_server_test.go @@ -1,4 +1,4 @@ -package mockserver +package native import ( "fmt" diff --git a/internal/native/verifier/verifier.go b/internal/native/verifier.go similarity index 72% rename from internal/native/verifier/verifier.go rename to internal/native/verifier.go index 56a761ac4..bcc4b4093 100644 --- a/internal/native/verifier/verifier.go +++ b/internal/native/verifier.go @@ -1,4 +1,4 @@ -package verifier +package native /* // Library headers @@ -7,10 +7,9 @@ typedef int bool; #define true 1 #define false 0 -void init(char* log); -char* version(); -void free_string(char* s); -int verify(char* s); +char* pactffi_version(); +void pactffi_free_string(char* s); +int pactffi_verify(char* s); */ import "C" @@ -18,36 +17,15 @@ import ( "fmt" "log" "strings" - "unsafe" ) type Verifier struct{} -// Version returns the current semver FFI interface version -func Version() string { - version := C.version() - - return C.GoString(version) -} - -func Init() { - log.Println("[DEBUG] initialising rust verifier interface") - logLevel := C.CString("LOG_LEVEL") - defer freeString(logLevel) - - C.init(logLevel) -} - -// Version returns the current semver FFI interface version -func (v *Verifier) Version() string { - return Version() -} - func (v *Verifier) Verify(args []string) error { log.Println("[DEBUG] executing verifier FFI with args", args) cargs := C.CString(strings.Join(args, "\n")) - defer freeString(cargs) - result := C.verify(cargs) + defer free(cargs) + result := C.pactffi_verify(cargs) /// | Error | Description | /// |-------|-------------| @@ -68,16 +46,9 @@ func (v *Verifier) Verify(args []string) error { } } -func freeString(str *C.char) { - C.free(unsafe.Pointer(str)) -} - -func free(p unsafe.Pointer) { - C.free(p) -} - -func libRustFree(str *C.char) { - C.free_string(str) +// Version returns the current semver FFI interface version +func (v *Verifier) Version() string { + return Version() } var ( diff --git a/internal/native/verifier/verifier_lib.go b/internal/native/verifier/verifier_lib.go deleted file mode 100644 index 9df6bcdcc..000000000 --- a/internal/native/verifier/verifier_lib.go +++ /dev/null @@ -1,8 +0,0 @@ -package verifier - -/* -#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_verifier_ffi -#cgo windows,amd64 LDFLAGS: -lpact_verifier_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_verifier_ffi -*/ -import "C" diff --git a/internal/native/verifier/verifier_test.go b/internal/native/verifier_test.go similarity index 97% rename from internal/native/verifier/verifier_test.go rename to internal/native/verifier_test.go index 68639dd5d..fff15e86d 100644 --- a/internal/native/verifier/verifier_test.go +++ b/internal/native/verifier_test.go @@ -1,4 +1,4 @@ -package verifier +package native import ( "fmt" diff --git a/message/message.go b/message/message.go index b1c0e98d4..f45d37c34 100644 --- a/message/message.go +++ b/message/message.go @@ -5,7 +5,7 @@ import ( "reflect" "testing" - "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" ) diff --git a/message/message_v3.go b/message/message_v3.go index 1f22ab27b..d16fda3ca 100644 --- a/message/message_v3.go +++ b/message/message_v3.go @@ -9,7 +9,7 @@ import ( "reflect" "testing" - "github.com/pact-foundation/pact-go/v2/internal/native/mockserver" + mockserver "github.com/pact-foundation/pact-go/v2/internal/native" ) type MessageConfig struct { diff --git a/provider/verify_request.go b/provider/verify_request.go index 9452d4186..9808344f9 100644 --- a/provider/verify_request.go +++ b/provider/verify_request.go @@ -10,7 +10,7 @@ import ( "strings" "time" - native "github.com/pact-foundation/pact-go/v2/internal/native/verifier" + native "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/proxy"