-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsePickle.go
124 lines (112 loc) · 3.42 KB
/
parsePickle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*******************************************************************************
*
* Copyright 2021 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*******************************************************************************/
package builderfile
import (
"encoding/json"
"errors"
"os"
"github.com/mitchellh/mapstructure"
"github.com/nlpodyssey/gopickle/pickle"
"github.com/nlpodyssey/gopickle/types"
"github.com/sapcc/go-bits/logg"
"github.com/sapcc/go-bits/must"
)
type pickleData struct {
ID string
Replicas float64
Dispersion float64
Devices []DeviceInfo `mapstructure:"devs"`
Partitions uint64 `mapstructure:"parts"`
Version uint64
Overload float64
}
func unmarshal(input interface{}) pickleData {
var mappedData pickleData
must.Succeed(mapstructure.Decode(guessType(input), &mappedData))
return mappedData
}
func guessType(input interface{}) interface{} {
switch v := input.(type) {
case *types.Dict:
data := make(map[string]interface{})
for _, entry := range *v {
key := entry.Key.(string) //nolint:errcheck
// skip keys which have tuple indexed Dicts
if key == "_dispersion_graph" || key == "_replica2part2dev" || key == "_last_part_moves" {
continue
}
// skip balance to avoid rounding errors when comparing with text based parser
if key == "balance" {
continue
}
if key == "meta" {
var meta *map[string]string
//nolint:errcheck
if value := entry.Value.(string); value != "" {
err := json.Unmarshal([]byte(value), &meta)
if err != nil {
logg.Fatal("Unmarshalling meta failed: ", err.Error())
}
}
data[entry.Key.(string)] = meta
continue
}
data[entry.Key.(string)] = guessType(entry.Value)
}
return data
case *types.List:
var data []interface{}
for _, entry := range *v {
// skip empty entries in Devices so that mapstructure does not convert them to empty DeviceInfos
if entry == nil {
continue
}
data = append(data, guessType(entry))
}
return data
case bool, float64, int, nil, string:
return v
default:
logg.Fatal("Can't translate type %T", v)
}
return nil
}
type Array types.List
var _ types.Callable = &Array{}
func (*Array) Call(args ...interface{}) (interface{}, error) {
// TODO: make fully functional
// args[0] contains a type like B or H which is not taken into account
return args[1].(*types.List), nil
}
func decodeBuilderFile(builderFilename string) pickleData {
builderReader, err := os.Open(builderFilename)
if err != nil {
logg.Fatal("Reading file failed: %s", err.Error())
}
defer builderReader.Close()
u := pickle.NewUnpickler(builderReader)
u.FindClass = func(module, name string) (interface{}, error) {
if module == "array" && name == "array" {
return &Array{}, nil
}
return nil, errors.New("class not found :(")
}
pickled := must.Return(u.Load())
return unmarshal(pickled)
}