Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Allow unmarshaling unquoted Ignite UIDs from JSON #178

Merged
merged 2 commits into from
Jul 15, 2019
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
16 changes: 14 additions & 2 deletions api/meta.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
* [func (t *TypeMeta) GetTypeMeta() *TypeMeta](#TypeMeta.GetTypeMeta)
* [type UID](#UID)
* [func (u UID) String() string](#UID.String)
* [func (u *UID) UnmarshalJSON(b []byte) error](#UID.UnmarshalJSON)


#### <a name="pkg-files">Package files</a>
Expand Down Expand Up @@ -664,7 +665,7 @@ This is a helper for APIType generation



## <a name="UID">type</a> [UID](/pkg/apis/meta/v1alpha1/uid.go?s=74:89#L6)
## <a name="UID">type</a> [UID](/pkg/apis/meta/v1alpha1/uid.go?s=152:167#L11)
``` go
type UID string
```
Expand All @@ -679,7 +680,7 @@ UID represents an unique ID for a type



### <a name="UID.String">func</a> (UID) [String](/pkg/apis/meta/v1alpha1/uid.go?s=172:200#L11)
### <a name="UID.String">func</a> (UID) [String](/pkg/apis/meta/v1alpha1/uid.go?s=250:278#L16)
``` go
func (u UID) String() string
```
Expand All @@ -688,6 +689,17 @@ String returns the UID in string representation



### <a name="UID.UnmarshalJSON">func</a> (\*UID) [UnmarshalJSON](/pkg/apis/meta/v1alpha1/uid.go?s=444:487#L23)
``` go
func (u *UID) UnmarshalJSON(b []byte) error
```
This unmarshaler enables the UID to be passed in as an
unquoted string in JSON. Upon marshaling, quotes will
be automatically added.







Expand Down
29 changes: 28 additions & 1 deletion pkg/apis/meta/v1alpha1/uid.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package v1alpha1

import "fmt"
import (
"fmt"
"strconv"
"unicode/utf8"

"github.com/weaveworks/ignite/pkg/constants"
)

// UID represents an unique ID for a type
type UID string
Expand All @@ -11,3 +17,24 @@ var _ fmt.Stringer = UID("")
func (u UID) String() string {
return string(u)
}

// This unmarshaler enables the UID to be passed in as an
// unquoted string in JSON. Upon marshaling, quotes will
// be automatically added.
func (u *UID) UnmarshalJSON(b []byte) error {
if !utf8.Valid(b) {
return fmt.Errorf("invalid UID string: %s", b)
}

uid, err := strconv.Unquote(string(b))
if err != nil {
return err
}

if len(uid) < constants.IGNITE_UID_LENGTH {
return fmt.Errorf("UID string too short: %q", uid)
}

*u = UID(uid)
return nil
}
3 changes: 3 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ const (

// Log level for the firecracker VM
VM_LOG_LEVEL = "Error"

// How many characters Ignite UIDs should have
IGNITE_UID_LENGTH = 16
)
2 changes: 1 addition & 1 deletion pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func processUID(obj meta.Object, c *client.Client) error {
// No UID set, generate one
var uidBytes []byte
for {
uidBytes = make([]byte, 8)
uidBytes = make([]byte, constants.IGNITE_UID_LENGTH/2)
if _, err := rand.Read(uidBytes); err != nil {
return fmt.Errorf("failed to generate ID: %v", err)
}
Expand Down