Skip to content

Commit

Permalink
Refactor of jsonSearchControllers
Browse files Browse the repository at this point in the history
  • Loading branch information
cnrudd committed Jan 27, 2025
1 parent 30d26d4 commit 5ad232f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This file belongs to Hoist, an application development toolkit
* developed by Extremely Heavy Industries (www.xh.io | [email protected])
*
* Copyright © 2025 Extremely Heavy Industries Inc.
*/

package io.xh.hoist.admin

import com.jayway.jsonpath.Configuration
import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.Option
import io.xh.hoist.BaseController

class BaseJsonSearchController extends BaseController {

private Configuration matchSearchConf = Configuration.builder()
.options(
Option.SUPPRESS_EXCEPTIONS,
Option.ALWAYS_RETURN_LIST
).build()

private Configuration nodeSearchConf = Configuration.builder()
.options(
Option.AS_PATH_LIST,
Option.ALWAYS_RETURN_LIST
).build()

def getMatchingNodes(String json, String path, boolean asPathList) {
def ret = JsonPath.using(nodeSearchConf).parse(json).read(path)
renderJSON(ret)
}

protected hasPathMatch(String json, String path) {
def result = JsonPath.using(matchSearchConf).parse(json).read(path)
return result.size() > 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,14 @@

package io.xh.hoist.admin

import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.Configuration
import com.jayway.jsonpath.Option
import io.xh.hoist.BaseController
import io.xh.hoist.jsonblob.JsonBlob
import io.xh.hoist.security.Access

@Access(['HOIST_ADMIN_READER'])
class JsonBlobSearchAdminController extends BaseController {
class JsonBlobSearchAdminController extends BaseJsonSearchController {

def searchByJsonPath() {
Configuration conf = Configuration.builder()
.options(
Option.SUPPRESS_EXCEPTIONS,
Option.ALWAYS_RETURN_LIST
).build()

List<JsonBlob> results = JsonBlob.list().findAll { entry ->
def result = JsonPath.using(conf).parse(entry.value).read(params.path)
return result.size() > 0
}
List<JsonBlob> results = JsonBlob.list().findAll { hasPathMatch(it.value, params.path) }

def ret = results.collect { it ->
[
Expand All @@ -41,14 +28,4 @@ class JsonBlobSearchAdminController extends BaseController {
}
renderJSON(ret)
}

def getMatchingNodes(String json, String path, boolean asPathList) {
Configuration conf = asPathList
? Configuration.builder().options(Option.AS_PATH_LIST, Option.ALWAYS_RETURN_LIST).build()
: Configuration.defaultConfiguration()

def ret = JsonPath.using(conf).parse(json).read(path)
renderJSON(ret)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,17 @@

package io.xh.hoist.admin

import com.jayway.jsonpath.Configuration
import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.Option
import io.xh.hoist.BaseController
import io.xh.hoist.pref.Preference
import io.xh.hoist.pref.UserPreference
import io.xh.hoist.security.Access

@Access(['HOIST_ADMIN_READER'])
class PreferenceJsonSearchAdminController extends BaseController {
class PreferenceJsonSearchAdminController extends BaseJsonSearchController {

def searchByJsonPath() {
Configuration conf = Configuration.builder()
.options(
Option.SUPPRESS_EXCEPTIONS,
Option.ALWAYS_RETURN_LIST
).build()

List<Preference> jsonPrefs = Preference.findAllByType('json')
List<UserPreference> userPrefs = jsonPrefs.collect { UserPreference.findAllByPreference(it) }.flatten()
List<UserPreference> results = userPrefs.findAll { entry ->
def result = JsonPath.using(conf).parse(entry.userValue).read(params.path)
return result.size() > 0
}
List<UserPreference> results = userPrefs.findAll { hasPathMatch(it.userValue, params.path) }

def ret = results.collect { it ->
[
Expand All @@ -45,13 +32,4 @@ class PreferenceJsonSearchAdminController extends BaseController {
renderJSON(ret)
}

def getMatchingNodes(String json, String path, boolean asPathList) {
Configuration conf = asPathList
? Configuration.builder().options(Option.AS_PATH_LIST, Option.ALWAYS_RETURN_LIST).build()
: Configuration.defaultConfiguration()

def ret = JsonPath.using(conf).parse(json).read(path)
renderJSON(ret)
}

}

0 comments on commit 5ad232f

Please sign in to comment.