wongoo opened a new pull request #309:
URL: https://github.com/apache/dubbo-go-hessian2/pull/309


   
   **What this PR does**:
   - support non-strict mode to decode object to map when unregistered
   - support encode object to class
   
   **Which issue(s) this PR fixes**:
   
   Fixes #308
   
   **Special notes for your reviewer**:
   
   **Does this PR introduce a user-facing change?**:
   ```release-note
   - support non-strict mode to decode object to map when unregistered
   ```
   
   example:
   ```go
          name := UserName{
                FirstName: "John",
                LastName:  "Doe",
        }
        person := Person{
                UserName: name,
                Age:      18,
                Sex:      true,
        }
   
        worker1 := &Worker{
                Person: person,
                CurJob: JOB{Title: "cto", Company: "facebook"},
                Jobs: []JOB{
                        {Title: "manager", Company: "google"},
                        {Title: "ceo", Company: "microsoft"},
                },
        }
   
        t.Logf("worker1: %v", worker1)
   
        e := NewEncoder()
        err := e.Encode(worker1)
        if err != nil {
                t.Fatalf("encode(worker:%#v) = error:%s", worker1, err)
        }
        data := e.Buffer()
        t.Logf("data: %s", data)
   
        // unRegisterPOJO before decode, so that to decode to map
        unRegisterPOJO(name)
        unRegisterPOJO(person)
        unRegisterPOJO(worker1)
        unRegisterPOJO(&worker1.Jobs[0])
   
        // strict mode
        d := NewDecoder(data)
        d.Strict = true // set to strict mode, then the decoding will return 
error for class not being registered.
        res, err := d.Decode()
        if err == nil {
                t.Error("after unregister pojo, decoding should return error 
for strict mode")
                t.FailNow()
        }
   
        // non-strict mode, decode to map for class not being registered.
        d = NewDecoder(data)
        res, err = d.Decode()
        if err != nil {
                t.Error(err)
                t.FailNow()
        }
        t.Logf("type of decode object:%v", reflect.TypeOf(res))
   
        worker2, ok := res.(map[string]interface{}) // the response is a map
        if !ok {
                t.Fatalf("res:%#v should be a map for non-strict mode", res)
        }
   
        t.Logf("worker2: %v", worker2)
   
        // register pojo again, so that there are class definitions, which are 
used to encode map to object.
        RegisterPOJO(name)
        RegisterPOJO(person)
        RegisterPOJO(worker1)
        RegisterPOJO(&worker1.Jobs[0])
   
        // encode the map to object again, note the worker2 is a map.
        e = NewEncoder()
        err = e.Encode(worker2)
        if err != nil {
                t.Error(err)
                t.FailNow()
        }
   
        data = e.Buffer()
        t.Logf("data: %s", data)
   
        // decode the encoded map data to struct object again.
        d = NewDecoder(data)
        res, err = d.Decode()
        if err != nil {
                t.Error(err)
                t.FailNow()
        }
        t.Logf("type of decode object:%v", reflect.TypeOf(res))
   
        worker3, ok := res.(*Worker) // worker3 is a Worker
        if !ok {
                t.Fatalf("res:%#v should be a worker type", res)
        }
   
        t.Logf("worker3: %v", worker3)
        if !reflect.DeepEqual(worker1, worker3) {
                t.Fatal("worker1 not equal to worker3!")
        }
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to