From c7ce590e3b4cffb102abb35cb0bb1e9821defc3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 6 May 2025 14:43:22 +0800 Subject: [PATCH] add bool support --- abstract/json_read.go | 2 ++ implement/gjson_read.go | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/abstract/json_read.go b/abstract/json_read.go index e9c07a8..6826652 100644 --- a/abstract/json_read.go +++ b/abstract/json_read.go @@ -23,6 +23,8 @@ type IJsonRead interface { Float(dataPath string) (float64, error) // String 转换为string类型 String(dataPath string) (string, error) + // Bool 转换为bool类型 + Bool(dataPath string) (bool, error) // Map 转换为map Map(dataPath string) (map[string]any, error) // MapWithReceiver 通过指针接收 diff --git a/implement/gjson_read.go b/implement/gjson_read.go index 9ba9eb9..9e3e66c 100644 --- a/implement/gjson_read.go +++ b/implement/gjson_read.go @@ -74,6 +74,14 @@ func (g *gjsonRead) Float(dataPath string) (float64, error) { return gjson_hack.Float64(g.gjsonResult.Get(dataPath)) } +func (g *gjsonRead) Bool(dataPath string) (bool, error) { + res := g.gjsonResult.Get(dataPath) + if !res.IsBool() { + return false, errors.New(dataPath + ": is not a bool") + } + return res.Bool(), nil +} + func (g *gjsonRead) String(dataPath string) (string, error) { if !g.Exist(dataPath) { return "", errors.New(dataPath + ": not found")