bbgo_origin/pkg/backtest/manifests.go

48 lines
1.1 KiB
Go
Raw Normal View History

package backtest
import "encoding/json"
2022-05-10 06:23:11 +00:00
type ManifestEntry struct {
Type string `json:"type"`
Filename string `json:"filename"`
StrategyID string `json:"strategyID"`
StrategyInstance string `json:"strategyInstance"`
StrategyProperty string `json:"strategyProperty"`
}
type Manifests map[InstancePropertyIndex]string
2022-05-19 12:31:25 +00:00
func (m *Manifests) UnmarshalJSON(j []byte) error {
var entries []ManifestEntry
if err := json.Unmarshal(j, &entries); err != nil {
return err
}
mm := make(Manifests)
for _, entry := range entries {
index := InstancePropertyIndex{
ID: entry.StrategyID,
InstanceID: entry.StrategyInstance,
Property: entry.StrategyProperty,
}
mm[index] = entry.Filename
}
*m = mm
return nil
}
func (m Manifests) MarshalJSON() ([]byte, error) {
2022-05-10 06:23:11 +00:00
var arr []ManifestEntry
for k, v := range m {
2022-05-10 06:23:11 +00:00
arr = append(arr, ManifestEntry{
Type: "strategyProperty",
Filename: v,
StrategyID: k.ID,
StrategyInstance: k.InstanceID,
StrategyProperty: k.Property,
})
}
return json.MarshalIndent(arr, "", " ")
}