Skip to content

Commit

Permalink
remove conversion to binary
Browse files Browse the repository at this point in the history
  • Loading branch information
tee8z committed Dec 15, 2022
1 parent 0687f25 commit 6c3da86
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 42 deletions.
8 changes: 1 addition & 7 deletions uint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,7 @@ func (n Uint32) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
if !n.isValid {
return clause.Expr{SQL: "?", Vars: []interface{}{nil}}
}
value := strconv.FormatUint(uint64(n.realValue), 2)
leadingZero := 32 - len(value)
for leadingZero > 0 {
value = "0" + value
leadingZero--
}
return clause.Expr{SQL: "?", Vars: []interface{}{value}}
return clause.Expr{SQL: "?", Vars: []interface{}{n.realValue}}
}
return clause.Expr{}
}
Expand Down
12 changes: 0 additions & 12 deletions uint32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,14 @@ func TestScanUint32(t *testing.T) {
nullableInt.Scan(37)
tests.AssertEqual(t, nullableInt.Get(), 37)

// raw binary of uint8
nullableInt.Scan("00000000000000000000000000010001")
tests.AssertEqual(t, nullableInt.Get(), 17)

// uint16
nullableInt.Scan(1234)
tests.AssertEqual(t, nullableInt.Get(), 1234)

// raw binary of uint16
nullableInt.Scan("00000000000000000001000011100001")
tests.AssertEqual(t, nullableInt.Get(), 4321)

// uint32
nullableInt.Scan(654321)
tests.AssertEqual(t, nullableInt.Get(), 654321)

// raw binary of uint32
nullableInt.Scan("00000000000001011010101000100010")
tests.AssertEqual(t, nullableInt.Get(), 371234)

nullableInt.Scan(nil)
tests.AssertEqual(t, nullableInt.Get(), nil)
}
Expand Down
9 changes: 2 additions & 7 deletions uint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,8 @@ func (n Uint64) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
if !n.isValid {
return clause.Expr{SQL: "?", Vars: []interface{}{nil}}
}
value := strconv.FormatUint(n.realValue, 2)
leadingZero := 64 - len(value)
for leadingZero > 0 {
value = "0" + value
leadingZero--
}
return clause.Expr{SQL: "?", Vars: []interface{}{value}}

return clause.Expr{SQL: "?", Vars: []interface{}{n.realValue}}
}
return clause.Expr{}
}
Expand Down
16 changes: 0 additions & 16 deletions uint64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,18 @@ func TestScanUint64(t *testing.T) {
nullableInt.Scan(37)
tests.AssertEqual(t, nullableInt.Get(), 37)

// raw binary of uint8
nullableInt.Scan("0000000000000000000000000000000000000000000000000000000000010001")
tests.AssertEqual(t, nullableInt.Get(), 17)

// uint16
nullableInt.Scan(1234)
tests.AssertEqual(t, nullableInt.Get(), 1234)

// raw binary of uint16
nullableInt.Scan("0000000000000000000000000000000000000000000000000001000011100001")
tests.AssertEqual(t, nullableInt.Get(), 4321)

// uint32
nullableInt.Scan(654321)
tests.AssertEqual(t, nullableInt.Get(), 654321)

// raw binary of uint32
nullableInt.Scan("0000000000000000000000000000000000000000000001011010101000100010")
tests.AssertEqual(t, nullableInt.Get(), 371234)

// uint64
nullableInt.Scan(50000000000)
tests.AssertEqual(t, nullableInt.Get(), 50000000000)

// raw binary of uint64
nullableInt.Scan("0000000000000000000000000000000000111010110111100110100010110001")
tests.AssertEqual(t, nullableInt.Get(), 987654321)

nullableInt.Scan(nil)
tests.AssertEqual(t, nullableInt.Get(), nil)
}
Expand Down

0 comments on commit 6c3da86

Please sign in to comment.