Skip to content

Commit bd3d1c0

Browse files
committed
examples: use json decoder for string to float conversion
1 parent 5e54bd8 commit bd3d1c0

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

examples/common.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io/ioutil"
1010
"net/http"
1111
"os"
12-
"strconv"
1312

1413
"github.com/golang/geo/r2"
1514
"github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/metadata"
@@ -60,6 +59,12 @@ func RedirectStdout(f func()) {
6059
os.Stdout = old
6160
}
6261

62+
type mapMetadata struct {
63+
PosX float64 `json:"pos_x,string"`
64+
PosY float64 `json:"pos_y,string"`
65+
Scale float64 `json:"scale,string"`
66+
}
67+
6368
// GetMapMetadata fetches metadata for a specific map version from
6469
// `https://radar-overviews.csgo.saiko.tech/<map>/<crc>/info.json`.
6570
// Panics if any error occurs.
@@ -71,32 +76,23 @@ func GetMapMetadata(name string, crc uint32) metadata.Map {
7176

7277
defer resp.Body.Close()
7378

74-
var data map[string]interface{}
79+
var data map[string]mapMetadata
7580

7681
err = json.NewDecoder(resp.Body).Decode(&data)
7782
checkError(err)
7883

79-
mapInfo, ok := data[name].(map[string]interface{})
84+
mapInfo, ok := data[name]
8085
if !ok {
8186
panic(fmt.Sprintf("failed to get map info.json entry for %q", name))
8287
}
8388

84-
x, err := strconv.ParseFloat(mapInfo["pos_x"].(string), 64)
85-
checkError(err)
86-
87-
y, err := strconv.ParseFloat(mapInfo["pos_y"].(string), 64)
88-
checkError(err)
89-
90-
scale, err := strconv.ParseFloat(mapInfo["scale"].(string), 64)
91-
checkError(err)
92-
9389
return metadata.Map{
9490
Name: name,
9591
PZero: r2.Point{
96-
X: x,
97-
Y: y,
92+
X: mapInfo.PosX,
93+
Y: mapInfo.PosY,
9894
},
99-
Scale: scale,
95+
Scale: mapInfo.Scale,
10096
}
10197
}
10298

0 commit comments

Comments
 (0)