Skip to content

Commit

Permalink
[components] For mozilla-mobile/android-components#191 - Adds JSON pa…
Browse files Browse the repository at this point in the history
…rsers

UltraBlame original commit: 874d8a6147ff8a2bb785249a51af2b7d7b009598
  • Loading branch information
marco-c committed May 31, 2024
1 parent 808e238 commit 1096653
Show file tree
Hide file tree
Showing 4 changed files with 3,924 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,6 @@ browser
.
search
import
kotlinx
.
coroutines
.
experimental
.
async
import
kotlinx
.
coroutines
.
experimental
.
Deferred
import
mozilla
.
components
Expand All @@ -33,7 +17,7 @@ search
.
parser
.
googleParser
defaultParser
typealias
SearchSuggestionFetcher
=
Expand Down Expand Up @@ -99,7 +83,7 @@ suggestionsURL
.
let
{
googleParser
defaultParser
(
it
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,28 @@ search
.
parser
import
mozilla
.
components
.
browser
.
search
.
SearchEngine
import
org
.
json
.
JSONArray
import
org
.
json
.
JSONObject
private
fun
JSONArray
.
Expand Down Expand Up @@ -115,11 +132,18 @@ List
<
String
>
val
googleParser
private
fun
fromArrayResult
(
resultsIndex
:
Int
)
:
Parser
=
{
return
{
input
-
Expand All @@ -131,7 +155,7 @@ input
.
getJSONArray
(
1
resultsIndex
)
.
asSequence
Expand All @@ -154,3 +178,187 @@ toList
(
)
}
}
private
fun
fromObjectResult
(
resultsKey
:
String
)
:
Parser
{
return
{
input
-
>
JSONObject
(
input
)
.
getJSONArray
(
resultsKey
)
.
asSequence
(
)
.
map
{
it
as
?
String
}
.
filterNotNull
(
)
.
toList
(
)
}
}
private
fun
qwantParserBuilder
(
)
:
Parser
{
return
{
input
-
>
JSONObject
(
input
)
.
getJSONObject
(
"
data
"
)
.
getJSONArray
(
"
items
"
)
.
asSequence
(
)
.
map
{
it
as
?
JSONObject
}
.
map
{
it
?
.
getString
(
"
value
"
)
}
.
filterNotNull
(
)
.
toList
(
)
}
}
val
defaultParser
=
fromArrayResult
(
1
)
val
azerdictParser
=
fromObjectResult
(
"
suggestions
"
)
val
daumParser
=
fromObjectResult
(
"
items
"
)
val
qwantParser
=
qwantParserBuilder
(
)
fun
selectParser
(
searchEngine
:
SearchEngine
)
:
Parser
=
when
(
searchEngine
.
name
)
{
"
Azerdict
"
-
>
azerdictParser
"
"
-
>
daumParser
"
Qwant
"
-
>
qwantParser
else
-
>
defaultParser
}
Loading

0 comments on commit 1096653

Please sign in to comment.