Skip to content

Commit

Permalink
feat(compiler): add JsonPath support for pattern actions and closed #11
Browse files Browse the repository at this point in the history
This commit introduces the ability to use JsonPath within pattern actions, allowing for JSON parsing and value extraction based on specified paths. The changes include:

- Adding a new `JsonPath` class in `PatternActionFunc.kt` to handle JsonPath functionality.
- Implementing the processing logic for the JsonPath action in `PatternFuncProcessor.kt`.
- Updating the HobbitHoleParser in `HobbitHoleParser.kt` to recognize and parse the new `jsonpath` pattern action.
  • Loading branch information
phodal committed Aug 25, 2024
1 parent a265177 commit 14bed16
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.findFile
import com.intellij.openapi.vfs.readText
import com.nfeld.jsonpathkt.JsonPath
import com.nfeld.jsonpathkt.extension.read
import com.phodal.shirecore.ShirelangNotifications
import com.phodal.shirecore.guard.RedactProcessor
import com.phodal.shirecore.search.function.ScoredText
Expand Down Expand Up @@ -227,6 +229,23 @@ open class PatternFuncProcessor(open val myProject: Project, open val hole: Hobb
ThreadProcessor.execute(myProject, action.fileName)
}

is PatternActionFunc.JsonPath -> {
val jsonStr = action.obj ?: lastResult as String

val result: String = try {
JsonPath.parse(jsonStr)?.read<Any>(action.path.trim()).toString()
} catch (e: Exception) {
logger<FunctionStatementProcessor>().warn("jsonpath error: $e")
return jsonStr
}

if (result == "null") {
logger<FunctionStatementProcessor>().warn("jsonpath error: $result for $jsonStr")
return jsonStr
}

result
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,19 @@ object HobbitHoleParser {
PatternActionFunc.Thread(args.first())
}

"jsonpath" -> {
if (args.isEmpty()) {
logger.error("parsePatternAction, jsonpath requires at least 1 arguments")
return null
}

if (args.size < 2) {
PatternActionFunc.JsonPath(null, args[0])
} else {
PatternActionFunc.JsonPath(args[0], args[1])
}
}

null -> {
logger.warn("parsePatternAction, Unknown pattern action: ${expr.funcCall}")
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ sealed class PatternActionFunc(open val funcName: String) {
*/
class Thread(val fileName: String) : PatternActionFunc("thread")

/**
* the jsonpath function will parse the json and get the value by jsonpath
*/
class JsonPath(val obj: String?, val path: String) : PatternActionFunc("jsonpath")

/**
* User Custom Functions
*/
Expand Down

0 comments on commit 14bed16

Please sign in to comment.