SetAttrValues sets multi-valued attribute.
(key string, attrs []string)
| 1532 | |
| 1533 | // SetAttrValues sets multi-valued attribute. |
| 1534 | func (o *Object) SetAttrValues(key string, attrs []string) error { |
| 1535 | ctx := context.TODO() // TODO: make it possible to get a context via Object; either new context field, or via some "ImportRun" field? |
| 1536 | |
| 1537 | exists := asSet(o.Attrs(key)) |
| 1538 | actual := asSet(attrs) |
| 1539 | o.mu.Lock() |
| 1540 | defer o.mu.Unlock() |
| 1541 | // add new values |
| 1542 | for v := range actual { |
| 1543 | if exists[v] { |
| 1544 | delete(exists, v) |
| 1545 | continue |
| 1546 | } |
| 1547 | _, err := o.h.upload(ctx, schema.NewAddAttributeClaim(o.pn, key, v)) |
| 1548 | if err != nil { |
| 1549 | return err |
| 1550 | } |
| 1551 | } |
| 1552 | // delete unneeded values |
| 1553 | for v := range exists { |
| 1554 | _, err := o.h.upload(ctx, schema.NewDelAttributeClaim(o.pn, key, v)) |
| 1555 | if err != nil { |
| 1556 | return err |
| 1557 | } |
| 1558 | } |
| 1559 | if o.attr == nil { |
| 1560 | o.attr = make(map[string][]string) |
| 1561 | } |
| 1562 | o.attr[key] = attrs |
| 1563 | return nil |
| 1564 | } |
| 1565 | |
| 1566 | func asSet(elts []string) map[string]bool { |
| 1567 | if len(elts) == 0 { |
no test coverage detected