本文整理匯總了Golang中cloud/google/com/go/trace.FromContext函數的典型用法代碼示例。如果您正苦於以下問題:Golang FromContext函數的具體用法?Golang FromContext怎麽用?Golang FromContext使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了FromContext函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ByUser
func (s *SpaceService) ByUser(ctx context.Context, u *User) ([]*Space, error) {
span := trace.FromContext(ctx).NewChild("trythings.space.ByUser")
defer span.Finish()
var sps []*Space
_, err := datastore.NewQuery("Space").
Ancestor(datastore.NewKey(ctx, "Root", "root", 0, nil)).
Filter("UserIDs =", u.ID).
GetAll(ctx, &sps)
if err != nil {
return nil, err
}
var ac []*Space
for _, sp := range sps {
ok, err := s.IsVisible(ctx, sp)
if err != nil {
// TODO use multierror
return nil, err
}
if ok {
ac = append(ac, sp)
}
}
return ac, nil
}
示例2: RunAll
func (s *MigrationService) RunAll(ctx context.Context) error {
span := trace.FromContext(ctx).NewChild("trythings.migration.RunAll")
defer span.Finish()
su, err := IsSuperuser(ctx)
if err != nil {
return err
}
if !su {
return errors.New("must run migrations as superuser")
}
latest, err := s.latestVersion(ctx)
if err != nil {
return err
}
log.Infof(ctx, "running all migrations. latest is %s", latest)
for _, m := range migrations {
if m.Version.After(latest) {
log.Infof(ctx, "running migration version %s", m.Version)
err = s.run(ctx, m)
if err != nil {
return err
}
}
}
return nil
}
示例3: BySpace
func (s *ViewService) BySpace(ctx context.Context, sp *Space) ([]*View, error) {
span := trace.FromContext(ctx).NewChild("trythings.view.BySpace")
defer span.Finish()
var vs []*View
_, err := datastore.NewQuery("View").
Ancestor(datastore.NewKey(ctx, "Root", "root", 0, nil)).
Filter("SpaceID =", sp.ID).
GetAll(ctx, &vs)
if err != nil {
return nil, err
}
var ac []*View
for _, v := range vs {
ok, err := s.IsVisible(ctx, v)
if err != nil {
// TODO use multierror
return nil, err
}
if ok {
ac = append(ac, v)
}
}
return ac, nil
}
示例4: ByID
func (s *TaskService) ByID(ctx context.Context, id string) (*Task, error) {
rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
k := datastore.NewKey(ctx, "Task", id, 0, rootKey)
ct, ok := CacheFromContext(ctx).Get(k).(*Task)
if ok {
return ct, nil
}
span := trace.FromContext(ctx).NewChild("trythings.task.ByID")
defer span.Finish()
var t Task
err := datastore.Get(ctx, k, &t)
if err != nil {
return nil, err
}
ok, err = s.IsVisible(ctx, &t)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("cannot access task")
}
CacheFromContext(ctx).Set(k, &t)
return &t, nil
}
示例5: Update
func (s *SearchService) Update(ctx context.Context, se *Search) error {
span := trace.FromContext(ctx).NewChild("trythings.search.Update")
defer span.Finish()
if se.ID == "" {
return errors.New("cannot update search with no ID")
}
// Make sure we have access to the search before it was modified.
_, err := s.ByID(ctx, se.ID)
if err != nil {
return err
}
// Make sure we continue to have access to the task after our update.
ok, err := s.IsVisible(ctx, se)
if err != nil {
return err
}
if !ok {
return errors.New("cannot update search to lose access")
}
rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
k := datastore.NewKey(ctx, "Search", se.ID, 0, rootKey)
_, err = datastore.Put(ctx, k, se)
if err != nil {
return err
}
CacheFromContext(ctx).Set(k, se)
return nil
}
示例6: ByIDs
// ByIDs filters out Tasks that are not visible to the current User.
func (s *TaskService) ByIDs(ctx context.Context, ids []string) ([]*Task, error) {
span := trace.FromContext(ctx).NewChild("trythings.task.ByIDs")
defer span.Finish()
rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
ks := []*datastore.Key{}
for _, id := range ids {
ks = append(ks, datastore.NewKey(ctx, "Task", id, 0, rootKey))
}
var allTasks = make([]*Task, len(ks))
err := datastore.GetMulti(ctx, ks, allTasks)
if err != nil {
return nil, err
}
ts := []*Task{}
for _, t := range allTasks {
// TODO#Perf: Batch the isVisible check.
ok, err := s.IsVisible(ctx, t)
if err != nil {
return nil, err
}
if !ok {
continue
}
ts = append(ts, t)
}
return ts, nil
}
示例7: Create
func (s *UserService) Create(ctx context.Context, u *User) error {
span := trace.FromContext(ctx).NewChild("trythings.user.Create")
defer span.Finish()
// TODO Make sure u.GoogleID == user.Current(ctx).ID
if u.ID != "" {
return fmt.Errorf("u already has id %q", u.ID)
}
if u.CreatedAt.IsZero() {
u.CreatedAt = time.Now()
}
id, _, err := datastore.AllocateIDs(ctx, "User", nil, 1)
if err != nil {
return err
}
u.ID = fmt.Sprintf("%x", id)
rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
k := datastore.NewKey(ctx, "User", u.ID, 0, rootKey)
k, err = datastore.Put(ctx, k, u)
if err != nil {
return err
}
return nil
}
示例8: ByView
func (s *SearchService) ByView(ctx context.Context, v *View) ([]*Search, error) {
span := trace.FromContext(ctx).NewChild("trythings.search.ByView")
defer span.Finish()
var ss []*Search
_, err := datastore.NewQuery("Search").
Ancestor(datastore.NewKey(ctx, "Root", "root", 0, nil)).
Filter("ViewID =", v.ID).
Order("ViewRank").
GetAll(ctx, &ss)
if err != nil {
return nil, err
}
var ac []*Search
for _, se := range ss {
ok, err := s.IsVisible(ctx, se)
if err != nil {
// TODO use multierror
return nil, err
}
if ok {
ac = append(ac, se)
}
}
return ac, nil
}
示例9: ByID
func (s *ViewService) ByID(ctx context.Context, id string) (*View, error) {
rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
k := datastore.NewKey(ctx, "View", id, 0, rootKey)
cv, ok := CacheFromContext(ctx).Get(k).(*View)
if ok {
return cv, nil
}
span := trace.FromContext(ctx).NewChild("trythings.view.ByID")
defer span.Finish()
var v View
err := datastore.Get(ctx, k, &v)
if err != nil {
return nil, err
}
ok, err = s.IsVisible(ctx, &v)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("cannot access view")
}
CacheFromContext(ctx).Set(k, &v)
return &v, nil
}
示例10: ByID
func (s *SpaceService) ByID(ctx context.Context, id string) (*Space, error) {
rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
k := datastore.NewKey(ctx, "Space", id, 0, rootKey)
csp, ok := CacheFromContext(ctx).Get(k).(*Space)
if ok {
return csp, nil
}
span := trace.FromContext(ctx).NewChild("trythings.space.ByID")
defer span.Finish()
var sp Space
err := datastore.Get(ctx, k, &sp)
if err != nil {
return nil, err
}
ok, err = s.IsVisible(ctx, &sp)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("cannot access space")
}
CacheFromContext(ctx).Set(k, &sp)
return &sp, nil
}
示例11: GetGoogleUser
func GetGoogleUser(ctx context.Context, idToken string) (*GoogleUser, error) {
span := trace.FromContext(ctx).NewChild("trythings.google_user.GetGoogleUser")
defer span.Finish()
tok, err := jwt.ParseSigned(idToken)
if err != nil {
return nil, err
}
if len(tok.Headers) != 1 {
// We must have a header to specify a kid.
// We don't know how to handle multiple headers,
// since it's unclear which kid to use.
return nil, errors.New("expected exactly one token header")
}
keys := googleKeys.Key(tok.Headers[0].KeyID)
if len(keys) == 0 {
err := updateGoogleKeys(ctx)
if err != nil {
return nil, err
}
keys = googleKeys.Key(tok.Headers[0].KeyID)
}
if len(keys) != 1 {
// We must have a key to check the signature.
// We don't know how to deal with multiple keys matching the same kid.
return nil, errors.New("expected exactly one key matching kid")
}
key := keys[0]
var payload struct {
jwt.Claims
GoogleUser
}
err = tok.Claims(&payload, key.Key)
if err != nil {
return nil, err
}
expectedIssuer := "accounts.google.com"
if strings.HasPrefix(payload.Issuer, "https://") {
expectedIssuer = "https://accounts.google.com"
}
err = payload.Validate(jwt.Expected{
Issuer: expectedIssuer,
Audience: []string{"695504958192-8k3tf807271m7jcllcvlauddeqhbr0hg.apps.googleusercontent.com"},
Time: time.Now(),
})
if err != nil {
return nil, err
}
return &payload.GoogleUser, nil
}
示例12: FromContext
// FromContext should not be subject to access control,
// because it would create a circular dependency.
func (s *UserService) FromContext(ctx context.Context) (*User, error) {
span := trace.FromContext(ctx).NewChild("trythings.user.FromContext")
defer span.Finish()
gu, ok := GoogleUserFromContext(ctx)
if !ok {
return nil, errors.New("expected google user, probably missing Authorization header")
}
return s.byGoogleID(ctx, gu.ID)
}
示例13: Index
func (s *TaskService) Index(ctx context.Context, t *Task) error {
span := trace.FromContext(ctx).NewChild("trythings.task.Index")
defer span.Finish()
index, err := search.Open("Task")
if err != nil {
return err
}
_, err = index.Put(ctx, t.ID, t)
if err != nil {
return err
}
return nil
}
示例14: Create
func (s *TaskService) Create(ctx context.Context, t *Task) error {
span := trace.FromContext(ctx).NewChild("trythings.task.Create")
defer span.Finish()
if t.ID != "" {
return fmt.Errorf("t already has id %q", t.ID)
}
if t.CreatedAt.IsZero() {
t.CreatedAt = time.Now()
}
if t.SpaceID == "" {
return errors.New("SpaceID is required")
}
ok, err := s.IsVisible(ctx, t)
if err != nil {
return err
}
if !ok {
return errors.New("cannot access space to create task")
}
id, _, err := datastore.AllocateIDs(ctx, "Task", nil, 1)
if err != nil {
return err
}
t.ID = fmt.Sprintf("%x", id)
rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
k := datastore.NewKey(ctx, "Task", t.ID, 0, rootKey)
k, err = datastore.Put(ctx, k, t)
if err != nil {
return err
}
err = s.Index(ctx, t)
if err != nil {
return err
}
return nil
}
示例15: Create
func (s *ViewService) Create(ctx context.Context, v *View) error {
span := trace.FromContext(ctx).NewChild("trythings.view.Create")
defer span.Finish()
if v.ID != "" {
return fmt.Errorf("v already has id %q", v.ID)
}
if v.CreatedAt.IsZero() {
v.CreatedAt = time.Now()
}
if v.Name == "" {
return errors.New("Name is required")
}
if v.SpaceID == "" {
return errors.New("SpaceID is required")
}
ok, err := s.IsVisible(ctx, v)
if err != nil {
return err
}
if !ok {
return errors.New("cannot access space to create view")
}
id, _, err := datastore.AllocateIDs(ctx, "View", nil, 1)
if err != nil {
return err
}
v.ID = fmt.Sprintf("%x", id)
rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
k := datastore.NewKey(ctx, "View", v.ID, 0, rootKey)
k, err = datastore.Put(ctx, k, v)
if err != nil {
return err
}
return nil
}