From cc27c8333bf11119586c99920e086eec8b830e40 Mon Sep 17 00:00:00 2001 From: Bhupesh-V Date: Thu, 21 Dec 2023 20:11:46 +0530 Subject: [PATCH] nope --- ...oogle-uuid-parse-side-effect-uuid-array.md | 82 ------------------- README.md | 3 +- SUMMARY.md | 1 - count.json | 2 +- 4 files changed, 2 insertions(+), 86 deletions(-) delete mode 100644 Go/google-uuid-parse-side-effect-uuid-array.md diff --git a/Go/google-uuid-parse-side-effect-uuid-array.md b/Go/google-uuid-parse-side-effect-uuid-array.md deleted file mode 100644 index a604fd0..0000000 --- a/Go/google-uuid-parse-side-effect-uuid-array.md +++ /dev/null @@ -1,82 +0,0 @@ -# Google's Golang UUID package has a nice side effect for handling UUID Arrays on PostgreSQL -**_Posted on 21 Dec, 2023_** - -Ever seen this error while scanning a Postgres column containing an array of UUIDs when using an ORM with Go? - -``` -sql: Scan error on column index 15, name \"column_uuids\": unsupported Scan, storing driver.Value type string into type *[]uuid.UUID -``` - -If you work with Golang, you will know that while reading a list of UUIDs from a Postgres column, you might have to implement a custom scanner to handle the conversion to `[]uuid.UUID` type. - -Here's a sample implementation of the scanner interface for the `[]uuid.UUID` type. - -```go -package uuid - -import ( - "fmt" - "strings" - - "github.com/google/uuid" -) - -type UUIDSlice []uuid.UUID - -// Implementing the Scanner interface for the UUIDSlice type -func (u *UUIDSlice) Scan(value interface{}) error { - if value == nil { - *u = make(UUIDSlice, 0) - return nil - } - strValue, ok := value.(string) - if !ok { - return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type UUIDSlice", value) - } - - if strValue == "{}" { - // Check for an empty UUID array representation in the database - *u = make(UUIDSlice, 0) - return nil - } - - uuidStrings := strings.Split(strValue, ",") // Assuming UUIDs are stored as a comma-separated string - result := make(UUIDArray, len(uuidStrings)) - - for i, uuidStr := range uuidStrings { - parsedUUID, err := uuid.Parse(uuidStr) - if err != nil { - return err - } - result[i] = parsedUUID - } - - *u = result - return nil -} -``` - -> It would be nice if it was available on pq just like [`pq.StringArray`](https://pkg.go.dev/github.com/lib/pq#StringArray) - -If you notice, this piece of code would look bugy at the first glance. - -```go - uuidStrings := strings.Split(strValue, ",") // Assuming UUIDs are stored as a comma-separated string - result := make(UUIDArray, len(uuidStrings)) - - for i, uuidStr := range uuidStrings { - parsedUUID, err := uuid.Parse(uuidStr) - if err != nil { - return err - } - ... - } -``` - -What happens if the `strValue` is `{5d80b766-7b0b-4638-8315-e1d58cd42996}`? I forgot to strip the curly braces from the string, right?. So the `len(uuidStrings)` will return `36 + 2` and ideally this code should fail while parsing the UUID on `uuid.Parse(uuidStr)`. But it doesn't. - -The reason is that `uuid.Parse()` method assumes it's a ["Microsoft GUID"](https://learn.microsoft.com/en-us/dynamicsax-2012/developer/guids#string-representations-of-a-guid) and parses it accordingly. So the above code will work even if the curly braces are not stripped from the string, and you will get a `uuid.UUID` type. - -See `uuid.Parse()` method's implementation [here](https://github.com/google/uuid/blob/master/uuid.go#L81-L83) - -So bottom line, its a side-effect we are assuming to be a feature. But it's a nice side-effect to have. Isn't it (or hell, maybe we will wait till it breaks)? diff --git a/README.md b/README.md index 5ba93cc..e231058 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ * [Distributed Computing](#distributed-computing) [**`2`**] * [Flutter](#flutter) [**`3`**] * [Git](#git) [**`10`**] -* [Go](#go) [**`16`**] +* [Go](#go) [**`15`**] * [Meta](#meta) [**`1`**] * [Miscellaneous](#miscellaneous) [**`13`**] * [Python](#python) [**`19`**] @@ -214,7 +214,6 @@
  • Concurrency Comparison: Go v/s Elixir
  • Convert `string` to `int` and vice-versa in Go
  • Creating Python's `next()` alternative using Go Closures -
  • Google's Golang UUID package has a nice side effect for handling UUID Arrays on PostgreSQL
  • Measure Exection time in Go
  • Memoization using Golang Generics
  • Parsing changelogs using regex with Go diff --git a/SUMMARY.md b/SUMMARY.md index 5e9db63..ff3aa95 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -126,7 +126,6 @@
  • Concurrency Comparison: Go v/s Elixir
  • Convert `string` to `int` and vice-versa in Go
  • Creating Python's `next()` alternative using Go Closures -
  • Google's Golang UUID package has a nice side effect for handling UUID Arrays on PostgreSQL
  • Measure Exection time in Go
  • Memoization using Golang Generics
  • Parsing changelogs using regex with Go diff --git a/count.json b/count.json index 3483086..915e62e 100644 --- a/count.json +++ b/count.json @@ -1,3 +1,3 @@ { - "count": 143 + "count": 142 } \ No newline at end of file