-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wrapper types for Text/Binary Marshaler and Stringer
- Loading branch information
1 parent
77e9ec0
commit ce8198f
Showing
10 changed files
with
147 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{{$doneTypes := dict }} | ||
{{- range $table := .Tables}} | ||
{{- $tAlias := $.Aliases.Table $table.Key}} | ||
{{range $column := $table.Columns -}} | ||
{{- $colTyp := $column.Type -}} | ||
{{- if hasKey $doneTypes $colTyp}}{{continue}}{{end -}} | ||
{{- $_ := set $doneTypes $colTyp nil -}} | ||
{{- $typInfo := index $.Types $column.Type -}} | ||
{{- if $typInfo.NoRandomizationTest}}{{continue}}{{end -}} | ||
{{- if isPrimitiveType $colTyp}}{{continue}}{{end -}} | ||
{{- if has $colTyp (list "bool" "[]byte" "time.Time")}}{{continue}}{{end -}} | ||
{{- $.Importer.ImportList $typInfo.Imports -}} | ||
{{$.Importer.Import "database/sql"}} | ||
{{$.Importer.Import "database/sql/driver"}} | ||
// Make sure the type {{$colTyp}} satisfies database/sql.Scanner | ||
var _ sql.Scanner = &{{$colTyp}}{} | ||
|
||
// Make sure the type {{$colTyp}} satisfies database/sql/driver.Valuer | ||
var _ driver.Valuer = {{$colTyp}}{} | ||
|
||
{{end -}} | ||
{{- end}} |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package types | ||
|
||
import ( | ||
"database/sql/driver" | ||
"encoding" | ||
"fmt" | ||
) | ||
|
||
type Text[T interface { | ||
encoding.TextMarshaler | ||
}, Tp interface { | ||
*T | ||
encoding.TextUnmarshaler | ||
}] struct { | ||
Val T | ||
} | ||
|
||
func (t Text[T, Tp]) Value() (driver.Value, error) { | ||
return t.Val.MarshalText() | ||
} | ||
|
||
func (t *Text[T, Tp]) Scan(value any) error { | ||
switch x := value.(type) { | ||
case string: | ||
v := Tp(&t.Val) | ||
return v.UnmarshalText([]byte(x)) | ||
case []byte: | ||
v := Tp(&t.Val) | ||
return v.UnmarshalText(x) | ||
case nil: | ||
return nil | ||
default: | ||
return fmt.Errorf("cannot scan type %T: %v", value, value) | ||
} | ||
} | ||
|
||
type Binary[T interface { | ||
encoding.BinaryMarshaler | ||
}, Tp interface { | ||
*T | ||
encoding.BinaryUnmarshaler | ||
}] struct { | ||
Val T | ||
} | ||
|
||
func (b Binary[T, Tp]) Value() (driver.Value, error) { | ||
return b.Val.MarshalBinary() | ||
} | ||
|
||
func (b *Binary[T, Tp]) Scan(value any) error { | ||
switch x := value.(type) { | ||
case string: | ||
v := Tp(&b.Val) | ||
return v.UnmarshalBinary([]byte(x)) | ||
case []byte: | ||
v := Tp(&b.Val) | ||
return v.UnmarshalBinary(x) | ||
case nil: | ||
return nil | ||
default: | ||
return fmt.Errorf("cannot scan type %T: %v", value, value) | ||
} | ||
} | ||
|
||
type Stringer[T interface { | ||
~[]byte | ~string | ||
fmt.Stringer | ||
}] struct { | ||
Val T | ||
} | ||
|
||
func (s Stringer[T]) Value() (driver.Value, error) { | ||
return []byte(s.Val), nil | ||
} | ||
|
||
func (s *Stringer[T]) Scan(value any) error { | ||
switch x := value.(type) { | ||
case string: | ||
s.Val = T(x) | ||
return nil | ||
case []byte: | ||
s.Val = T(x) | ||
return nil | ||
case nil: | ||
return nil | ||
default: | ||
return fmt.Errorf("cannot scan type %T: %v", value, value) | ||
} | ||
} |
This file contains 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