-
Notifications
You must be signed in to change notification settings - Fork 17
/
multiepoch-getVersion.go
48 lines (43 loc) · 1.4 KB
/
multiepoch-getVersion.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
package main
import (
"encoding/json"
"fmt"
"github.com/sourcegraph/jsonrpc2"
)
func (ser *MultiEpoch) tryEnrichGetVersion(body []byte) ([]byte, error) {
var decodedRemote jsonrpc2.Response
if err := json.Unmarshal(body, &decodedRemote); err != nil {
return nil, err
}
if decodedRemote.Error != nil || decodedRemote.Result == nil {
return nil, fmt.Errorf("response is not a success response")
}
// node decode the result:
var decodedResult map[string]any
if err := json.Unmarshal(*decodedRemote.Result, &decodedResult); err != nil {
return nil, fmt.Errorf("failed to decode result: %w", err)
}
// enrich the result:
faithfulVersion := ser.GetFaithfulVersionInfo()
decodedResult["faithful"] = faithfulVersion
// re-encode the result:
encodedResult, err := json.Marshal(decodedResult)
if err != nil {
return nil, fmt.Errorf("failed to re-encode result: %w", err)
}
// re-encode the response:
decodedRemote.Result = (*json.RawMessage)(&encodedResult)
encodedResponse, err := json.Marshal(decodedRemote)
if err != nil {
return nil, fmt.Errorf("failed to re-encode response: %w", err)
}
// return the response:
return encodedResponse, nil
}
func (ser *MultiEpoch) GetFaithfulVersionInfo() map[string]any {
faithfulVersion := make(map[string]any)
faithfulVersion["version"] = GitTag
faithfulVersion["commit"] = GitCommit
faithfulVersion["epochs"] = ser.GetEpochNumbers()
return faithfulVersion
}