From 7d9ff3599cf28ab740cea0cd870bd179cd0ab75c Mon Sep 17 00:00:00 2001 From: Joel Bryan Juliano Date: Thu, 10 Oct 2024 13:34:45 +0200 Subject: [PATCH] simplify resource resolver methods --- deps/pkl/Exec.pkl | 32 ++++++-------------------------- deps/pkl/Http.pkl | 27 +++++++++------------------ deps/pkl/LLM.pkl | 27 +++++---------------------- 3 files changed, 20 insertions(+), 66 deletions(-) diff --git a/deps/pkl/Exec.pkl b/deps/pkl/Exec.pkl index e2815c8..b351fda 100644 --- a/deps/pkl/Exec.pkl +++ b/deps/pkl/Exec.pkl @@ -23,16 +23,8 @@ class ResourceExec { } function resource(id: String): ResourceExec = - if (!resources.isEmpty) - if (resources.containsKey(id)) + if (resources.getOrNull(id) != null) resources[id] - else - new ResourceExec { - command = "" - stderr = "" - stdout = "" - exitCode = 0 - } else new ResourceExec { command = "" @@ -42,28 +34,16 @@ function resource(id: String): ResourceExec = } function stderr(id: String): String = - if (resource(id).stderr != null) - resource(id).stderr - else - "" + resource(id).stderr function stdout(id: String): String = - if (resource(id).stdout != null) - resource(id).stdout - else - "" + resource(id).stdout function exitCode(id: String): Int = - if (resource(id).exitCode != null) - resource(id).exitCode - else - 0 + resource(id).exitCode function env(id: String, envName: String): String = - if (resource(id).env != null) - if (!resource(id).env.isEmpty) - if (resource(id).env.containsKey(envName)) resource(id).env[envName] else "" - else - "" + if (!resource(id).env.isEmpty) + if (resource(id).env.containsKey(envName)) resource(id).env[envName] else "" else "" diff --git a/deps/pkl/Http.pkl b/deps/pkl/Http.pkl index 5726a5b..0d8d263 100644 --- a/deps/pkl/Http.pkl +++ b/deps/pkl/Http.pkl @@ -30,34 +30,25 @@ class ResponseBlock { } function resource(id: String): ResourceHTTPClient = - if (!resources.isEmpty) - if (resources.containsKey(id)) - resources[id] - else - new ResourceHTTPClient { - method = "GET" - url = "" - data {} - headers {} - response {} -} + if (resources.getOrNull(id) != null) + resources[id] else new ResourceHTTPClient { - method = "GET" - url = "" - data {} - headers {} - response {} + method = "GET" + url = "" + data {} + headers {} + response {} } function responseBody(id: String): String = - if (resource(id).response != null) + if (!resource(id).response.isEmpty) resource(id).response.body else "" function responseHeader(id: String, headerId: String): String = - if (resource(id).response != null) + if (!resource(id).response.isEmpty) if (!resource(id).response.headers.isEmpty) if (resource(id).response.headers.containsKey(headerId)) resource(id).response.headers[headerId] else "" else diff --git a/deps/pkl/LLM.pkl b/deps/pkl/LLM.pkl index 6b843cc..54a5e81 100644 --- a/deps/pkl/LLM.pkl +++ b/deps/pkl/LLM.pkl @@ -17,16 +17,8 @@ class ResourceChat { } function resource(id: String): ResourceChat = - if (!resources.isEmpty) - if (resources.containsKey(id)) - resources[id] - else - new ResourceChat { - prompt = "" - response = "" - jsonResponse = false - jsonResponseKeys {} - } + if (resources.getOrNull(id) != null) + resources[id] else new ResourceChat { prompt = "" @@ -36,19 +28,10 @@ function resource(id: String): ResourceChat = } function response(id: String): String = - if (resource(id).response != null) - resource(id).response - else - "" + resource(id).response function prompt(id: String): String = - if (resource(id).prompt != null) - resource(id).prompt - else - "" + resource(id).prompt function jsonResponse(id: String): Boolean = - if (resource(id).jsonResponse != null) - resource(id).jsonResponse - else - false + resource(id).jsonResponse