Skip to content

Commit

Permalink
feat: implement an apache adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean committed Oct 11, 2024
1 parent cb56ecb commit 0299e1b
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions bridge/apache_bridge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed 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 bridge

import (
"fmt"
"io"
"reflect"
"unsafe"

"github.com/cloudwego/gopkg/bufiox"
"github.com/cloudwego/gopkg/protocol/thrift"
)

func ApacheReadBridge(iprot interface{}, readFunc func(b []byte) (int, error)) error {
var br bufiox.Reader
fieldNames := []string{"br", "trans"}
for _, fn := range fieldNames {
reader, exist, err := getUnexportField(iprot, fn)
if err != nil {
return err
}
if exist {
switch reader.(type) {

Check failure on line 36 in bridge/apache_bridge.go

View workflow job for this annotation

GitHub Actions / lint

S1034: assigning the result of this type assertion to a variable (switch reader := reader.(type)) could eliminate type assertions in switch cases (gosimple)
case bufiox.Reader:
br = reader.(bufiox.Reader)

Check failure on line 38 in bridge/apache_bridge.go

View workflow job for this annotation

GitHub Actions / lint

S1034(related information): could eliminate this type assertion (gosimple)
case io.Reader:
br = bufiox.NewDefaultReader(reader.(io.Reader))

Check failure on line 40 in bridge/apache_bridge.go

View workflow job for this annotation

GitHub Actions / lint

S1034(related information): could eliminate this type assertion (gosimple)
default:
return fmt.Errorf("reader not ok")
}
break
}
}
buf, err := thrift.NewSkipDecoder(br).Next(thrift.STRUCT)
if err != nil {
return err
}
_, err = readFunc(buf)
return err
}

func ApacheWriteBridge(oprot interface{}, bufFunc func() []byte) error {
var bw bufiox.Writer
fieldNames := []string{"bw", "trans"}
for _, fn := range fieldNames {
writer, exist, err := getUnexportField(oprot, fn)
if err != nil {
return err
}
if exist {
switch writer.(type) {

Check failure on line 64 in bridge/apache_bridge.go

View workflow job for this annotation

GitHub Actions / lint

S1034: assigning the result of this type assertion to a variable (switch writer := writer.(type)) could eliminate type assertions in switch cases (gosimple)
case bufiox.Writer:
bw = writer.(bufiox.Writer)

Check failure on line 66 in bridge/apache_bridge.go

View workflow job for this annotation

GitHub Actions / lint

S1034(related information): could eliminate this type assertion (gosimple)
case io.Writer:
bw = bufiox.NewDefaultWriter(writer.(io.Writer))
default:
return fmt.Errorf("writer not ok")
}
break
}
}
_, err := bw.WriteBinary(bufFunc())
if err != nil {
return err
}
return bw.Flush()
}

func getUnexportField(p interface{}, fieldName string) (value interface{}, ok bool, error error) {
if reflect.TypeOf(p).Kind() != reflect.Ptr {
return nil, false, fmt.Errorf("%s is not a ptr", p)
}
field := reflect.ValueOf(p).Elem().FieldByName(fieldName)
if field.IsValid() {
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface(), true, nil
}
return nil, false, nil
}

0 comments on commit 0299e1b

Please sign in to comment.