github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/indexer/test/helpers.go (about)

     1  // Copyright 2018-2022 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package test
    20  
    21  import (
    22  	"errors"
    23  	"os"
    24  	"path"
    25  	"reflect"
    26  	"strings"
    27  )
    28  
    29  // CreateTmpDir creates a temporary dir for tests data.
    30  func CreateTmpDir() (string, error) {
    31  	name, err := os.MkdirTemp("/tmp", "testfiles-")
    32  	if err != nil {
    33  		return "", err
    34  	}
    35  
    36  	return name, nil
    37  }
    38  
    39  // ValueOf gets the value of a type v on a given field <field>.
    40  func ValueOf(v interface{}, field string) string {
    41  	r := reflect.ValueOf(v)
    42  	f := reflect.Indirect(r).FieldByName(field)
    43  
    44  	return f.String()
    45  }
    46  
    47  func getType(v interface{}) (reflect.Value, error) {
    48  	rv := reflect.ValueOf(v)
    49  	for rv.Kind() == reflect.Ptr || rv.Kind() == reflect.Interface {
    50  		rv = rv.Elem()
    51  	}
    52  	if !rv.IsValid() {
    53  		return reflect.Value{}, errors.New("failed to read value via reflection")
    54  	}
    55  
    56  	return rv, nil
    57  }
    58  
    59  // GetTypeFQN formats a valid name from a type <t>. This is a duplication of the already existing function in the
    60  // indexer package, but since there is a circular dependency we chose to duplicate it.
    61  func GetTypeFQN(t interface{}) string {
    62  	typ, _ := getType(t)
    63  	typeName := path.Join(typ.Type().PkgPath(), typ.Type().Name())
    64  	typeName = strings.ReplaceAll(typeName, "/", ".")
    65  	return typeName
    66  }