You are viewing Nextmv legacy docs. ⚡️ Go to latest docs ⚡️

Sdk

v0.17.0 Upgrade Guide

Nextmv SDK

  • Set your go.mod requirements to match the new version.
require (
    github.com/nextmv-io/code/engines v0.17.0
)
Copy

The route.VehicleMarshaller interface was removed and the route.FleetMarshaller interface was renamed to route.Marshaler.

// VehicleMarshaller allows a user to customize the output of the router engine
// when using a single vehicle.
type VehicleMarshaller interface {
    // Marshal translates the output of the vehicle engine's state to an
    // interpretable result.
    Marshal(s vehicleEngine.State) ([]byte, error)
}

// FleetMarshaller allows a user to customize the output of the router engine
// when using multiple vehicles.
type FleetMarshaller interface {
    // Marshal translates the output of the fleet engine's state to an
    // interpretable result.
    Marshal(s fleetEngine.State) ([]byte, error)
}
Copy

The route.Output option was the only place were these interfaces were used. Given that the route.VehicleMarshaller interface was removed, the option now takes in a single argument: a route.Marshaler interface, which operates in the same way as the deprecated route.FleetMarshaller interface.

// Custom type to implement the VehicleMarshaller interface.
type vehicleMarshaller struct{}

// Marshall the length of the route, excluding the starts and ends.
func (m vehicleMarshaller) Marshal(s vehicleEngine.State) ([]byte, error) {
    v := map[string]int{
        "route_length": len(s.Route()) - 2,
    }
    return json.Marshal(v)
}

// Custom type to implement the FleetMarshaller interface.
type fleetMarshaller struct{}

// Marshall the number of unassigned stops and routes. Only count routes that
// have locations assigned.
func (m fleetMarshaller) Marshal(s fleetEngine.State) ([]byte, error) {
    routes := 0
    for _, state := range s.Vehicles() {
        if len(state.Route()) > 2 {
            routes++
        }
    }
    v := map[string]int{
        "num_routes":     routes,
        "num_unassigned": s.Unassigned().Len(),
    }
    return json.Marshal(v)
}

// Using the custom types.
func main() {
    v := vehicleMarshaller{}
    f := fleetMarshaller{}
    router, err := route.NewRouter(stops, vehicles, route.Output(v, f))
}
Copy

Page last updated