String returns a single string from a selector. It is only allowed when selecting one field.
(ctx context.Context)
| 361 | |
| 362 | // String returns a single string from a selector. It is only allowed when selecting one field. |
| 363 | func (s *selector) String(ctx context.Context) (_ string, err error) { |
| 364 | var v []string |
| 365 | if v, err = s.Strings(ctx); err != nil { |
| 366 | return |
| 367 | } |
| 368 | switch len(v) { |
| 369 | case 1: |
| 370 | return v[0], nil |
| 371 | case 0: |
| 372 | err = &NotFoundError{s.label} |
| 373 | default: |
| 374 | err = fmt.Errorf("ent: Strings returned %d results when one was expected", len(v)) |
| 375 | } |
| 376 | return |
| 377 | } |
| 378 | |
| 379 | // StringX is like String, but panics if an error occurs. |
| 380 | func (s *selector) StringX(ctx context.Context) string { |