Codebase list golang-github-go-kit-kit / 99f1fbe
examples/profilesvc: fix PUT and PATCH semantics Peter Bourgon 8 years ago
1 changed file(s) with 28 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
5555 s.mtx.Lock()
5656 defer s.mtx.Unlock()
5757 if _, ok := s.m[p.ID]; ok {
58 return errAlreadyExists
58 return errAlreadyExists // POST = create, don't overwrite
5959 }
6060 s.m[p.ID] = p
6161 return nil
7777 }
7878 s.mtx.Lock()
7979 defer s.mtx.Unlock()
80 if _, ok := s.m[id]; ok {
81 return errAlreadyExists
82 }
83 s.m[id] = p
80 s.m[id] = p // PUT = create or update
8481 return nil
8582 }
8683
8784 func (s *inmemService) PatchProfile(ctx context.Context, id string, p Profile) error {
88 return s.PutProfile(ctx, id, p) // perhaps more granular behavior is needed here
85 if p.ID != "" && id != p.ID {
86 return errInconsistentIDs
87 }
88
89 s.mtx.Lock()
90 defer s.mtx.Unlock()
91
92 existing, ok := s.m[id]
93 if !ok {
94 return errNotFound // PATCH = update existing, don't create
95 }
96
97 // We assume that it's not possible to PATCH the ID, and that it's not
98 // possible to PATCH any field to its zero value. That is, the zero value
99 // means not specified. The way around this is to use e.g. Name *string in
100 // the Profile definition. But since this is just a demonstrative example,
101 // I'm leaving that out.
102
103 if p.Name != "" {
104 existing.Name = p.Name
105 }
106 if len(p.Addresses) > 0 {
107 existing.Addresses = p.Addresses
108 }
109 s.m[id] = existing
110 return nil
89111 }
90112
91113 func (s *inmemService) DeleteProfile(ctx context.Context, id string) error {