Skip to content

Commit 52ba379

Browse files
authored
feat: add Registry.IsRegisteredWithRefs (#220)
1 parent c74aaf2 commit 52ba379

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

registry/client.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ type Registry interface {
5454
// CreateSchema creates a schema in the registry, returning the schema id.
5555
CreateSchema(ctx context.Context, subject, schema string, references ...SchemaReference) (int, avro.Schema, error)
5656

57-
// IsRegistered determines of the schema is registered.
57+
// IsRegistered determines if the schema is registered.
5858
IsRegistered(ctx context.Context, subject, schema string) (int, avro.Schema, error)
59+
60+
// IsRegisteredWithRefs determines if the schema is registered, with optional referenced schemas.
61+
IsRegisteredWithRefs(ctx context.Context, subject, schema string, refs ...SchemaReference) (int, avro.Schema, error)
5962
}
6063

6164
type schemaPayload struct {
@@ -276,11 +279,21 @@ func (c *Client) CreateSchema(
276279
return payload.ID, sch, err
277280
}
278281

279-
// IsRegistered determines of the schema is registered.
282+
// IsRegistered determines if the schema is registered.
280283
func (c *Client) IsRegistered(ctx context.Context, subject, schema string) (int, avro.Schema, error) {
284+
return c.IsRegisteredWithRefs(ctx, subject, schema)
285+
}
286+
287+
// IsRegisteredWithRefs determines if the schema is registered, with optional referenced schemas.
288+
func (c *Client) IsRegisteredWithRefs(
289+
ctx context.Context,
290+
subject, schema string,
291+
references ...SchemaReference,
292+
) (int, avro.Schema, error) {
281293
var payload idPayload
282294
p := path.Join("subjects", subject)
283-
err := c.request(ctx, http.MethodPost, p, schemaPayload{Schema: schema}, &payload)
295+
inPayload := schemaPayload{Schema: schema, References: references}
296+
err := c.request(ctx, http.MethodPost, p, inPayload, &payload)
284297
if err != nil {
285298
return 0, nil, err
286299
}

registry/client_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package registry_test
22

33
import (
44
"context"
5+
"encoding/json"
6+
"io"
57
"net/http"
68
"net/http/httptest"
79
"testing"
@@ -493,6 +495,43 @@ func TestClient_IsRegistered(t *testing.T) {
493495
assert.Equal(t, `["null","string","int"]`, schema.String())
494496
}
495497

498+
func TestClient_IsRegisteredWithRefs(t *testing.T) {
499+
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
500+
assert.Equal(t, "POST", r.Method)
501+
assert.Equal(t, "/subjects/test", r.URL.Path)
502+
body, _ := io.ReadAll(r.Body)
503+
var decoded map[string]interface{}
504+
_ = json.Unmarshal(body, &decoded)
505+
assert.Contains(t, decoded, "references")
506+
refs, ok := decoded["references"].([]interface{})
507+
assert.Equal(t, ok, true)
508+
assert.Len(t, refs, 1)
509+
ref, ok := refs[0].(map[string]interface{})
510+
assert.Equal(t, true, ok)
511+
assert.Equal(t, "some_schema", ref["name"].(string))
512+
assert.Equal(t, "some_subject", ref["subject"].(string))
513+
assert.Equal(t, float64(3), ref["version"].(float64))
514+
_, _ = w.Write([]byte(`{"id":10}`))
515+
}))
516+
defer s.Close()
517+
client, _ := registry.NewClient(s.URL)
518+
519+
id, schema, err := client.IsRegisteredWithRefs(
520+
context.Background(),
521+
"test",
522+
"[\"null\",\"string\",\"int\"]",
523+
registry.SchemaReference{
524+
Name: "some_schema",
525+
Subject: "some_subject",
526+
Version: 3,
527+
},
528+
)
529+
530+
require.NoError(t, err)
531+
assert.Equal(t, 10, id)
532+
assert.Equal(t, `["null","string","int"]`, schema.String())
533+
}
534+
496535
func TestClient_IsRegisteredRequestError(t *testing.T) {
497536
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
498537
w.WriteHeader(500)

0 commit comments

Comments
 (0)