github.com/octohelm/cuemod@v0.9.4/pkg/cueify/golang/func_method.go (about)

     1  package golang
     2  
     3  import "go/types"
     4  
     5  var stringInterfaces = map[string]method{
     6  	"MarshalText": {
     7  		Results: []string{"[]byte", "error"},
     8  	},
     9  	"UnmarshalText": {
    10  		PtrRecv: true,
    11  		Params:  []string{"[]byte"},
    12  		Results: []string{"error"},
    13  	},
    14  }
    15  
    16  var topInterfaces = map[string]method{
    17  	"MarshalJSON": {
    18  		Results: []string{"[]byte", "error"},
    19  	},
    20  	"UnmarshalJSON": {
    21  		PtrRecv: true,
    22  		Params:  []string{"[]byte"},
    23  		Results: []string{"error"},
    24  	},
    25  	"MarshalYAML": {
    26  		Results: []string{"interface{}", "error"},
    27  	},
    28  	"UnmarshalYAML": {
    29  		PtrRecv: true,
    30  		Params:  []string{"interface{}"},
    31  		Results: []string{"error"},
    32  	},
    33  }
    34  
    35  type method struct {
    36  	PtrRecv bool
    37  	Params  []string
    38  	Results []string
    39  }
    40  
    41  func (m *method) Equal(fn *types.Func) bool {
    42  	s := fn.Type().(*types.Signature)
    43  
    44  	if m.PtrRecv {
    45  		if _, ok := s.Recv().Type().(*types.Pointer); !ok {
    46  			return false
    47  		}
    48  	}
    49  
    50  	params := s.Params()
    51  	results := s.Results()
    52  
    53  	paramsLen := params.Len()
    54  	resultsLen := results.Len()
    55  
    56  	if paramsLen != len(m.Params) || resultsLen != len(m.Results) {
    57  		return false
    58  	}
    59  
    60  	for i := 0; i < paramsLen; i++ {
    61  		if params.At(i).Type().String() != m.Params[i] {
    62  			return false
    63  		}
    64  	}
    65  
    66  	for i := 0; i < resultsLen; i++ {
    67  		if results.At(i).Type().String() != m.Results[i] {
    68  			return false
    69  		}
    70  	}
    71  
    72  	return true
    73  }