Skip to content

Commit

Permalink
feat: add Registry.IsRegisteredWithRefs (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan authored Dec 2, 2022
1 parent c74aaf2 commit 52ba379
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
19 changes: 16 additions & 3 deletions registry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
39 changes: 39 additions & 0 deletions registry/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package registry_test

import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 52ba379

Please sign in to comment.