...

Source file src/golang.conradwood.net/go-easyops/ctx/shared/to_string.go

Documentation: golang.conradwood.net/go-easyops/ctx/shared

     1  package shared
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	ge "golang.conradwood.net/apis/goeasyops"
     8  )
     9  
    10  func LocalState2string(ls LocalState) string {
    11  	if isNil(ls) {
    12  		return "[no localstate]"
    13  	}
    14  	s := "LocalState:\n"
    15  	s = s + fmt.Sprintf("  User            : %s\n", UserIDString(ls.User()))
    16  	s = s + fmt.Sprintf("  Sudo-User       : %s\n", UserIDString(ls.SudoUser()))
    17  	s = s + fmt.Sprintf("  CreatorService  : %s\n", UserIDString(ls.CreatorService()))
    18  	s = s + fmt.Sprintf("  CallingService  : %s\n", UserIDString(ls.CallingService()))
    19  	s = s + fmt.Sprintf("  Services        : %s\n", comma_delimited(ls.Services(), func(x *ge.ServiceTrace) string { return x.ID }))
    20  	s = s + fmt.Sprintf("  Experiments     : %s\n", comma_delimited(ls.Experiments(), func(x *ge.Experiment) string { return x.Name }))
    21  	s = s + fmt.Sprintf("  Debug           : %v\n", ls.Debug())
    22  	s = s + fmt.Sprintf("  Trace           : %v\n", ls.Trace())
    23  	s = s + fmt.Sprintf("  RequestID       : %s\n", ls.RequestID())
    24  	s = s + fmt.Sprintf("  RoutingTags     : %v\n", ls.RoutingTags())
    25  	s = s + fmt.Sprintf("  AuthTags        : %v\n", ls.AuthTags())
    26  
    27  	return s
    28  
    29  }
    30  func ContextProto2string(prefix string, x *ge.InContext) string {
    31  	s := "InContext:\n"
    32  	s = s + "  Immutable:\n"
    33  	s = s + Imctx2string("    ", x.ImCtx)
    34  	s = s + "\n  Mutable:\n"
    35  	s = s + Mctx2string("    ", x.MCtx)
    36  	return AddPrefixToLines(prefix, s)
    37  }
    38  func Imctx2string(prefix string, x *ge.ImmutableContext) string {
    39  	s := ""
    40  	s = s + fmt.Sprintf("RequestID      : %s\n", x.RequestID)
    41  	s = s + fmt.Sprintf("User           : %s\n", UserIDString(x.User))
    42  	s = s + fmt.Sprintf("SudoUser       : %s\n", UserIDString(x.SudoUser))
    43  	s = s + fmt.Sprintf("CreatorService : %s", UserIDString(x.CreatorService))
    44  	return AddPrefixToLines(prefix, s)
    45  }
    46  func Mctx2string(prefix string, x *ge.MutableContext) string {
    47  	s := ""
    48  	s = s + fmt.Sprintf("CallingService : %s\n", UserIDString(x.CallingService))
    49  	s = s + fmt.Sprintf("Serviceids     : %d\n", len(x.ServiceIDs))
    50  	s = s + fmt.Sprintf("Debug          : %v\n", x.Debug)
    51  	s = s + fmt.Sprintf("Trace          : %v\n", x.Trace)
    52  	tag_s := ""
    53  	if x.Tags != nil {
    54  		tag_s = "Got tags"
    55  	}
    56  	s = s + fmt.Sprintf("Tags           : %v\n", tag_s)
    57  	s = s + fmt.Sprintf("Experiments    : %s", comma_delimited(x.Experiments, func(x *ge.Experiment) string { return x.Name }))
    58  	return AddPrefixToLines(prefix, s)
    59  }
    60  
    61  func comma_delimited[K interface{}](objects []K, f func(K) string) string {
    62  	deli := ""
    63  	xx := ""
    64  	if len(objects) > 0 {
    65  		xx = ": "
    66  	}
    67  	res := fmt.Sprintf("%d%s", len(objects), xx)
    68  	for _, x := range objects {
    69  		xs := f(x)
    70  		res = res + deli + fmt.Sprintf("\"%s\"", xs)
    71  		deli = ", "
    72  	}
    73  	return res
    74  }
    75  
    76  func AddPrefixToLines(prefix, txt string) string {
    77  	res := ""
    78  	for _, line := range strings.Split(txt, "\n") {
    79  		nl := prefix + line + "\n"
    80  		res = res + nl
    81  	}
    82  	return strings.TrimSuffix(res, "\n")
    83  }
    84  

View as plain text