Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(go): decode a repeated JSON key as Python does, and treat null as a no-op

Adeshina Lasisi1c6b691c704d
diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml
index 2d3f05ac..3287884b 100644
--- a/.github/workflows/go.yml
+++ b/.github/workflows/go.yml
@@ -129,7 +129,7 @@ jobs:
# case fails here, and adding one means raising this number.
- name: No test was skipped
if: ${{ !cancelled() }}
- run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test.json" --min-tests 528
+ run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test.json" --min-tests 530
# go.mod declares go 1.22, so that is what a consumer may build with.
- name: Run the tests on the oldest supported Go
@@ -141,7 +141,7 @@ jobs:
run: go test -count=1 -json ./... > "$RUNNER_TEMP/go-test-oldest.json"
- name: No test was skipped on the oldest Go either
if: ${{ !cancelled() }}
- run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test-oldest.json" --min-tests 528
+ run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test-oldest.json" --min-tests 530
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
if: ${{ failure() }}
diff --git a/laya-go/lang/object_test.go b/laya-go/lang/object_test.go
index 5c74667d..94d80a22 100644
--- a/laya-go/lang/object_test.go
+++ b/laya-go/lang/object_test.go
@@ -50,6 +50,43 @@ func TestObjectUnmarshalJSONRefusesWhatIsNotAnObject(t *testing.T) {
}
}
+// A repeated key keeps its first position and takes its last value, as Python's json.loads
+// does; keeping both members would read text Python never sees and can flip the verdict.
+func TestObjectUnmarshalJSONRepeatedKeyIsLastWins(t *testing.T) {
+ var o Object
+ doc := `{"a":"Ich kann mich nicht anmelden bitte hilfe","b":"please help me with my card refund today","a":"ok"}`
+ if err := json.Unmarshal([]byte(doc), &o); err != nil {
+ t.Fatal(err)
+ }
+ if len(o) != 2 || o[0].Key != "a" || o[0].Value != "ok" || o[1].Key != "b" {
+ t.Fatalf("members = %#v", o)
+ }
+ if got := StateText(o); got != "ok please help me with my card refund today" {
+ t.Errorf("StateText = %q", got)
+ }
+ if !IsEnglish(o) {
+ t.Error("Python reads this state as English")
+ }
+}
+
+// null is a no-op, as encoding/json treats it for its own types; trailing data is refused.
+func TestObjectUnmarshalJSONNullAndTrailingData(t *testing.T) {
+ o := Object{{Key: "k", Value: "kept"}}
+ if err := json.Unmarshal([]byte(`null`), &o); err != nil || len(o) != 1 {
+ t.Errorf("null: err %v, members %v", err, o)
+ }
+ var holder struct{ State Object }
+ if err := json.Unmarshal([]byte(`{"State":null}`), &holder); err != nil || holder.State != nil {
+ t.Errorf("null field: err %v, state %v", err, holder.State)
+ }
+ var direct Object
+ for _, doc := range []string{`{} x`, `{"a":[1}`, `{"a":"b"} {}`} {
+ if err := direct.UnmarshalJSON([]byte(doc)); err == nil {
+ t.Errorf("%s must be refused", doc)
+ }
+ }
+}
+
func TestScriptConstantsAreTheNamesPythonUses(t *testing.T) {
for script, text := range map[Script]string{
ScriptLatin: "hello", ScriptHan: "你好吗", ScriptDevanagari: "नमस्ते", ScriptCyrillic: "привет",
diff --git a/laya-go/lang/state.go b/laya-go/lang/state.go
index d3bc4246..a1c435c4 100644
--- a/laya-go/lang/state.go
+++ b/laya-go/lang/state.go
@@ -4,6 +4,7 @@ import (
"bytes"
"cmp"
"encoding/json"
+ "errors"
"fmt"
"io"
"reflect"
@@ -38,9 +39,14 @@ type Member struct {
// key order instead.
type Object []Member
-// UnmarshalJSON decodes a JSON object keeping its key order. Nested objects become Objects and
-// arrays become []any; numbers become float64, which a state ignores anyway.
+// UnmarshalJSON decodes a JSON object keeping its key order, as Python's json.loads does: a
+// repeated key keeps its first position and takes its last value. Nested objects become Objects
+// and arrays become []any; numbers become float64, which a state ignores anyway. JSON null leaves
+// the Object unchanged, as encoding/json does for its own types.
func (o *Object) UnmarshalJSON(data []byte) error {
+ if string(bytes.TrimSpace(data)) == "null" {
+ return nil
+ }
dec := json.NewDecoder(bytes.NewReader(data))
tok, err := dec.Token()
if err != nil {
@@ -53,6 +59,9 @@ func (o *Object) UnmarshalJSON(data []byte) error {
if err != nil {
return err
}
+ if _, err := dec.Token(); err != io.EOF {
+ return errors.New("lang.Object: data after the JSON object")
+ }
*o = v
return nil
}
@@ -60,6 +69,7 @@ func (o *Object) UnmarshalJSON(data []byte) error {
// decodeObject reads members up to the closing brace of an object whose opening brace was read.
func decodeObject(dec *json.Decoder) (Object, error) {
obj := Object{}
+ at := map[string]int{} // each key's position, so a repeated key overwrites in place
for dec.More() {
keyTok, err := dec.Token()
if err != nil {
@@ -69,7 +79,13 @@ func decodeObject(dec *json.Decoder) (Object, error) {
if err != nil {
return nil, err
}
- obj = append(obj, Member{keyTok.(string), value})
+ key := keyTok.(string)
+ if i, seen := at[key]; seen {
+ obj[i].Value = value
+ continue
+ }
+ at[key] = len(obj)
+ obj = append(obj, Member{key, value})
}
if _, err := dec.Token(); err != nil {
return nil, err
@@ -97,7 +113,7 @@ func decodeValue(dec *json.Decoder) (any, error) {
}
list = append(list, v)
}
- if _, err := dec.Token(); err != nil && err != io.EOF {
+ if _, err := dec.Token(); err != nil {
return nil, err
}
return list, nil