Skip to content

Commit 293b7c4

Browse files
authored
Merge pull request #849 from mihaitodor/add-create-schema--with-fixed-id
Add `CreateSchemaWithIDAndVersion()` to the sr package
2 parents 605b994 + 143aeab commit 293b7c4

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

Diff for: pkg/sr/api.go

+28-8
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ type (
9393
// a Kafka topic, and whether this is for a key or value. For example,
9494
// "foo-key" would be the subject for the foo topic for serializing the
9595
// key field of a record.
96-
Subject string `json:"subject"`
96+
Subject string `json:"subject,omitempty"`
9797

9898
// Version is the version of this subject.
99-
Version int `json:"version"`
99+
Version int `json:"version,omitempty"`
100100

101101
// ID is the globally unique ID of the schema.
102-
ID int `json:"id"`
102+
ID int `json:"id,omitempty"`
103103

104104
Schema
105105
}
@@ -352,18 +352,38 @@ func (cl *Client) Schemas(ctx context.Context, subject string) ([]SubjectSchema,
352352
//
353353
// This supports param [Normalize].
354354
func (cl *Client) CreateSchema(ctx context.Context, subject string, s Schema) (SubjectSchema, error) {
355+
return cl.CreateSchemaWithIDAndVersion(ctx, subject, s, -1, -1)
356+
}
357+
358+
// CreateSchemaWithIDAndVersion attempts to create a schema with a fixed ID and
359+
// version ID in the given subject. If the id is set to -1 or 0, this method is
360+
// equivalent to CreateSchema(). If the versionID is set to -1 or 0, it will be
361+
// omitted when creating the schema.
362+
//
363+
// This supports param [Normalize].
364+
func (cl *Client) CreateSchemaWithIDAndVersion(ctx context.Context, subject string, s Schema, id, versionID int) (SubjectSchema, error) {
355365
// POST /subjects/{subject}/versions => returns ID
356366
// Newer SR returns the full SubjectSchema, but old does not, so we
357367
// re-request to find the full information.
358368
path := pathSubjectWithVersion(subject)
359-
var id struct {
369+
var into struct {
360370
ID int `json:"id"`
361371
}
362-
if err := cl.post(ctx, path, s, &id); err != nil {
363-
return SubjectSchema{}, err
372+
if id == -1 {
373+
if err := cl.post(ctx, path, s, &into); err != nil {
374+
return SubjectSchema{}, err
375+
}
376+
} else {
377+
ss := SubjectSchema{Schema: s, ID: id}
378+
if versionID != -1 {
379+
ss.Version = versionID
380+
}
381+
if err := cl.post(ctx, path, ss, &into); err != nil {
382+
return SubjectSchema{}, err
383+
}
364384
}
365385

366-
usages, err := cl.SchemaUsagesByID(ctx, id.ID)
386+
usages, err := cl.SchemaUsagesByID(ctx, into.ID)
367387
if err != nil {
368388
return SubjectSchema{}, err
369389
}
@@ -372,7 +392,7 @@ func (cl *Client) CreateSchema(ctx context.Context, subject string, s Schema) (S
372392
return usage, nil
373393
}
374394
}
375-
return SubjectSchema{}, fmt.Errorf("created schema under id %d, but unable to find SubjectSchema", id.ID)
395+
return SubjectSchema{}, fmt.Errorf("created schema under id %d, but unable to find SubjectSchema", into.ID)
376396
}
377397

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

0 commit comments

Comments
 (0)