github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/idutil/test/getter.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package test
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  
    11  	"github.com/keybase/client/go/kbfs/idutil"
    12  	kbname "github.com/keybase/client/go/kbun"
    13  	"github.com/keybase/client/go/protocol/keybase1"
    14  )
    15  
    16  // NormalizedUsernameGetter is a simple map of IDs -> usernames that
    17  // can be useful for testing.
    18  type NormalizedUsernameGetter map[keybase1.UserOrTeamID]kbname.NormalizedUsername
    19  
    20  var _ idutil.NormalizedUsernameGetter = NormalizedUsernameGetter{}
    21  
    22  // GetNormalizedUsername implements the idutil.NormalizedUsernameGetter
    23  // interface for NormalizedUsernameGetter.
    24  func (g NormalizedUsernameGetter) GetNormalizedUsername(
    25  	ctx context.Context, id keybase1.UserOrTeamID,
    26  	_ keybase1.OfflineAvailability) (kbname.NormalizedUsername, error) {
    27  	name, ok := g[id]
    28  	if !ok {
    29  		return kbname.NormalizedUsername(""),
    30  			idutil.NoSuchUserError{Input: fmt.Sprintf("uid:%s", id)}
    31  	}
    32  	return name, nil
    33  }
    34  
    35  // UIDMap converts this getter into a typed map of user IDs ->
    36  // normalized names.
    37  func (g NormalizedUsernameGetter) UIDMap() map[keybase1.UserOrTeamID]kbname.NormalizedUsername {
    38  	return (map[keybase1.UserOrTeamID]kbname.NormalizedUsername)(g)
    39  }