make context great again

This commit is contained in:
2019-11-14 22:28:58 +03:00
parent 3560be99a1
commit 6a37870bd2
12 changed files with 160 additions and 84 deletions

View File

@ -13,26 +13,26 @@ const (
)
type ClientCtx struct {
ctx context.Context
context.Context
}
func NewClientContext(config *util.Config, logger *zerolog.Logger) ClientCtx {
ctx := context.Context(context.TODO())
ctx = context.WithValue(ctx, configKey, config)
ctx = context.WithValue(ctx, loggerKey, logger)
return ClientCtx{ctx: ctx}
return ClientCtx{ ctx}
}
func (c *ClientCtx) Config() *util.Config {
cfg, ok := c.ctx.Value(configKey).(*util.Config)
func Config(c context.Context) *util.Config {
cfg, ok := c.Value(configKey).(*util.Config)
if !ok {
panic(fmt.Errorf("no access to config from context"))
}
return cfg
}
func (c *ClientCtx) Logger() *zerolog.Logger {
logger, ok := c.ctx.Value(loggerKey).(*zerolog.Logger)
func Logger(c context.Context) *zerolog.Logger {
logger, ok := c.Value(loggerKey).(*zerolog.Logger)
if !ok {
panic(fmt.Errorf("no access to logger from context"))
}

View File

@ -16,7 +16,7 @@ func GetTriangles(coords []types.Coords, w, h int) []types.Edge {
return output
}
func GetMst(coords []types.Coords, w, h int) []types.Edge {
func GetMst(coords []types.Coords, w, h, negativeWeight int) []types.Edge {
d := &Delaunay{}
@ -48,7 +48,7 @@ func GetMst(coords []types.Coords, w, h int) []types.Edge {
kruskals.SimpleWeightedEdge{
nodeMap[e.Nodes[0].Id],
nodeMap[e.Nodes[1].Id],
int(e.Nodes[0].Coords.DistanceTo(e.Nodes[1].Coords))},
negativeWeight - int(e.Nodes[0].Coords.DistanceTo(e.Nodes[1].Coords))},
)
}