Added kmb and refactored query.Parse
This commit is contained in:
@@ -4,68 +4,87 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
query "github.com/tgckpg/golifehk/query"
|
||||
utils "github.com/tgckpg/golifehk/utils"
|
||||
)
|
||||
|
||||
type QueryResult struct {
|
||||
Schedules *map[BusStop] *BusStopBuses
|
||||
Schedules *map[*BusStop] *BusStopBuses
|
||||
Lang string
|
||||
Error error
|
||||
Query *QueryObject
|
||||
}
|
||||
|
||||
var MARKDOWN_ESC []string = []string{"_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"}
|
||||
func _escape( t string ) string {
|
||||
for _, c := range MARKDOWN_ESC {
|
||||
t = strings.Replace( t, c, "\\" + c, -1 )
|
||||
}
|
||||
return t
|
||||
Query *query.QueryObject
|
||||
}
|
||||
|
||||
func writeShortRoute( lang *string, sb *strings.Builder, b *BusStop ) {
|
||||
if b.PrevStop() != nil {
|
||||
sb.WriteString( _escape( (*b.PrevStop().Name)[ *lang ] ) )
|
||||
utils.WriteMDv2Text( sb, (*b.PrevStop().Name)[ *lang ] )
|
||||
sb.WriteString( " \\> " )
|
||||
}
|
||||
|
||||
sb.WriteString( "*" )
|
||||
sb.WriteString( _escape( (*b.Name)[ *lang ] ) )
|
||||
utils.WriteMDv2Text( sb, (*b.Name)[ *lang ] )
|
||||
sb.WriteString( "*" )
|
||||
|
||||
if b.NextStop() != nil {
|
||||
sb.WriteString( " \\> " )
|
||||
sb.WriteString( _escape( (*b.NextStop().Name)[ *lang ] ) )
|
||||
utils.WriteMDv2Text( sb, (*b.NextStop().Name)[ *lang ] )
|
||||
}
|
||||
|
||||
sb.WriteString( "\n" )
|
||||
}
|
||||
|
||||
func ( result QueryResult ) Message() string {
|
||||
func ( this QueryResult ) Message() ( string, error ) {
|
||||
|
||||
if result.Error != nil {
|
||||
return fmt.Sprintf( "%s", result.Error )
|
||||
if this.Error != nil {
|
||||
return "", this.Error
|
||||
}
|
||||
|
||||
sb := strings.Builder{}
|
||||
|
||||
if result.Schedules == nil {
|
||||
if this.Schedules == nil {
|
||||
|
||||
query := *result.Query
|
||||
if query.Route == "" {
|
||||
sort.Sort( ByRouteId( *query.BusStops ) )
|
||||
for _, busStop := range *query.BusStops {
|
||||
sb.WriteString( _escape( busStop.RouteId ) )
|
||||
q := *this.Query
|
||||
if q.Key == "" {
|
||||
sort.Sort( query.ByKey( *q.Results ) )
|
||||
for _, entry := range *q.Results {
|
||||
busStop := any( entry ).( *BusStop )
|
||||
utils.WriteMDv2Text( &sb, busStop.RouteId )
|
||||
sb.WriteString( " " )
|
||||
writeShortRoute( &result.Lang, &sb, &busStop )
|
||||
writeShortRoute( &this.Lang, &sb, busStop )
|
||||
}
|
||||
} else if query.BusStops == nil && query.RouteStarts != nil {
|
||||
for d, b := range *query.RouteStarts {
|
||||
sb.WriteString( query.Route )
|
||||
} else if 1 == len( *q.SearchTerms ) && 0 < len( *q.Results ) {
|
||||
|
||||
// Route listing
|
||||
st := map[string] *BusStop{}
|
||||
keys := [] string{}
|
||||
|
||||
for _, entry := range *q.Results {
|
||||
|
||||
busStop := any( entry ).( *BusStop )
|
||||
if _, ok := st[ busStop.Direction ]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
st[ busStop.Direction ] = busStop
|
||||
keys = append( keys, busStop.Direction )
|
||||
|
||||
for st[ busStop.Direction ].PrevStop() != nil {
|
||||
st[ busStop.Direction ] = st[ busStop.Direction ].PrevStop()
|
||||
}
|
||||
}
|
||||
|
||||
sort.Strings( keys )
|
||||
|
||||
for _, d := range keys {
|
||||
b := st[ d ]
|
||||
sb.WriteString( q.Key )
|
||||
sb.WriteString( "\\-" )
|
||||
sb.WriteString( _escape( d ) )
|
||||
utils.WriteMDv2Text( &sb, d )
|
||||
sb.WriteString( "\n " )
|
||||
|
||||
for {
|
||||
sb.WriteString( _escape( (*b.Name)[ result.Lang ] ) )
|
||||
utils.WriteMDv2Text( &sb, (*b.Name)[ this.Lang ] )
|
||||
b = b.NextStop()
|
||||
if b == nil {
|
||||
break
|
||||
@@ -75,28 +94,35 @@ func ( result QueryResult ) Message() string {
|
||||
}
|
||||
sb.WriteString( "\n" )
|
||||
}
|
||||
|
||||
} else if 1 < len( *q.SearchTerms ) && len( *q.Results ) == 0 {
|
||||
terms := make( []string, len(*q.SearchTerms), len(*q.SearchTerms) )
|
||||
for i, term := range *q.SearchTerms {
|
||||
terms[i] = term.Org
|
||||
}
|
||||
return "", fmt.Errorf( "Not Found: \"%s\"", strings.Join( terms, "\", \"" ) )
|
||||
} else {
|
||||
sb.WriteString( _escape( "Unreachable condition occured!?" ) )
|
||||
return "", fmt.Errorf( "%s", "Unreachable condition occured!?" )
|
||||
}
|
||||
} else {
|
||||
if 0 < len(*result.Schedules) {
|
||||
for busStop, buses := range *result.Schedules {
|
||||
writeShortRoute( &result.Lang, &sb, &busStop )
|
||||
if 0 < len( *this.Schedules ) {
|
||||
for busStop, buses := range *this.Schedules {
|
||||
writeShortRoute( &this.Lang, &sb, busStop )
|
||||
for _, bus := range buses.Buses {
|
||||
sb.WriteString( " \\* " )
|
||||
if bus.ETAText == "" {
|
||||
sb.WriteString( _escape( bus.ETDText ) )
|
||||
utils.WriteMDv2Text( &sb, bus.ETDText )
|
||||
} else {
|
||||
sb.WriteString( _escape( bus.ETAText ) )
|
||||
utils.WriteMDv2Text( &sb, bus.ETAText )
|
||||
}
|
||||
sb.WriteString( "\n" )
|
||||
}
|
||||
sb.WriteString( "\n" )
|
||||
}
|
||||
} else {
|
||||
sb.WriteString( _escape( "Schedules are empty...perhaps Out of Service Time?" ) )
|
||||
utils.WriteMDv2Text( &sb, "Schedules are empty...perhaps Out of Service Time?" )
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
return sb.String(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user