Skip to content

Unmarshal empty strings as uuid.Nil with Google's UUID library

License

Notifications You must be signed in to change notification settings

skagget77/uuidnil

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UUID Nil

Allow Google's UUID to be unmarshaled from empty and malformed JSON strings. Useful when working with JSON-based APIs not conforming to the UUID standard.

Example

Wrapping the user in a uuidnil wrapper allows it to be unmarshaled without any errors:

func main() {
    var user struct {
        ID   uuid.UUID
        Name string
    }

    data := []byte(`{"id": "", "name": "John"}`)
    if err := json.Unmarshal(data, uuidnil.Wrap(&user, uuidnil.AllowEmpty)); err != nil {
        log.Fatal(err)
    }

    fmt.Println(user)
}

About

Unmarshal empty strings as uuid.Nil with Google's UUID library

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages