// Copyright (c) 2019-2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package fxreflect

import (
	
	
	
	
	
	
)

// Match from beginning of the line until the first `vendor/` (non-greedy)
var vendorRe = regexp.MustCompile("^.*?/vendor/")

// sanitize makes the function name suitable for logging display. It removes
// url-encoded elements from the `dot.git` package names and shortens the
// vendored paths.
func sanitize( string) string {
	// Use the stdlib to un-escape any package import paths which can happen
	// in the case of the "dot-git" postfix. Seems like a bug in stdlib =/
	if ,  := url.QueryUnescape();  == nil {
		 = 
	}

	// strip everything prior to the vendor
	return vendorRe.ReplaceAllString(, "vendor/")
}

// Caller returns the formatted calling func name
func () string {
	return CallerStack(1, 0).CallerName()
}

// FuncName returns a funcs formatted name
func ( interface{}) string {
	 := reflect.ValueOf()
	if .Kind() != reflect.Func {
		return fmt.Sprint()
	}

	 := runtime.FuncForPC(.Pointer()).Name()
	return fmt.Sprintf("%s()", sanitize())
}

// Ascend the call stack until we leave the Fx production code. This allows us
// to avoid hard-coding a frame skip, which makes this code work well even
// when it's wrapped.
func shouldIgnoreFrame( Frame) bool {
	// Treat test files as leafs.
	if strings.Contains(.File, "_test.go") {
		return false
	}

	// The unique, fully-qualified name for all functions begins with
	// "{{importPath}}.". We'll ignore Fx and its subpackages.
	 := strings.TrimPrefix(.Function, "go.uber.org/fx")
	if len() > 0 && [0] == '.' || [0] == '/' {
		// We want to match,
		//   go.uber.org/fx.Foo
		//   go.uber.org/fx/something.Foo
		// But not, go.uber.org/fxfoo
		return true
	}

	return false
}