本文整理汇总了Golang中google/golang.org/cloud/trace.FromContext函数的典型用法代码示例。如果您正苦于以下问题:Golang FromContext函数的具体用法?Golang FromContext怎么用?Golang FromContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FromContext函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: 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
}
开发者ID:trythings,项目名称:trythings,代码行数:29,代码来源:search.go
示例2: 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
}
开发者ID:trythings,项目名称:trythings,代码行数:28,代码来源:space.go
示例3: 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
}
开发者ID:trythings,项目名称:trythings,代码行数:28,代码来源:view.go
示例4: 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
}
开发者ID:trythings,项目名称:trythings,代码行数:28,代码来源:view.go
示例5: 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
}
开发者ID:trythings,项目名称:trythings,代码行数:34,代码来源:task.go
示例6: 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
}
开发者ID:trythings,项目名称:trythings,代码行数:34,代码来源:search.go
示例7: 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
}
开发者ID:trythings,项目名称:trythings,代码行数:29,代码来源:task.go
示例8: ByID
func (s *SearchService) ByID(ctx context.Context, id string) (*Search, error) {
rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
k := datastore.NewKey(ctx, "Search", id, 0, rootKey)
cse, ok := CacheFromContext(ctx).Get(k).(*Search)
if ok {
return cse, nil
}
span := trace.FromContext(ctx).NewChild("trythings.search.ByID")
defer span.Finish()
var se Search
err := datastore.Get(ctx, k, &se)
if err != nil {
return nil, err
}
ok, err = s.IsVisible(ctx, &se)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("cannot access search")
}
CacheFromContext(ctx).Set(k, &se)
return &se, nil
}
开发者ID:trythings,项目名称:trythings,代码行数:28,代码来源:search.go
示例9: 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
}
开发者ID:trythings,项目名称:trythings,代码行数:29,代码来源:user.go
示例10: 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
}
开发者ID:trythings,项目名称:trythings,代码行数:30,代码来源:migration.go
示例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
}
开发者ID:trythings,项目名称:trythings,代码行数:57,代码来源:google_user.go
示例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)
}
开发者ID:trythings,项目名称:trythings,代码行数:12,代码来源:user.go
示例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
}
开发者ID:trythings,项目名称:trythings,代码行数:14,代码来源:task.go
示例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
}
开发者ID:trythings,项目名称:trythings,代码行数:45,代码来源:task.go
示例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
}
开发者ID:trythings,项目名称:trythings,代码行数:44,代码来源:view.go
示例16: Create
func (s *SpaceService) Create(ctx context.Context, sp *Space) error {
span := trace.FromContext(ctx).NewChild("trythings.space.Create")
defer span.Finish()
if sp.ID != "" {
return fmt.Errorf("sp already has id %q", sp.ID)
}
if sp.CreatedAt.IsZero() {
sp.CreatedAt = time.Now()
}
if len(sp.UserIDs) > 0 {
return errors.New("UserIDs must be empty")
}
su, err := IsSuperuser(ctx)
if err != nil {
return err
}
if !su {
u, err := s.UserService.FromContext(ctx)
if err != nil {
return err
}
sp.UserIDs = []string{u.ID}
}
id, _, err := datastore.AllocateIDs(ctx, "Space", nil, 1)
if err != nil {
return err
}
sp.ID = fmt.Sprintf("%x", id)
rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
k := datastore.NewKey(ctx, "Space", sp.ID, 0, rootKey)
k, err = datastore.Put(ctx, k, sp)
if err != nil {
return err
}
return nil
}
开发者ID:trythings,项目名称:trythings,代码行数:43,代码来源:space.go
示例17: updateGoogleKeys
func updateGoogleKeys(ctx context.Context) error {
span := trace.FromContext(ctx).NewChild("trythings.google_user.updateGoogleKeys")
defer span.Finish()
// Try to fetch new public keys from Google.
client := urlfetch.Client(ctx)
client.Timeout = 1 * time.Second
resp, err := client.Get("https://www.googleapis.com/oauth2/v3/certs")
if err != nil {
return err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&googleKeys)
if err != nil {
return err
}
return nil
}
开发者ID:trythings,项目名称:trythings,代码行数:20,代码来源:google_user.go
示例18: byGoogleID
func (s *UserService) byGoogleID(ctx context.Context, googleID string) (*User, error) {
span := trace.FromContext(ctx).NewChild("trythings.user.byGoogleID")
defer span.Finish()
var us []*User
_, err := datastore.NewQuery("User").
Ancestor(datastore.NewKey(ctx, "Root", "root", 0, nil)).
Filter("GoogleID =", googleID).
Limit(1).
GetAll(ctx, &us)
if err != nil {
return nil, err
}
if len(us) == 0 {
return nil, ErrUserNotFound
}
return us[0], nil
}
开发者ID:trythings,项目名称:trythings,代码行数:20,代码来源:user.go
示例19: Start
func (api *ViewAPI) Start() error {
api.Type = graphql.NewObject(graphql.ObjectConfig{
Name: "View",
Fields: graphql.Fields{
"id": relay.GlobalIDField("View", nil),
"createdAt": &graphql.Field{
Description: "When the view was first created.",
Type: graphql.String,
},
"name": &graphql.Field{
Description: "The name to display for the view.",
Type: graphql.String,
},
"searches": &graphql.Field{
Type: graphql.NewList(api.SearchAPI.Type),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
span := trace.FromContext(p.Context).NewChild("trythings.viewAPI.searches")
defer span.Finish()
v, ok := p.Source.(*View)
if !ok {
return nil, errors.New("expected view source")
}
ss, err := api.SearchService.ByView(p.Context, v)
if err != nil {
return nil, err
}
return ss, nil
},
},
},
Interfaces: []*graphql.Interface{
api.NodeInterface,
},
})
return nil
}
开发者ID:trythings,项目名称:trythings,代码行数:39,代码来源:view.go
示例20: Update
func (s *TaskService) Update(ctx context.Context, t *Task) error {
span := trace.FromContext(ctx).NewChild("trythings.task.Update")
defer span.Finish()
if t.ID == "" {
return errors.New("cannot update task with no ID")
}
// Make sure we have access to the task to start.
_, err := s.ByID(ctx, t.ID)
if err != nil {
return err
}
// Make sure we continue to have access to the task after our update.
ok, err := s.IsVisible(ctx, t)
if err != nil {
return err
}
if !ok {
return errors.New("cannot update task to lose access")
}
rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
k := datastore.NewKey(ctx, "Task", t.ID, 0, rootKey)
_, err = datastore.Put(ctx, k, t)
if err != nil {
return err
}
err = s.Index(ctx, t)
if err != nil {
return err
}
CacheFromContext(ctx).Set(k, t)
return nil
}
开发者ID:trythings,项目名称:trythings,代码行数:39,代码来源:task.go
注:本文中的google/golang.org/cloud/trace.FromContext函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论