go.temporal.io/server/common/number/number.go

131 LOC · 69 covered · 62 uncovered · 20 ranges · 10 concepts · 8 introducers · 5 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 package number
2
3 import (
4 "fmt"
5 )
6
7 const (
8 TypeUnknown Type = iota
9 TypeFloat
10 TypeInt
11 TypeUint
12 )
13
14 type (
15 Type int
16 Number struct {
17 numberType Type
18 value any
19 }
20 )
21
22 func NewNumber(
23 value any,
24 > ) Number { number.go ×3
25 >
26 > var numberType Type
27 > var number any
28 > switch n := value.(type) {
29 > case int8: number.go ×4
30 > numberType = TypeInt
31 > number = int(n)
32 > case int16:
33 > numberType = TypeInt
34 > number = int(n)
35 > case int32: number.go ×1
36 > numberType = TypeInt
37 > number = int(n)
38 > case int64: number.go ×4
39 > numberType = TypeInt
40 > number = int(n)
41 > case int: number.go ×1
42 > numberType = TypeInt
43 > number = n
44
45 > case uint8: number.go ×4
46 > numberType = TypeUint
47 > number = uint(n)
48 > case uint16:
49 > numberType = TypeUint
50 > number = uint(n)
51 > case uint32:
52 > numberType = TypeUint
53 > number = uint(n)
54 > case uint64:
55 > numberType = TypeUint
56 > number = uint(n)
57 > case uint:
58 > numberType = TypeUint
59 > number = n
60
61 > case float32: number.go ×4
62 > numberType = TypeFloat
63 > number = float64(n)
64 > case float64:
65 > numberType = TypeFloat
66 > number = n
67
68 default:
69 // DO NOT panic here
70 // the value is provided during runtime
71 // the logic cannot just panic if input is not a number
72 numberType = TypeUnknown
73 number = nil
74 }
75
76 > return Number{ number.go ×3
77 > numberType: numberType,
78 > value: number,
79 > }
80 }
81
82 func (n Number) GetIntOrDefault(
83 defaultValue int,
84 > ) int { number.go ×2
85 > switch n.numberType {
86 > case TypeFloat: number.go ×4
87 > return int(n.value.(float64))
88 > case TypeInt: number.go ×4
89 > return n.value.(int)
90 > case TypeUint: number.go ×4
91 > return int(n.value.(uint))
92 case TypeUnknown:
93 return defaultValue
94 default:
95 panic(fmt.Sprintf("unknown number type: %v", n.numberType))
96 }
97 }
98
99 func (n Number) GetUintOrDefault(
100 defaultValue uint,
101 > ) uint { number.go ×2
102 > switch n.numberType {
103 > case TypeFloat: number.go ×4
104 > return uint(n.value.(float64))
105 > case TypeInt: number.go ×4
106 > return uint(n.value.(int))
107 > case TypeUint: number.go ×4
108 > return n.value.(uint)
109 case TypeUnknown:
110 return defaultValue
111 default:
112 panic(fmt.Sprintf("unknown number type: %v", n.numberType))
113 }
114 }
115
116 func (n Number) GetFloatOrDefault(
117 defaultValue float64,
118 > ) float64 { number.go ×3
119 > switch n.numberType {
120 > case TypeFloat: number.go ×4
121 > return n.value.(float64)
122 > case TypeInt: number.go ×1
123 > return float64(n.value.(int))
124 > case TypeUint: number.go ×4
125 > return float64(n.value.(uint))
126 case TypeUnknown:
127 return defaultValue
128 default:
129 panic(fmt.Sprintf("unknown number type: %v", n.numberType))
130 }
131 }