MCPcopy Create free account
hub / github.com/dolanor/rip / updatePathID

Function updatePathID

handler.go:172–288  ·  view source on GitHub ↗
(urlPath, method string, f updateFunc[Ent], get getFunc[Ent], options *RouteOptions)

Source from the content-addressed store, hash-verified

170}
171
172func updatePathID[Ent any](urlPath, method string, f updateFunc[Ent], get getFunc[Ent], options *RouteOptions) http.HandlerFunc {
173 return func(w http.ResponseWriter, r *http.Request) {
174 //TODO add edit mode on
175 id, field, _, contentType, accept, _, err := getIDAndEditMode(w, r, method, urlPath, options)
176 if err != nil {
177 writeError(w, accept, err, options)
178 return
179 }
180
181 var ent Ent
182 if field == "" {
183 // if we have no field selected, we just decode the entire entity
184 ent, err = decode[Ent](r.Body, contentType, options)
185 if err != nil {
186 writeError(w, accept, fmt.Errorf("bad input format: %w", err), options)
187 return
188 }
189 } else {
190 err = func() (err error) {
191 defer func() {
192 rerr := recover()
193 if rerr != nil {
194 err = Error{
195 Source: ErrorSource{
196 Pointer: field,
197 },
198 Debug: fmt.Errorf("%v: %w", rerr, err).Error(),
199 }
200 }
201 }()
202
203 ent, err = get(r.Context(), id)
204 if err != nil {
205 writeError(w, accept, fmt.Errorf("can not get original entity: %w", err), options)
206 return err
207 }
208
209 st := structOf(ent)
210
211 fieldValue := st.FieldByNameFunc(func(s string) bool {
212 return strings.ToLower(s) == strings.ToLower(field)
213 })
214
215 if !fieldValue.CanSet() {
216 fieldValue = fieldValue.Elem()
217 log.Println("fvalue can set: false")
218 if fieldValue.CanSet() {
219 log.Println("fvalue can set: true again")
220 }
221 }
222
223 var fieldData any
224
225 decoder, err := encoding.ContentTypeDecoder(r.Body, contentType, options.codecs)
226 if err != nil {
227 return err
228 }
229

Callers 1

handleEntityWithPathFunction · 0.85

Calls 12

ContentTypeDecoderFunction · 0.92
GetIDFunction · 0.92
SetIDFunction · 0.92
AcceptEncoderFunction · 0.92
getIDAndEditModeFunction · 0.85
writeErrorFunction · 0.85
structOfFunction · 0.85
fFunction · 0.85
StringMethod · 0.80
DecodeMethod · 0.65
EncodeMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected