Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update strings.format to adhere to the specification #1133

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ go_repository(
go_repository(
name = "dev_cel_expr",
importpath = "cel.dev/expr",
sum = "h1:NciYrtDRIR0lNCnH1LFJegdjspNx9fI59O7TWcua/W4=",
version = "v0.19.1",
sum = "h1:o+Wj235dy4gFYlYin3JsMpp3EEfMrPm/6tdoyjT98S0=",
version = "v0.21.2",
)

# local_repository(
Expand Down
4 changes: 2 additions & 2 deletions cel/cel_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Example() {
}

fmt.Println(out)
// Output:Hello world! Nice to meet you, I'm CEL.
// Output:"Hello \"world\"! Nice to meet you, I'm \"CEL\".\n"
}

func Example_globalOverload() {
Expand Down Expand Up @@ -116,7 +116,7 @@ func Example_globalOverload() {
}

fmt.Println(out)
// Output:CEL and world are shaking hands.
// Output:"\"CEL\" and \"world\" are shaking hands.\n"
}

func Example_statefulOverload() {
Expand Down
12 changes: 6 additions & 6 deletions cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2438,7 +2438,7 @@ func TestQuotedFields(t *testing.T) {
},
{
expr: "{'key-1': 64}.`key-2`",
errorSubstr: "no such key: key-2",
errorSubstr: "no such key: \"key-2\"",
},
{
expr: "has({'key-1': 64}.`key-1`)",
Expand Down Expand Up @@ -2951,7 +2951,7 @@ func TestOptionalValuesEval(t *testing.T) {
},
},
},
out: "no such key: a",
out: "no such key: \"a\"",
},
{
expr: `m.?c.missing.or(m.?c['dashed-index']).orValue('').size()`,
Expand Down Expand Up @@ -3136,23 +3136,23 @@ func TestEnableErrorOnBadPresenceTest(t *testing.T) {
},
{
expr: `{'null_field': dyn(null)}.?null_field.?nested`,
out: "no such key: nested",
out: "no such key: \"nested\"",
},
{
expr: `{'zero_field': dyn(0)}.?zero_field.?invalid`,
out: "no such key: invalid",
out: "no such key: \"invalid\"",
},
{
expr: `{0: dyn(0)}[?0].?invalid`,
out: "no such key: invalid",
out: "no such key: \"invalid\"",
},
{
expr: `{true: dyn(0)}[?false].?invalid`,
out: types.OptionalNone,
},
{
expr: `{true: dyn(0)}[?true].?invalid`,
out: "no such key: invalid",
out: "no such key: \"invalid\"",
},
}

Expand Down
10 changes: 8 additions & 2 deletions common/types/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package types
import (
"fmt"
"reflect"
"strconv"

"github.com/google/cel-go/common/types/ref"

Expand Down Expand Up @@ -93,7 +92,7 @@ func (b Bool) ConvertToNative(typeDesc reflect.Type) (any, error) {
func (b Bool) ConvertToType(typeVal ref.Type) ref.Val {
switch typeVal {
case StringType:
return String(strconv.FormatBool(bool(b)))
return String(b.String())
case BoolType:
return b
case TypeType:
Expand Down Expand Up @@ -128,6 +127,13 @@ func (b Bool) Value() any {
return bool(b)
}

func (b Bool) String() string {
if b {
return "true"
}
return "false"
}

// IsBool returns whether the input ref.Val or ref.Type is equal to BoolType.
func IsBool(elem ref.Val) bool {
switch v := elem.(type) {
Expand Down
15 changes: 15 additions & 0 deletions common/types/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/base64"
"fmt"
"reflect"
"strings"
"unicode/utf8"

"github.com/google/cel-go/common/types/ref"
Expand Down Expand Up @@ -138,3 +139,17 @@ func (b Bytes) Type() ref.Type {
func (b Bytes) Value() any {
return []byte(b)
}

func (b Bytes) String() string {
return fmt.Sprintf("b\"%s\"", bytesToOctets([]byte(b)))
}

// bytesToOctets converts byte sequences to a string using a three digit octal encoded value
// per byte.
func bytesToOctets(byteVal []byte) string {
var b strings.Builder
for _, c := range byteVal {
fmt.Fprintf(&b, "\\%03o", c)
}
return b.String()
}
19 changes: 19 additions & 0 deletions common/types/double.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"fmt"
"math"
"reflect"
"strconv"
"strings"

"github.com/google/cel-go/common/types/ref"

Expand Down Expand Up @@ -209,3 +211,20 @@ func (d Double) Type() ref.Type {
func (d Double) Value() any {
return float64(d)
}

func (d Double) String() string {
if math.IsNaN(float64(d)) {
return "double(\"NaN\")"
}
if math.IsInf(float64(d), -1) {
return "double(\"-Infinity\")"
}
if math.IsInf(float64(d), 1) {
return "double(\"Infinity\")"
}
s := strconv.FormatFloat(float64(d), 'f', -1, 64)
if !strings.ContainsRune(s, '.') {
s = s + ".0"
}
return s
}
4 changes: 4 additions & 0 deletions common/types/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ func (d Duration) Value() any {
return d.Duration
}

func (d Duration) String() string {
return fmt.Sprintf("duration(\"%ss\")", strconv.FormatFloat(d.Seconds(), 'f', -1, 64))
}

// DurationGetHours returns the duration in hours.
func DurationGetHours(val ref.Val) ref.Val {
dur, ok := val.(Duration)
Expand Down
4 changes: 4 additions & 0 deletions common/types/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ func (i Int) Value() any {
return int64(i)
}

func (i Int) String() string {
return strconv.FormatInt(int64(i), 10)
}

// isJSONSafe indicates whether the int is safely representable as a floating point value in JSON.
func (i Int) isJSONSafe() bool {
return i >= minIntJSON && i <= maxIntJSON
Expand Down
2 changes: 1 addition & 1 deletion common/types/json_struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func TestJsonStructGet(t *testing.T) {
}

e, isError := mapVal.Get(String("third")).(*Err)
if !isError || e.Error() != "no such key: third" {
if !isError || e.Error() != "no such key: \"third\"" {
t.Errorf("Got %v, wanted no such key: third", e)
}
}
2 changes: 1 addition & 1 deletion common/types/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (l *baseList) String() string {
var sb strings.Builder
sb.WriteString("[")
for i := 0; i < l.size; i++ {
sb.WriteString(fmt.Sprintf("%v", l.get(i)))
sb.WriteString(fmt.Sprintf("%s", l.Get(Int(i))))
if i != l.size-1 {
sb.WriteString(", ")
}
Expand Down
4 changes: 2 additions & 2 deletions common/types/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestBaseListGet(t *testing.T) {

func TestBaseListString(t *testing.T) {
l := DefaultTypeAdapter.NativeToValue([]any{1, "hello", 2.1, true, []string{"world"}})
want := `[1, hello, 2.1, true, [world]]`
want := `[1, "hello", 2.1, true, ["world"]]`
if fmt.Sprintf("%v", l) != want {
t.Errorf("l.String() got %v, wanted %v", l, want)
}
Expand All @@ -192,7 +192,7 @@ func TestBaseListString(t *testing.T) {
func TestConcatListString(t *testing.T) {
l := DefaultTypeAdapter.NativeToValue([]any{1, "hello", 2.1, true}).(traits.Lister)
c := l.Add(DefaultTypeAdapter.NativeToValue([]string{"world"}))
want := `[1, hello, 2.1, true, world]`
want := `[1, "hello", 2.1, true, "world"]`
if fmt.Sprintf("%v", c) != want {
t.Errorf("c.String() got %v, wanted %v", c, want)
}
Expand Down
24 changes: 20 additions & 4 deletions common/types/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package types
import (
"fmt"
"reflect"
"sort"
"strings"

"github.com/stoewer/go-strcase"
Expand Down Expand Up @@ -299,20 +300,35 @@ func (m *baseMap) Size() ref.Val {
return Int(m.size)
}

type baseMapEntry struct {
key string
val string
}

// String converts the map into a human-readable string.
func (m *baseMap) String() string {
var sb strings.Builder
sb.WriteString("{")
it := m.Iterator()
i := 0
var ents []baseMapEntry
if s, ok := m.Size().(Int); ok {
ents = make([]baseMapEntry, 0, int(s))
}
for it.HasNext() == True {
k := it.Next()
v, _ := m.Find(k)
sb.WriteString(fmt.Sprintf("%v: %v", k, v))
if i != m.size-1 {
ents = append(ents, baseMapEntry{fmt.Sprintf("%s", k), fmt.Sprintf("%s", v)})
}
sort.SliceStable(ents, func(i, j int) bool {
return ents[i].key < ents[j].key
})
for i, ent := range ents {
if i > 0 {
sb.WriteString(", ")
}
i++
sb.WriteString(ent.key)
sb.WriteString(": ")
sb.WriteString(ent.val)
}
sb.WriteString("}")
return sb.String()
Expand Down
48 changes: 24 additions & 24 deletions common/types/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,24 +429,24 @@ func TestDynamicMapGet(t *testing.T) {
t.Errorf("nestedVal.Get(1) got %v, wanted -1.0", floatVal)
}
err := mapVal.Get(String("absent"))
if !IsError(err) || err.(*Err).Error() != "no such key: absent" {
t.Errorf("mapVal.Get('absent') got %v, wanted no such key: absent.", err)
if !IsError(err) || err.(*Err).Error() != "no such key: \"absent\"" {
t.Errorf("mapVal.Get('absent') got %v, wanted no such key: \"absent\".", err)
}
err = nestedVal.Get(String("bad_key"))
if !IsError(err) || err.(*Err).Error() != "no such key: bad_key" {
t.Errorf("nestedVal.Get('bad_key') errored %v, wanted no such key: bad_key.", err)
if !IsError(err) || err.(*Err).Error() != "no such key: \"bad_key\"" {
t.Errorf("nestedVal.Get('bad_key') errored %v, wanted no such key: \"bad_key\".", err)
}
empty, ok := mapVal.Get(String("empty")).(traits.Mapper)
if !ok {
t.Fatalf("mapVal.Get('empty') got %v, wanted empty map", mapVal.Get(String("empty")))
}
err = empty.Get(String("hello"))
if !IsError(err) || err.(*Err).Error() != "no such key: hello" {
t.Errorf("empty.Get('hello') got %v, wanted no such key: hello", err)
if !IsError(err) || err.(*Err).Error() != "no such key: \"hello\"" {
t.Errorf("empty.Get('hello') got %v, wanted no such key: \"hello\"", err)
}
err = empty.Get(Double(-1.0))
if !IsError(err) || err.(*Err).Error() != "no such key: -1" {
t.Errorf("empty.Get(-1.0) got %v, wanted no such key: -1", err)
if !IsError(err) || err.(*Err).Error() != "no such key: -1.0" {
t.Errorf("empty.Get(-1.0) got %v, wanted no such key: -1.0", err)
}
}

Expand All @@ -465,24 +465,24 @@ func TestStringIfaceMapGet(t *testing.T) {
t.Errorf("nestedVal.Get(1) got %v, wanted -1.0", floatVal)
}
err := mapVal.Get(String("absent"))
if !IsError(err) || err.(*Err).Error() != "no such key: absent" {
t.Errorf("mapVal.Get('absent') got %v, wanted no such key: absent.", err)
if !IsError(err) || err.(*Err).Error() != "no such key: \"absent\"" {
t.Errorf("mapVal.Get('absent') got %v, wanted no such key: \"absent\".", err)
}
err = nestedVal.Get(String("bad_key"))
if !IsError(err) || err.(*Err).Error() != "no such key: bad_key" {
t.Errorf("nestedVal.Get('bad_key') got %v, no such key: bad_key", err)
if !IsError(err) || err.(*Err).Error() != "no such key: \"bad_key\"" {
t.Errorf("nestedVal.Get('bad_key') got %v, no such key: \"bad_key\"", err)
}
empty, ok := mapVal.Get(String("empty")).(traits.Mapper)
if !ok {
t.Fatalf("mapVal.Get('empty') got %v, wanted empty map", mapVal.Get(String("empty")))
}
err = empty.Get(String("hello"))
if !IsError(err) || err.(*Err).Error() != "no such key: hello" {
t.Errorf("empty.Get('hello') got %v, wanted no such key: hello", err)
if !IsError(err) || err.(*Err).Error() != "no such key: \"hello\"" {
t.Errorf("empty.Get('hello') got %v, wanted no such key: \"hello\"", err)
}
err = empty.Get(Double(-1.0))
if !IsError(err) || err.(*Err).Error() != "no such key: -1" {
t.Errorf("empty.Get(-1.0) got %v, wanted no such key: -1", err)
if !IsError(err) || err.(*Err).Error() != "no such key: -1.0" {
t.Errorf("empty.Get(-1.0) got %v, wanted no such key: -1.0", err)
}
}

Expand Down Expand Up @@ -520,24 +520,24 @@ func TestRefValMapGet(t *testing.T) {
t.Errorf("nestedVal.Get(1) got %v, wanted -1.0", floatVal)
}
err := mapVal.Get(String("absent"))
if !IsError(err) || err.(*Err).Error() != "no such key: absent" {
if !IsError(err) || err.(*Err).Error() != "no such key: \"absent\"" {
t.Errorf("mapVal.Get('absent') got %v, wanted no such key: absent.", err)
}
err = nestedVal.Get(String("bad_key"))
if !IsError(err) || err.(*Err).Error() != "no such key: bad_key" {
t.Errorf("nestedVal.Get('bad_key') got %v, wanted no such key: bad_key.", err)
if !IsError(err) || err.(*Err).Error() != "no such key: \"bad_key\"" {
t.Errorf("nestedVal.Get('bad_key') got %v, wanted no such key: \"bad_key\".", err)
}
empty, ok := mapVal.Get(String("empty")).(traits.Mapper)
if !ok {
t.Fatalf("mapVal.Get('empty') got %v, wanted empty map", mapVal.Get(String("empty")))
}
err = empty.Get(String("hello"))
if !IsError(err) || err.(*Err).Error() != "no such key: hello" {
t.Errorf("empty.Get('hello') got %v, wanted no such key: hello", err)
if !IsError(err) || err.(*Err).Error() != "no such key: \"hello\"" {
t.Errorf("empty.Get('hello') got %v, wanted no such key: \"hello\"", err)
}
err = empty.Get(Double(-1.0))
if !IsError(err) || err.(*Err).Error() != "no such key: -1" {
t.Errorf("empty.Get(-1.0) got %v, wanted no such key: -1", err)
if !IsError(err) || err.(*Err).Error() != "no such key: -1.0" {
t.Errorf("empty.Get(-1.0) got %v, wanted no such key: -1.0", err)
}
}

Expand Down Expand Up @@ -842,7 +842,7 @@ func TestProtoMapString(t *testing.T) {
}
reg := newTestRegistry(t)
m := reg.NativeToValue(strMap)
want := `{hello: world}`
want := `{"hello": "world"}`
if fmt.Sprintf("%v", m) != want {
t.Errorf("map.String() got %v, wanted %v", m, want)
}
Expand Down
4 changes: 4 additions & 0 deletions common/types/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ func (n Null) Type() ref.Type {
func (n Null) Value() any {
return structpb.NullValue_NULL_VALUE
}

func (n Null) String() string {
return "null"
}
Loading