diff --git a/.gitignore b/.gitignore index 66fd13c90..9df85c07e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Idea configuration file +.idea/ + # Binaries for programs and plugins *.exe *.exe~ diff --git a/pkg/rm/tcc/tcc_resource.go b/pkg/rm/tcc/tcc_resource.go index de2cd2dd2..8646a888f 100644 --- a/pkg/rm/tcc/tcc_resource.go +++ b/pkg/rm/tcc/tcc_resource.go @@ -19,14 +19,15 @@ package tcc import ( "context" + "encoding/json" "fmt" "sync" - "github.com/seata/seata-go/pkg/protocol/resource" - "github.com/seata/seata-go/pkg/tm" - + "github.com/seata/seata-go/pkg/common" "github.com/seata/seata-go/pkg/protocol/branch" + "github.com/seata/seata-go/pkg/protocol/resource" "github.com/seata/seata-go/pkg/rm" + "github.com/seata/seata-go/pkg/tm" ) var ( @@ -125,12 +126,22 @@ func (t *TCCResourceManager) BranchCommit(ctx context.Context, ranchType branch. } func (t *TCCResourceManager) getBusinessActionContext(xid string, branchID int64, resourceID string, applicationData []byte) tm.BusinessActionContext { + var actionContextMap = make(map[string]interface{}, 2) + if len(applicationData) > 0 { + var tccContext map[string]interface{} + if err := json.Unmarshal(applicationData, &tccContext); err != nil { + panic("application data failed to unmarshl as json") + } + if v, ok := tccContext[common.ActionContext]; ok { + actionContextMap = v.(map[string]interface{}) + } + } + return tm.BusinessActionContext{ - Xid: xid, - BranchId: branchID, - ActionName: resourceID, - // todo get ActionContext - //ActionContext:, + Xid: xid, + BranchId: branchID, + ActionName: resourceID, + ActionContext: &actionContextMap, } } diff --git a/pkg/rm/tcc/tcc_resource_test.go b/pkg/rm/tcc/tcc_resource_test.go new file mode 100644 index 000000000..f83b9a2ba --- /dev/null +++ b/pkg/rm/tcc/tcc_resource_test.go @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tcc + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestActionContext(t *testing.T) { + applicationData := `{"actionContext":{"zhangsan":"lisi"}}` + businessActionContext := GetTCCResourceManagerInstance(). + getBusinessActionContext("1111111111", 2645276141, "TestActionContext", []byte(applicationData)) + + assert.NotEmpty(t, businessActionContext) + bytes, err := json.Marshal(businessActionContext.ActionContext) + assert.Nil(t, err) + assert.Equal(t, `{"zhangsan":"lisi"}`, string(bytes)) +}