Skip to content

Commit

Permalink
Add CreateSchemaWithID() to the sr package
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Todor <[email protected]>
  • Loading branch information
mihaitodor committed Oct 30, 2024
1 parent cea7aa5 commit af3fea9
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions pkg/sr/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,32 @@ func (cl *Client) Schemas(ctx context.Context, subject string) ([]SubjectSchema,
//
// This supports param [Normalize].
func (cl *Client) CreateSchema(ctx context.Context, subject string, s Schema) (SubjectSchema, error) {
return cl.CreateSchemaWithID(ctx, subject, s, -1)
}

// CreateSchemaWithID attempts to create a schema with a fixed ID in the given
// subject. If the id is set to -1, this method is equivalent to CreateSchema().
//
// This supports param [Normalize].
func (cl *Client) CreateSchemaWithID(ctx context.Context, subject string, s Schema, id int) (SubjectSchema, error) {
// POST /subjects/{subject}/versions => returns ID
// Newer SR returns the full SubjectSchema, but old does not, so we
// re-request to find the full information.
path := pathSubjectWithVersion(subject)
var id struct {
var into struct {
ID int `json:"id"`
}
if err := cl.post(ctx, path, s, &id); err != nil {
return SubjectSchema{}, err
if id == -1 {
if err := cl.post(ctx, path, s, &into); err != nil {
return SubjectSchema{}, err
}
} else {
if err := cl.post(ctx, path, SubjectSchema{Schema: s, ID: id}, &into); err != nil {
return SubjectSchema{}, err
}
}

usages, err := cl.SchemaUsagesByID(ctx, id.ID)
usages, err := cl.SchemaUsagesByID(ctx, into.ID)
if err != nil {
return SubjectSchema{}, err
}
Expand All @@ -372,7 +386,7 @@ func (cl *Client) CreateSchema(ctx context.Context, subject string, s Schema) (S
return usage, nil
}
}
return SubjectSchema{}, fmt.Errorf("created schema under id %d, but unable to find SubjectSchema", id.ID)
return SubjectSchema{}, fmt.Errorf("created schema under id %d, but unable to find SubjectSchema", into.ID)
}

// LookupSchema checks to see if a schema is already registered and if so,
Expand Down

0 comments on commit af3fea9

Please sign in to comment.