Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support pointer references #312

Merged
merged 2 commits into from
Dec 16, 2024
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
2 changes: 2 additions & 0 deletions sdk-go/inferable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func TestGetSchema(t *testing.T) {
"TestFunc": {
"name": "TestFunc",
"input": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"a": {"type": "integer"},
Expand All @@ -210,6 +211,7 @@ func TestGetSchema(t *testing.T) {
"TestFunc2": {
"name": "TestFunc2",
"input": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"c": {
Expand Down
3 changes: 0 additions & 3 deletions sdk-go/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,9 @@ func TestInferableFunctions(t *testing.T) {
t.Fatalf("Error registering reverse function: %v", err)
}

jsonDef, err := inferableInstance.toJSONDefinition()
if err != nil {
t.Fatalf("Error generating JSON definition: %v", err)
}
t.Logf("JSON Definition:\n%s\n", string(jsonDef))

t.Run("Echo Function", func(t *testing.T) {
testInput := EchoInput{Input: "Hello, Inferable!"}
result, err := inferableInstance.callFunc("string_operations", "echo", testInput)
Expand Down
10 changes: 8 additions & 2 deletions sdk-go/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,18 @@ func (s *service) RegisterFunc(fn Function) (*FunctionReference, error) {
return nil, fmt.Errorf("function '%s' must have exactly one argument", fn.Name)
}
argType := fnType.In(0)

// Set the argument type to the referenced type
if argType.Kind() == reflect.Ptr {
argType = argType.Elem()
}

if argType.Kind() != reflect.Struct {
return nil, fmt.Errorf("function '%s' first argument must be a struct", fn.Name)
return nil, fmt.Errorf("function '%s' first argument must be a struct or a pointer to a struct", fn.Name)
}

// Get the schema for the input struct
reflector := jsonschema.Reflector{}
reflector := jsonschema.Reflector{DoNotReference: true, Anonymous: true}
schema := reflector.Reflect(reflect.New(argType).Interface())

if schema == nil {
Expand Down
43 changes: 0 additions & 43 deletions sdk-go/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,49 +133,6 @@ func TestRegistrationAndConfig(t *testing.T) {
require.NoError(t, err)
}

func TestErrorneousRegistration(t *testing.T) {
machineSecret, _, _, apiEndpoint := util.GetTestVars()

machineID := "random-machine-id"

// Create a new Inferable instance
i, err := New(InferableOptions{
APIEndpoint: apiEndpoint,
APISecret: machineSecret,
MachineID: machineID,
})
require.NoError(t, err)

// Register a service
service, err := i.RegisterService("TestService1")
require.NoError(t, err)

type F struct {
G int `json:"g"`
}

// Register a test function
type TestInput struct {
A int `json:"a"`
B int `json:"b"`
C []struct {
D int `json:"d"`
E string `json:"e"`
F []F `json:"f"`
} `json:"c"`
}

testFunc := func(input TestInput) int { return input.A + input.B }

_, err = service.RegisterFunc(Function{
Func: testFunc,
Name: "TestFunc",
Description: "Test function",
})

require.ErrorContains(t, err, "schema for function 'TestFunc' contains a $ref to an external definition. this is currently not supported.")
}

func TestServiceStartAndReceiveMessage(t *testing.T) {
machineSecret, consumeSecret, clusterId, apiEndpoint := util.GetTestVars()

Expand Down
Loading