Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema validation #557

Merged
merged 1 commit into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pkg/partmgr/partmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,21 @@ func (p *PartitionManager) ReadAndUpdateSchema() (err error) {
return
}

func unmarshalSchema(data []byte) (*config.Schema, error) {
schemaInstance := &config.Schema{}
err := json.Unmarshal(data, schemaInstance)
if err != nil {
return nil, err
}
if schemaInstance.TableSchemaInfo.PartitionerInterval == "" {
return nil, errors.New("Schema partition interval may not be empty")
}
if schemaInstance.TableSchemaInfo.ChunckerInterval == "" {
return nil, errors.New("Schema chunk interval may not be empty")
}
return schemaInstance, nil
}

func (p *PartitionManager) updatePartitionsFromSchema(schema *config.Schema) error {
if schema == nil {
schemaFilePath := p.GetSchemaFilePath()
Expand All @@ -275,8 +290,7 @@ func (p *PartitionManager) updatePartitionsFromSchema(schema *config.Schema) err
return errors.Wrapf(innerError, "Failed to read schema at path '%s'.", schemaFilePath)
}

schema = &config.Schema{}
innerError = json.Unmarshal(resp.Body(), schema)
schema, innerError = unmarshalSchema(resp.Body())
if innerError != nil {
return errors.Wrapf(innerError, "Failed to unmarshal schema at path '%s'.", schemaFilePath)
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/partmgr/partmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ func TestNewPartitionMngrBadInput(t *testing.T) {
assert.Error(t, err)
}

func TestUnmarshalSchemaError(t *testing.T) {
schemaString := []byte(`{}`)
_, err := unmarshalSchema(schemaString)
assert.Error(t, err)

schemaString = []byte(`{"tableSchemaInfo": {}}`)
_, err = unmarshalSchema(schemaString)
assert.Error(t, err)

schemaString = []byte(`{"tableSchemaInfo": {"PartitionerInterval": ""}}`)
_, err = unmarshalSchema(schemaString)
assert.Error(t, err)
}

func TestPartsForRange(tst *testing.T) {
numPartitions := 5
manager := getPartitionManager(tst)
Expand Down