Skip to content

Commit

Permalink
feat: added int16 parser
Browse files Browse the repository at this point in the history
  • Loading branch information
alfarih31 committed Oct 26, 2022
1 parent dfa653e commit c993f6d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (i Int) ToInt() int {
return int(i)
}

func (i Int) ToInt16() int16 {
return int16(i)
}

func (i Int) ToInt32() int32 {
return int32(i)
}
Expand All @@ -35,6 +39,10 @@ func (i Int) ToIntPtr() *int {
return toIntPtr(int(i))
}

func (i Int) ToInt16Ptr() *int16 {
return toInt16Ptr(int16(i))
}

func (i Int) ToInt32Ptr() *int32 {
return toInt32Ptr(i.ToInt32())
}
Expand Down
31 changes: 31 additions & 0 deletions int_ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"fmt"
)

func toInt16Ptr(i int16) *int16 {
return &i
}

func toIntPtr(i int) *int {
return &i
}
Expand All @@ -21,6 +25,10 @@ type IntPtr struct {
i *int
}

type Int16Ptr struct {
i *int16
}

type Int32Ptr struct {
i *int32
}
Expand Down Expand Up @@ -97,3 +105,26 @@ func (p *Int64Ptr) Scan(value interface{}) error {

return nil
}

func (p Int16Ptr) Value() (driver.Value, error) {
return p.i, nil
}

func (p *Int16Ptr) Scan(value interface{}) error {
if value == nil {
p.i = nil
return nil
}

if bv, err := driver.Int32.ConvertValue(value); err != nil {
if v, ok := bv.(int16); ok {
*p = Int16Ptr{
i: toInt16Ptr(v),
}
}
} else {
return fmt.Errorf("failed to scan Int16Ptr:\n%v", err)
}

return nil
}

0 comments on commit c993f6d

Please # to comment.