github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/credentials/anonymous.go (about) 1 package credentials 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack" 8 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring" 9 ) 10 11 var ( 12 _ Credentials = (*Anonymous)(nil) 13 _ fmt.Stringer = (*Anonymous)(nil) 14 _ AnonymousCredentialsOption = SourceInfoOption("") 15 ) 16 17 type AnonymousCredentialsOption interface { 18 ApplyAnonymousCredentialsOption(c *Anonymous) 19 } 20 21 // Anonymous implements Credentials interface with Anonymous access 22 type Anonymous struct { 23 sourceInfo string 24 } 25 26 func NewAnonymousCredentials(opts ...AnonymousCredentialsOption) *Anonymous { 27 c := &Anonymous{ 28 sourceInfo: stack.Record(1), 29 } 30 for _, opt := range opts { 31 if opt != nil { 32 opt.ApplyAnonymousCredentialsOption(c) 33 } 34 } 35 36 return c 37 } 38 39 // Token implements Credentials. 40 func (c Anonymous) Token(_ context.Context) (string, error) { 41 return "", nil 42 } 43 44 // Token implements Credentials. 45 func (c Anonymous) String() string { 46 buffer := xstring.Buffer() 47 defer buffer.Free() 48 buffer.WriteString("Anonymous{") 49 if c.sourceInfo != "" { 50 buffer.WriteString("From:") 51 fmt.Fprintf(buffer, "%q", c.sourceInfo) 52 } 53 buffer.WriteByte('}') 54 55 return buffer.String() 56 }