Skip to content

Commit c207918

Browse files
dev: add some Must method
1 parent f3ccdb5 commit c207918

File tree

4 files changed

+171
-9
lines changed

4 files changed

+171
-9
lines changed

format/bytes.go

+28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package format
33
import (
44
"encoding/binary"
55
"fmt"
6+
"log"
67
)
78

89
// Uint16ToBytes uint16 -> bytes
@@ -35,6 +36,15 @@ func BytesToUint16(b []byte) (uint16, error) {
3536
return binary.BigEndian.Uint16(b), nil
3637
}
3738

39+
// MustBytesToUint16 bytes -> uint16
40+
func MustBytesToUint16(b []byte) uint16 {
41+
if len(b) != 2 {
42+
log.Fatalf("invalid data, must 2 bytes, but %d", len(b))
43+
}
44+
45+
return binary.BigEndian.Uint16(b)
46+
}
47+
3848
// BytesToUint32 bytes -> uint32
3949
func BytesToUint32(b []byte) (uint32, error) {
4050
if len(b) != 4 {
@@ -44,6 +54,15 @@ func BytesToUint32(b []byte) (uint32, error) {
4454
return binary.BigEndian.Uint32(b), nil
4555
}
4656

57+
// MustBytesToUint32 bytes -> uint16
58+
func MustBytesToUint32(b []byte) uint32 {
59+
if len(b) != 4 {
60+
log.Fatalf("invalid data, must 4 bytes, but %d", len(b))
61+
}
62+
63+
return binary.BigEndian.Uint32(b)
64+
}
65+
4766
// BytesToUint64 bytes -> uint64
4867
func BytesToUint64(b []byte) (uint64, error) {
4968
if len(b) != 8 {
@@ -52,3 +71,12 @@ func BytesToUint64(b []byte) (uint64, error) {
5271

5372
return binary.BigEndian.Uint64(b), nil
5473
}
74+
75+
// MustBytesToUint64 bytes -> uint16
76+
func MustBytesToUint64(b []byte) uint64 {
77+
if len(b) != 8 {
78+
log.Fatalf("invalid data, must 8 bytes, but %d", len(b))
79+
}
80+
81+
return binary.BigEndian.Uint64(b)
82+
}

format/string.go

+60-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,71 @@
11
package format
22

33
import (
4+
"log"
5+
"runtime"
46
"strconv"
5-
6-
"github.com/fagongzi/util/hack"
77
)
88

9+
// ParseStrUInt64 str -> uint64
10+
func ParseStrUInt64(data string) (uint64, error) {
11+
ret, err := strconv.ParseInt(data, 10, 64)
12+
if err != nil {
13+
return 0, err
14+
}
15+
return uint64(ret), nil
16+
}
17+
18+
// MustParseStrUInt64 str -> uint64
19+
func MustParseStrUInt64(data string) uint64 {
20+
value, err := ParseStrUInt64(data)
21+
if err != nil {
22+
buf := make([]byte, 4096)
23+
runtime.Stack(buf, true)
24+
log.Fatalf("parse to uint64 failed, data=<%s> errors:\n %+v \n %s",
25+
data,
26+
err,
27+
buf)
28+
}
29+
30+
return value
31+
}
32+
933
// ParseStrInt64 str -> int64
10-
func ParseStrInt64(v []byte) (int64, error) {
11-
return strconv.ParseInt(hack.SliceToString(v), 10, 64)
34+
func ParseStrInt64(data string) (int64, error) {
35+
return strconv.ParseInt(data, 10, 64)
36+
}
37+
38+
// MustParseStrInt64 str -> int64
39+
func MustParseStrInt64(data string) int64 {
40+
value, err := ParseStrInt64(data)
41+
if err != nil {
42+
buf := make([]byte, 4096)
43+
runtime.Stack(buf, true)
44+
log.Fatalf("parse to int64 failed, data=<%s> errors:\n %+v \n %s",
45+
data,
46+
err,
47+
buf)
48+
}
49+
50+
return value
1251
}
1352

1453
// ParseStrFloat64 str -> float64
15-
func ParseStrFloat64(v []byte) (float64, error) {
16-
return strconv.ParseFloat(hack.SliceToString(v), 64)
54+
func ParseStrFloat64(data string) (float64, error) {
55+
return strconv.ParseFloat(data, 64)
56+
}
57+
58+
// MustParseStrFloat64 str -> float64
59+
func MustParseStrFloat64(data string) float64 {
60+
value, err := ParseStrFloat64(data)
61+
if err != nil {
62+
buf := make([]byte, 4096)
63+
runtime.Stack(buf, true)
64+
log.Fatalf("parse to float64 failed, data=<%s> errors:\n %+v \n %s",
65+
data,
66+
err,
67+
buf)
68+
}
69+
70+
return value
1771
}

json/json.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package json
22

33
import (
44
"encoding/json"
5-
"fmt"
5+
"log"
6+
"runtime"
67
)
78

89
// InitModel init model
@@ -14,7 +15,12 @@ type InitModel interface {
1415
func MustMarshal(value interface{}) []byte {
1516
v, err := json.Marshal(value)
1617
if err != nil {
17-
panic(fmt.Sprintf("marash failed: %+v", err))
18+
buf := make([]byte, 4096)
19+
runtime.Stack(buf, true)
20+
log.Fatalf("json marshal failed, value=<%v> errors:\n %+v \n %s",
21+
value,
22+
err,
23+
buf)
1824
}
1925
return v
2026
}
@@ -23,7 +29,12 @@ func MustMarshal(value interface{}) []byte {
2329
func MustUnmarshal(value interface{}, data []byte) {
2430
err := Unmarshal(value, data)
2531
if err != nil {
26-
panic(fmt.Sprintf("unmarash failed: %+v", err))
32+
buf := make([]byte, 4096)
33+
runtime.Stack(buf, true)
34+
log.Fatalf("json unmarshal failed, data=<%v> errors:\n %+v \n %s",
35+
data,
36+
err,
37+
buf)
2738
}
2839

2940
if init, ok := value.(InitModel); ok {

protoc/protoc.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2016 DeepFabric, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package protoc
15+
16+
import (
17+
"log"
18+
"runtime"
19+
)
20+
21+
// PB pb interface
22+
type PB interface {
23+
Marshal() ([]byte, error)
24+
MarshalTo(data []byte) (int, error)
25+
Unmarshal(data []byte) error
26+
}
27+
28+
// MustUnmarshal if unmarshal failed, will panic
29+
func MustUnmarshal(pb PB, data []byte) {
30+
err := pb.Unmarshal(data)
31+
if err != nil {
32+
buf := make([]byte, 4096)
33+
runtime.Stack(buf, true)
34+
log.Fatalf("pb unmarshal failed, data=<%v> errors:\n %+v \n %s",
35+
data,
36+
err,
37+
buf)
38+
}
39+
}
40+
41+
// MustMarshal if marsh failed, will panic
42+
func MustMarshal(pb PB) []byte {
43+
data, err := pb.Marshal()
44+
if err != nil {
45+
buf := make([]byte, 4096)
46+
runtime.Stack(buf, true)
47+
log.Fatalf("pb marshal failed, pb=<%+v> errors:\n %+v \n %s",
48+
pb,
49+
err,
50+
buf)
51+
}
52+
53+
return data
54+
}
55+
56+
// MustMarshalTo if marsh failed, will panic
57+
func MustMarshalTo(pb PB, data []byte) int {
58+
n, err := pb.MarshalTo(data)
59+
if err != nil {
60+
buf := make([]byte, 4096)
61+
runtime.Stack(buf, true)
62+
log.Fatalf("pb marshal failed, pb=<%v> errors:\n %+v \n %s",
63+
pb,
64+
err,
65+
buf)
66+
}
67+
68+
return n
69+
}

0 commit comments

Comments
 (0)