| 196 | } |
| 197 | |
| 198 | func (m *Mapper) FromTuple(ctx context.Context, ts ...*ketoapi.RelationTuple) (res []*RelationTuple, err error) { |
| 199 | ctx, span := trace.SpanFromContext(ctx).TracerProvider().Tracer("keto/internal/relationtuple").Start(ctx, "Mapper.FromTuple") |
| 200 | defer otelx.End(span, &err) |
| 201 | |
| 202 | onSuccess := newSuccess(&err) |
| 203 | defer onSuccess.apply() |
| 204 | |
| 205 | res = make([]*RelationTuple, 0, len(ts)) |
| 206 | s := make([]string, 0, len(ts)*2) |
| 207 | var u []uuid.UUID |
| 208 | |
| 209 | nm, err := m.D.Config(ctx).NamespaceManager() |
| 210 | if err != nil { |
| 211 | return nil, err |
| 212 | } |
| 213 | |
| 214 | for _, t := range ts { |
| 215 | t := t |
| 216 | n, err := nm.GetNamespaceByName(ctx, t.Namespace) |
| 217 | if err != nil { |
| 218 | return nil, err |
| 219 | } |
| 220 | mt := RelationTuple{ |
| 221 | Namespace: n.Name, |
| 222 | Relation: t.Relation, |
| 223 | } |
| 224 | i := len(res) |
| 225 | |
| 226 | if err := t.Validate(); err != nil { |
| 227 | return nil, err |
| 228 | } |
| 229 | if t.SubjectID != nil { |
| 230 | s = append(s, *t.SubjectID) |
| 231 | onSuccess.do(func() { |
| 232 | mt.Subject = &SubjectID{u[i*2]} |
| 233 | }) |
| 234 | } else if t.SubjectSet != nil { |
| 235 | n, err := nm.GetNamespaceByName(ctx, t.SubjectSet.Namespace) |
| 236 | if err != nil { |
| 237 | return nil, err |
| 238 | } |
| 239 | s = append(s, t.SubjectSet.Object) |
| 240 | onSuccess.do(func() { |
| 241 | mt.Subject = &SubjectSet{ |
| 242 | Namespace: n.Name, |
| 243 | Object: u[i*2], |
| 244 | Relation: t.SubjectSet.Relation, |
| 245 | } |
| 246 | }) |
| 247 | } |
| 248 | |
| 249 | s = append(s, t.Object) |
| 250 | onSuccess.do(func() { |
| 251 | mt.Object = u[i*2+1] |
| 252 | }) |
| 253 | |
| 254 | res = append(res, &mt) |
| 255 | } |