Partially working draft

This commit is contained in:
2022-09-14 21:21:39 +08:00
parent afb73b70ff
commit 534bc70f0f
6 changed files with 151 additions and 40 deletions

View File

@@ -1,41 +1,68 @@
package bus
import (
"errors"
"fmt"
"strings"
"github.com/tgckpg/golifehk/utils"
)
type QueryObject struct {
type queryObj struct {
Route string
BusStops *[]BusStop
}
type QueryResult struct {
BusStops []BusStop
err string
}
type Term struct {
type qTerm struct {
Value string
ProblyRoute bool
}
func Query( message string ) *QueryResult {
return &QueryResult{ err: "No Result" }
func Query( lang string, message string ) *QueryResult {
qo, err := parse( message )
if err != nil {
return &QueryResult{ Error: err }
}
if qo.Route != "" {
schedules, err := getSchedule( lang, qo.Route )
if err != nil {
return &QueryResult{ Error: err }
}
if len( schedules.BusStops ) == 0 {
return &QueryResult{ Error: errors.New( "Schedules are empty. Maybe outside service time?" ) }
}
matches := map[BusStop] *BusStopBuses{}
for _, busStop := range *qo.BusStops {
for _, busStopSch := range schedules.BusStops {
if busStopSch.BusStopId == busStop.StationId {
matches[busStop] = &busStopSch
break
}
}
}
return &QueryResult{ Lang: lang, Schedules: &matches }
}
return &QueryResult{ Error: errors.New( "No Result" ) }
}
func test( entry BusStop, val string ) bool {
switch true {
case strings.Contains( entry.Name_zhant, val ):
case strings.Contains( (*entry.Name)["zh"], val ):
fallthrough
case strings.Contains( entry.Name_en, val ):
case strings.Contains( (*entry.Name)["en"], val ):
return true
}
return false
}
func parse( line string ) ( *QueryObject, error ) {
func parse( line string ) ( *queryObj, error ) {
busStops, err := getBusStops()
if err != nil {
@@ -47,10 +74,10 @@ func parse( line string ) ( *QueryObject, error ) {
matches := []BusStop{}
// Sanitize and assume properties for each of the keywords
terms := []Term{}
terms := []qTerm{}
for _, val := range strings.Split( line, " " ) {
val = strings.ToUpper( strings.Trim( val, " " ) )
term := Term{
term := qTerm{
Value: val,
ProblyRoute: strings.ContainsAny( val, utils.ROUTE_CHARS ),
}
@@ -99,5 +126,5 @@ func parse( line string ) ( *QueryObject, error ) {
matches = matches_in
}
return &QueryObject{ Route: route, BusStops: &matches }, err
return &queryObj{ Route: route, BusStops: &matches }, err
}