-
Notifications
You must be signed in to change notification settings - Fork 25
Add UUID fields into spaces and subnets #139
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| module github.com/juju/description/v5 | ||
| module github.com/juju/description/v6 | ||
|
|
||
| go 1.21 | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ type spaces struct { | |
|
|
||
| type space struct { | ||
| Id_ string `yaml:"id"` | ||
| UUID_ string `yaml:"uuid"` | ||
| Name_ string `yaml:"name"` | ||
| Public_ bool `yaml:"public"` | ||
| ProviderID_ string `yaml:"provider-id,omitempty"` | ||
|
|
@@ -24,6 +25,7 @@ type space struct { | |
| // type that supports the Space interface. | ||
| type SpaceArgs struct { | ||
| Id string | ||
| UUID string | ||
| Name string | ||
| Public bool | ||
| ProviderID string | ||
|
|
@@ -32,6 +34,7 @@ type SpaceArgs struct { | |
| func newSpace(args SpaceArgs) *space { | ||
| return &space{ | ||
| Id_: args.Id, | ||
| UUID_: args.UUID, | ||
| Name_: args.Name, | ||
| Public_: args.Public, | ||
| ProviderID_: args.ProviderID, | ||
|
|
@@ -43,6 +46,11 @@ func (s *space) Id() string { | |
| return s.Id_ | ||
| } | ||
|
|
||
| // UUID implements Space. | ||
| func (s *space) UUID() string { | ||
| return s.UUID_ | ||
| } | ||
|
|
||
| // Name implements Space. | ||
| func (s *space) Name() string { | ||
| return s.Name_ | ||
|
|
@@ -67,22 +75,28 @@ func importSpaces(source map[string]interface{}) ([]*space, error) { | |
| valid := coerced.(map[string]interface{}) | ||
|
|
||
| version := int(valid["version"].(int64)) | ||
| importFunc, ok := spaceDeserializationFuncs[version] | ||
| getFields, ok := spaceFieldsFuncs[version] | ||
| if !ok { | ||
| return nil, errors.NotValidf("version %d", version) | ||
| } | ||
| sourceList := valid["spaces"].([]interface{}) | ||
| return importSpaceList(sourceList, importFunc) | ||
| return importSpaceList(sourceList, schema.FieldMap(getFields()), version) | ||
| } | ||
|
|
||
| func importSpaceList(sourceList []interface{}, importFunc spaceDeserializationFunc) ([]*space, error) { | ||
| func importSpaceList(sourceList []interface{}, checker schema.Checker, version int) ([]*space, error) { | ||
| result := make([]*space, 0, len(sourceList)) | ||
| for i, value := range sourceList { | ||
| source, ok := value.(map[string]interface{}) | ||
| if !ok { | ||
| return nil, errors.Errorf("unexpected value for space %d, %T", i, value) | ||
| } | ||
| space, err := importFunc(source) | ||
| coerced, err := checker.Coerce(source, nil) | ||
|
|
||
| if err != nil { | ||
| return nil, errors.Annotatef(err, "space %d v%d schema check failed", i, version) | ||
| } | ||
| valid := coerced.(map[string]interface{}) | ||
| space, err := newSpaceFromValid(valid, version) | ||
| if err != nil { | ||
| return nil, errors.Annotatef(err, "space %d", i) | ||
| } | ||
|
|
@@ -91,51 +105,26 @@ func importSpaceList(sourceList []interface{}, importFunc spaceDeserializationFu | |
| return result, nil | ||
| } | ||
|
|
||
| type spaceDeserializationFunc func(map[string]interface{}) (*space, error) | ||
|
|
||
| var spaceDeserializationFuncs = map[int]spaceDeserializationFunc{ | ||
| 1: importSpaceV1, | ||
| 2: importSpaceV2, | ||
| } | ||
|
|
||
| func importSpaceV1(source map[string]interface{}) (*space, error) { | ||
| fields, defaults := spaceV1Fields() | ||
| checker := schema.FieldMap(fields, defaults) | ||
|
|
||
| coerced, err := checker.Coerce(source, nil) | ||
| if err != nil { | ||
| return nil, errors.Annotatef(err, "space v1 schema check failed") | ||
| } | ||
| valid := coerced.(map[string]interface{}) | ||
| // From here we know that the map returned from the schema coercion | ||
| // contains fields of the right type. | ||
|
|
||
| return &space{ | ||
| func newSpaceFromValid(valid map[string]interface{}, version int) (*space, error) { | ||
| result := space{ | ||
| Name_: valid["name"].(string), | ||
| Public_: valid["public"].(bool), | ||
| ProviderID_: valid["provider-id"].(string), | ||
| }, nil | ||
| } | ||
|
|
||
| func importSpaceV2(source map[string]interface{}) (*space, error) { | ||
| fields, defaults := spaceV1Fields() | ||
| fields["id"] = schema.String() | ||
| checker := schema.FieldMap(fields, defaults) | ||
|
|
||
| coerced, err := checker.Coerce(source, nil) | ||
| if err != nil { | ||
| return nil, errors.Annotatef(err, "space v2 schema check failed") | ||
| } | ||
| valid := coerced.(map[string]interface{}) | ||
| // From here we know that the map returned from the schema coercion | ||
| // contains fields of the right type. | ||
| // id was added in V2 and removed in V3. | ||
| if version == 2 { | ||
| result.Id_ = valid["id"].(string) | ||
| } | ||
| if version >= 3 { | ||
| result.UUID_ = valid["uuid"].(string) | ||
| } | ||
| return &result, nil | ||
| } | ||
|
|
||
| return &space{ | ||
| Id_: valid["id"].(string), | ||
| Name_: valid["name"].(string), | ||
| Public_: valid["public"].(bool), | ||
| ProviderID_: valid["provider-id"].(string), | ||
| }, nil | ||
| var spaceFieldsFuncs = map[int]fieldsFunc{ | ||
| 1: spaceV1Fields, | ||
| 2: spaceV2Fields, | ||
| 3: spaceV3Fields, | ||
| } | ||
|
|
||
| func spaceV1Fields() (schema.Fields, schema.Defaults) { | ||
|
|
@@ -150,3 +139,18 @@ func spaceV1Fields() (schema.Fields, schema.Defaults) { | |
| } | ||
| return fields, defaults | ||
| } | ||
|
|
||
| func spaceV2Fields() (schema.Fields, schema.Defaults) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this method is added.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, added V3 and used this func |
||
| fields, defaults := spaceV1Fields() | ||
| fields["id"] = schema.String() | ||
|
|
||
| return fields, defaults | ||
| } | ||
|
|
||
| func spaceV3Fields() (schema.Fields, schema.Defaults) { | ||
| fields, defaults := spaceV2Fields() | ||
| fields["uuid"] = schema.String() | ||
| delete(fields, "id") | ||
|
|
||
| return fields, defaults | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the addition of uuid, can't we do away with Id now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
answered below