package bus import ( "fmt" "sort" "strings" ) type QueryResult struct { Schedules *map[BusStop] *BusStopBuses Lang string Error error Query *QueryObject } func writeShortRoute( lang *string, sb *strings.Builder, b *BusStop ) { if b.PrevStop() != nil { sb.WriteString( (*b.PrevStop().Name)[ *lang ] ) sb.WriteString( " > " ) } sb.WriteString( "**" ) sb.WriteString( (*b.Name)[ *lang ] ) sb.WriteString( "**" ) if b.NextStop() != nil { sb.WriteString( " > " ) sb.WriteString( (*b.NextStop().Name)[ *lang ] ) } sb.WriteString( "\n" ) } type ByRouteId []BusStop func (a ByRouteId) Len() int { return len(a) } func (a ByRouteId) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByRouteId) Less(i, j int) bool { return a[i].RouteId < a[j].RouteId } func ( result QueryResult ) Message() string { if result.Error != nil { return fmt.Sprintf( "%s", result.Error ) } sb := strings.Builder{} if result.Schedules == nil { query := *result.Query if query.Route == "" { sort.Sort( ByRouteId( *query.BusStops ) ) for _, busStop := range *query.BusStops { sb.WriteString( busStop.RouteId ) sb.WriteString( " " ) writeShortRoute( &result.Lang, &sb, &busStop ) } } else if query.BusStops == nil && query.RouteStarts != nil { for d, b := range *query.RouteStarts { sb.WriteString( query.Route ) sb.WriteString( "-" ) sb.WriteString( d ) sb.WriteString( "\n " ) for { sb.WriteString( (*b.Name)[ result.Lang ] ) b = b.NextStop() if b == nil { break } sb.WriteString( " > " ) } sb.WriteString( "\n" ) } } else { sb.WriteString( "Unreachable condition occured!?" ) } } else { for busStop, buses := range *result.Schedules { writeShortRoute( &result.Lang, &sb, &busStop ) for _, bus := range buses.Buses { sb.WriteString( " * " ) if bus.ETAText == "" { sb.WriteString( bus.ETDText ) } else { sb.WriteString( bus.ETAText ) } sb.WriteString( "\n" ) } sb.WriteString( "\n" ) } } return sb.String() }