github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/queue/helper.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package queue
     7  
     8  import (
     9  	"reflect"
    10  
    11  	"github.com/gitbundle/modules/json"
    12  )
    13  
    14  // Mappable represents an interface that can MapTo another interface
    15  type Mappable interface {
    16  	MapTo(v interface{}) error
    17  }
    18  
    19  // toConfig will attempt to convert a given configuration cfg into the provided exemplar type.
    20  //
    21  // It will tolerate the cfg being passed as a []byte or string of a json representation of the
    22  // exemplar or the correct type of the exemplar itself
    23  func toConfig(exemplar, cfg interface{}) (interface{}, error) {
    24  	// First of all check if we've got the same type as the exemplar - if so it's all fine.
    25  	if reflect.TypeOf(cfg).AssignableTo(reflect.TypeOf(exemplar)) {
    26  		return cfg, nil
    27  	}
    28  
    29  	// Now if not - does it provide a MapTo function we can try?
    30  	if mappable, ok := cfg.(Mappable); ok {
    31  		newVal := reflect.New(reflect.TypeOf(exemplar))
    32  		if err := mappable.MapTo(newVal.Interface()); err == nil {
    33  			return newVal.Elem().Interface(), nil
    34  		}
    35  		// MapTo has failed us ... let's try the json route ...
    36  	}
    37  
    38  	// OK we've been passed a byte array right?
    39  	configBytes, ok := cfg.([]byte)
    40  	if !ok {
    41  		// oh ... it's a string then?
    42  		var configStr string
    43  
    44  		configStr, ok = cfg.(string)
    45  		configBytes = []byte(configStr)
    46  	}
    47  	if !ok {
    48  		// hmm ... can we marshal it to json?
    49  		var err error
    50  		configBytes, err = json.Marshal(cfg)
    51  		ok = err == nil
    52  	}
    53  	if !ok {
    54  		// no ... we've tried hard enough at this point - throw an error!
    55  		return nil, ErrInvalidConfiguration{cfg: cfg}
    56  	}
    57  
    58  	// OK unmarshal the byte array into a new copy of the exemplar
    59  	newVal := reflect.New(reflect.TypeOf(exemplar))
    60  	if err := json.Unmarshal(configBytes, newVal.Interface()); err != nil {
    61  		// If we can't unmarshal it then return an error!
    62  		return nil, ErrInvalidConfiguration{cfg: cfg, err: err}
    63  	}
    64  	return newVal.Elem().Interface(), nil
    65  }
    66  
    67  // unmarshalAs will attempt to unmarshal provided bytes as the provided exemplar
    68  func unmarshalAs(bs []byte, exemplar interface{}) (data Data, err error) {
    69  	if exemplar != nil {
    70  		t := reflect.TypeOf(exemplar)
    71  		n := reflect.New(t)
    72  		ne := n.Elem()
    73  		err = json.Unmarshal(bs, ne.Addr().Interface())
    74  		data = ne.Interface().(Data)
    75  	} else {
    76  		err = json.Unmarshal(bs, &data)
    77  	}
    78  	return
    79  }
    80  
    81  // assignableTo will check if provided data is assignable to the same type as the exemplar
    82  // if the provided exemplar is nil then it will always return true
    83  func assignableTo(data Data, exemplar interface{}) bool {
    84  	if exemplar == nil {
    85  		return true
    86  	}
    87  
    88  	// Assert data is of same type as exemplar
    89  	t := reflect.TypeOf(data)
    90  	exemplarType := reflect.TypeOf(exemplar)
    91  
    92  	return t.AssignableTo(exemplarType) && data != nil
    93  }