From 52ba3793bb315b607954556269ec179d597c5b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deluan=20Quint=C3=A3o?= Date: Fri, 2 Dec 2022 10:36:25 -0500 Subject: [PATCH] feat: add `Registry.IsRegisteredWithRefs` (#220) --- registry/client.go | 19 ++++++++++++++++--- registry/client_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/registry/client.go b/registry/client.go index cb3689b7..2bb9037d 100644 --- a/registry/client.go +++ b/registry/client.go @@ -54,8 +54,11 @@ type Registry interface { // CreateSchema creates a schema in the registry, returning the schema id. CreateSchema(ctx context.Context, subject, schema string, references ...SchemaReference) (int, avro.Schema, error) - // IsRegistered determines of the schema is registered. + // IsRegistered determines if the schema is registered. IsRegistered(ctx context.Context, subject, schema string) (int, avro.Schema, error) + + // IsRegisteredWithRefs determines if the schema is registered, with optional referenced schemas. + IsRegisteredWithRefs(ctx context.Context, subject, schema string, refs ...SchemaReference) (int, avro.Schema, error) } type schemaPayload struct { @@ -276,11 +279,21 @@ func (c *Client) CreateSchema( return payload.ID, sch, err } -// IsRegistered determines of the schema is registered. +// IsRegistered determines if the schema is registered. func (c *Client) IsRegistered(ctx context.Context, subject, schema string) (int, avro.Schema, error) { + return c.IsRegisteredWithRefs(ctx, subject, schema) +} + +// IsRegisteredWithRefs determines if the schema is registered, with optional referenced schemas. +func (c *Client) IsRegisteredWithRefs( + ctx context.Context, + subject, schema string, + references ...SchemaReference, +) (int, avro.Schema, error) { var payload idPayload p := path.Join("subjects", subject) - err := c.request(ctx, http.MethodPost, p, schemaPayload{Schema: schema}, &payload) + inPayload := schemaPayload{Schema: schema, References: references} + err := c.request(ctx, http.MethodPost, p, inPayload, &payload) if err != nil { return 0, nil, err } diff --git a/registry/client_test.go b/registry/client_test.go index 593f5081..7d727266 100644 --- a/registry/client_test.go +++ b/registry/client_test.go @@ -2,6 +2,8 @@ package registry_test import ( "context" + "encoding/json" + "io" "net/http" "net/http/httptest" "testing" @@ -493,6 +495,43 @@ func TestClient_IsRegistered(t *testing.T) { assert.Equal(t, `["null","string","int"]`, schema.String()) } +func TestClient_IsRegisteredWithRefs(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "POST", r.Method) + assert.Equal(t, "/subjects/test", r.URL.Path) + body, _ := io.ReadAll(r.Body) + var decoded map[string]interface{} + _ = json.Unmarshal(body, &decoded) + assert.Contains(t, decoded, "references") + refs, ok := decoded["references"].([]interface{}) + assert.Equal(t, ok, true) + assert.Len(t, refs, 1) + ref, ok := refs[0].(map[string]interface{}) + assert.Equal(t, true, ok) + assert.Equal(t, "some_schema", ref["name"].(string)) + assert.Equal(t, "some_subject", ref["subject"].(string)) + assert.Equal(t, float64(3), ref["version"].(float64)) + _, _ = w.Write([]byte(`{"id":10}`)) + })) + defer s.Close() + client, _ := registry.NewClient(s.URL) + + id, schema, err := client.IsRegisteredWithRefs( + context.Background(), + "test", + "[\"null\",\"string\",\"int\"]", + registry.SchemaReference{ + Name: "some_schema", + Subject: "some_subject", + Version: 3, + }, + ) + + require.NoError(t, err) + assert.Equal(t, 10, id) + assert.Equal(t, `["null","string","int"]`, schema.String()) +} + func TestClient_IsRegisteredRequestError(t *testing.T) { s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(500)