| 1 |
// Code generated by ent, DO NOT EDIT. |
| 2 |
|
| 3 |
package ent |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
"fmt" |
| 8 |
|
| 9 |
"entgo.io/contrib/entgql" |
| 10 |
"github.com/99designs/gqlgen/graphql" |
| 11 |
"github.com/google/uuid" |
| 12 |
"github.com/hashicorp/go-multierror" |
| 13 |
"github.com/in-toto/archivista/ent/attestation" |
| 14 |
"github.com/in-toto/archivista/ent/attestationcollection" |
| 15 |
"github.com/in-toto/archivista/ent/attestationpolicy" |
| 16 |
"github.com/in-toto/archivista/ent/dsse" |
| 17 |
"github.com/in-toto/archivista/ent/payloaddigest" |
| 18 |
"github.com/in-toto/archivista/ent/signature" |
| 19 |
"github.com/in-toto/archivista/ent/statement" |
| 20 |
"github.com/in-toto/archivista/ent/subject" |
| 21 |
"github.com/in-toto/archivista/ent/subjectdigest" |
| 22 |
"github.com/in-toto/archivista/ent/timestamp" |
| 23 |
) |
| 24 |
|
| 25 |
// Noder wraps the basic Node method. |
| 26 |
type Noder interface { |
| 27 |
IsNode() |
| 28 |
} |
| 29 |
|
| 30 |
var attestationImplementors = []string{"Attestation", "Node"} |
| 31 |
|
| 32 |
// IsNode implements the Node interface check for GQLGen. |
| 33 |
func (*Attestation) IsNode() {} |
| 34 |
|
| 35 |
var attestationcollectionImplementors = []string{"AttestationCollection", "Node"} |
| 36 |
|
| 37 |
// IsNode implements the Node interface check for GQLGen. |
| 38 |
func (*AttestationCollection) IsNode() {} |
| 39 |
|
| 40 |
var attestationpolicyImplementors = []string{"AttestationPolicy", "Node"} |
| 41 |
|
| 42 |
// IsNode implements the Node interface check for GQLGen. |
| 43 |
func (*AttestationPolicy) IsNode() {} |
| 44 |
|
| 45 |
var dsseImplementors = []string{"Dsse", "Node"} |
| 46 |
|
| 47 |
// IsNode implements the Node interface check for GQLGen. |
| 48 |
func (*Dsse) IsNode() {} |
| 49 |
|
| 50 |
var payloaddigestImplementors = []string{"PayloadDigest", "Node"} |
| 51 |
|
| 52 |
// IsNode implements the Node interface check for GQLGen. |
| 53 |
func (*PayloadDigest) IsNode() {} |
| 54 |
|
| 55 |
var signatureImplementors = []string{"Signature", "Node"} |
| 56 |
|
| 57 |
// IsNode implements the Node interface check for GQLGen. |
| 58 |
func (*Signature) IsNode() {} |
| 59 |
|
| 60 |
var statementImplementors = []string{"Statement", "Node"} |
| 61 |
|
| 62 |
// IsNode implements the Node interface check for GQLGen. |
| 63 |
func (*Statement) IsNode() {} |
| 64 |
|
| 65 |
var subjectImplementors = []string{"Subject", "Node"} |
| 66 |
|
| 67 |
// IsNode implements the Node interface check for GQLGen. |
| 68 |
func (*Subject) IsNode() {} |
| 69 |
|
| 70 |
var subjectdigestImplementors = []string{"SubjectDigest", "Node"} |
| 71 |
|
| 72 |
// IsNode implements the Node interface check for GQLGen. |
| 73 |
func (*SubjectDigest) IsNode() {} |
| 74 |
|
| 75 |
var timestampImplementors = []string{"Timestamp", "Node"} |
| 76 |
|
| 77 |
// IsNode implements the Node interface check for GQLGen. |
| 78 |
func (*Timestamp) IsNode() {} |
| 79 |
|
| 80 |
var errNodeInvalidID = &NotFoundError{"node"} |
| 81 |
|
| 82 |
// NodeOption allows configuring the Noder execution using functional options. |
| 83 |
type NodeOption func(*nodeOptions) |
| 84 |
|
| 85 |
// WithNodeType sets the node Type resolver function (i.e. the table to query). |
| 86 |
// If was not provided, the table will be derived from the universal-id |
| 87 |
// configuration as described in: https://entgo.io/docs/migrate/#universal-ids. |
| 88 |
func WithNodeType(f func(context.Context, uuid.UUID) (string, error)) NodeOption { |
| 89 |
return func(o *nodeOptions) { |
| 90 |
o.nodeType = f |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// WithFixedNodeType sets the Type of the node to a fixed value. |
| 95 |
func WithFixedNodeType(t string) NodeOption { |
| 96 |
return WithNodeType(func(context.Context, uuid.UUID) (string, error) { |
| 97 |
return t, nil |
| 98 |
}) |
| 99 |
} |
| 100 |
|
| 101 |
type nodeOptions struct { |
| 102 |
nodeType func(context.Context, uuid.UUID) (string, error) |
| 103 |
} |
| 104 |
|
| 105 |
func (c *Client) newNodeOpts(opts []NodeOption) *nodeOptions { |
| 106 |
nopts := &nodeOptions{} |
| 107 |
for _, opt := range opts { |
| 108 |
opt(nopts) |
| 109 |
} |
| 110 |
if nopts.nodeType == nil { |
| 111 |
nopts.nodeType = func(ctx context.Context, id uuid.UUID) (string, error) { |
| 112 |
return "", fmt.Errorf("cannot resolve noder (%v) without its type", id) |
| 113 |
} |
| 114 |
} |
| 115 |
return nopts |
| 116 |
} |
| 117 |
|
| 118 |
// Noder returns a Node by its id. If the NodeType was not provided, it will |
| 119 |
// be derived from the id value according to the universal-id configuration. |
| 120 |
// |
| 121 |
// c.Noder(ctx, id) |
| 122 |
// c.Noder(ctx, id, ent.WithNodeType(typeResolver)) |
| 123 |
func (c *Client) Noder(ctx context.Context, id uuid.UUID, opts ...NodeOption) (_ Noder, err error) { |
| 124 |
defer func() { |
| 125 |
if IsNotFound(err) { |
| 126 |
err = multierror.Append(err, entgql.ErrNodeNotFound(id)) |
| 127 |
} |
| 128 |
}() |
| 129 |
table, err := c.newNodeOpts(opts).nodeType(ctx, id) |
| 130 |
if err != nil { |
| 131 |
return nil, err |
| 132 |
} |
| 133 |
return c.noder(ctx, table, id) |
| 134 |
} |
| 135 |
|
| 136 |
func (c *Client) noder(ctx context.Context, table string, id uuid.UUID) (Noder, error) { |
| 137 |
switch table { |
| 138 |
case attestation.Table: |
| 139 |
query := c.Attestation.Query(). |
| 140 |
Where(attestation.ID(id)) |
| 141 |
if fc := graphql.GetFieldContext(ctx); fc != nil { |
| 142 |
if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, attestationImplementors...); err != nil { |
| 143 |
return nil, err |
| 144 |
} |
| 145 |
} |
| 146 |
return query.Only(ctx) |
| 147 |
case attestationcollection.Table: |
| 148 |
query := c.AttestationCollection.Query(). |
| 149 |
Where(attestationcollection.ID(id)) |
| 150 |
if fc := graphql.GetFieldContext(ctx); fc != nil { |
| 151 |
if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, attestationcollectionImplementors...); err != nil { |
| 152 |
return nil, err |
| 153 |
} |
| 154 |
} |
| 155 |
return query.Only(ctx) |
| 156 |
case attestationpolicy.Table: |
| 157 |
query := c.AttestationPolicy.Query(). |
| 158 |
Where(attestationpolicy.ID(id)) |
| 159 |
if fc := graphql.GetFieldContext(ctx); fc != nil { |
| 160 |
if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, attestationpolicyImplementors...); err != nil { |
| 161 |
return nil, err |
| 162 |
} |
| 163 |
} |
| 164 |
return query.Only(ctx) |
| 165 |
case dsse.Table: |
| 166 |
query := c.Dsse.Query(). |
| 167 |
Where(dsse.ID(id)) |
| 168 |
if fc := graphql.GetFieldContext(ctx); fc != nil { |
| 169 |
if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, dsseImplementors...); err != nil { |
| 170 |
return nil, err |
| 171 |
} |
| 172 |
} |
| 173 |
return query.Only(ctx) |
| 174 |
case payloaddigest.Table: |
| 175 |
query := c.PayloadDigest.Query(). |
| 176 |
Where(payloaddigest.ID(id)) |
| 177 |
if fc := graphql.GetFieldContext(ctx); fc != nil { |
| 178 |
if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, payloaddigestImplementors...); err != nil { |
| 179 |
return nil, err |
| 180 |
} |
| 181 |
} |
| 182 |
return query.Only(ctx) |
| 183 |
case signature.Table: |
| 184 |
query := c.Signature.Query(). |
| 185 |
Where(signature.ID(id)) |
| 186 |
if fc := graphql.GetFieldContext(ctx); fc != nil { |
| 187 |
if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, signatureImplementors...); err != nil { |
| 188 |
return nil, err |
| 189 |
} |
| 190 |
} |
| 191 |
return query.Only(ctx) |
| 192 |
case statement.Table: |
| 193 |
query := c.Statement.Query(). |
| 194 |
Where(statement.ID(id)) |
| 195 |
if fc := graphql.GetFieldContext(ctx); fc != nil { |
| 196 |
if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, statementImplementors...); err != nil { |
| 197 |
return nil, err |
| 198 |
} |
| 199 |
} |
| 200 |
return query.Only(ctx) |
| 201 |
case subject.Table: |
| 202 |
query := c.Subject.Query(). |
| 203 |
Where(subject.ID(id)) |
| 204 |
if fc := graphql.GetFieldContext(ctx); fc != nil { |
| 205 |
if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, subjectImplementors...); err != nil { |
| 206 |
return nil, err |
| 207 |
} |
| 208 |
} |
| 209 |
return query.Only(ctx) |
| 210 |
case subjectdigest.Table: |
| 211 |
query := c.SubjectDigest.Query(). |
| 212 |
Where(subjectdigest.ID(id)) |
| 213 |
if fc := graphql.GetFieldContext(ctx); fc != nil { |
| 214 |
if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, subjectdigestImplementors...); err != nil { |
| 215 |
return nil, err |
| 216 |
} |
| 217 |
} |
| 218 |
return query.Only(ctx) |
| 219 |
case timestamp.Table: |
| 220 |
query := c.Timestamp.Query(). |
| 221 |
Where(timestamp.ID(id)) |
| 222 |
if fc := graphql.GetFieldContext(ctx); fc != nil { |
| 223 |
if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, timestampImplementors...); err != nil { |
| 224 |
return nil, err |
| 225 |
} |
| 226 |
} |
| 227 |
return query.Only(ctx) |
| 228 |
default: |
| 229 |
return nil, fmt.Errorf("cannot resolve noder from table %q: %w", table, errNodeInvalidID) |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
func (c *Client) Noders(ctx context.Context, ids []uuid.UUID, opts ...NodeOption) ([]Noder, error) { |
| 234 |
switch len(ids) { |
| 235 |
case 1: |
| 236 |
noder, err := c.Noder(ctx, ids[0], opts...) |
| 237 |
if err != nil { |
| 238 |
return nil, err |
| 239 |
} |
| 240 |
return []Noder{noder}, nil |
| 241 |
case 0: |
| 242 |
return []Noder{}, nil |
| 243 |
} |
| 244 |
|
| 245 |
noders := make([]Noder, len(ids)) |
| 246 |
errors := make([]error, len(ids)) |
| 247 |
tables := make(map[string][]uuid.UUID) |
| 248 |
id2idx := make(map[uuid.UUID][]int, len(ids)) |
| 249 |
nopts := c.newNodeOpts(opts) |
| 250 |
for i, id := range ids { |
| 251 |
table, err := nopts.nodeType(ctx, id) |
| 252 |
if err != nil { |
| 253 |
errors[i] = err |
| 254 |
continue |
| 255 |
} |
| 256 |
tables[table] = append(tables[table], id) |
| 257 |
id2idx[id] = append(id2idx[id], i) |
| 258 |
} |
| 259 |
|
| 260 |
for table, ids := range tables { |
| 261 |
nodes, err := c.noders(ctx, table, ids) |
| 262 |
if err != nil { |
| 263 |
for _, id := range ids { |
| 264 |
for _, idx := range id2idx[id] { |
| 265 |
errors[idx] = err |
| 266 |
} |
| 267 |
} |
| 268 |
} else { |
| 269 |
for i, id := range ids { |
| 270 |
for _, idx := range id2idx[id] { |
| 271 |
noders[idx] = nodes[i] |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
for i, id := range ids { |
| 278 |
if errors[i] == nil { |
| 279 |
if noders[i] != nil { |
| 280 |
continue |
| 281 |
} |
| 282 |
errors[i] = entgql.ErrNodeNotFound(id) |
| 283 |
} else if IsNotFound(errors[i]) { |
| 284 |
errors[i] = multierror.Append(errors[i], entgql.ErrNodeNotFound(id)) |
| 285 |
} |
| 286 |
ctx := graphql.WithPathContext(ctx, |
| 287 |
graphql.NewPathWithIndex(i), |
| 288 |
) |
| 289 |
graphql.AddError(ctx, errors[i]) |
| 290 |
} |
| 291 |
return noders, nil |
| 292 |
} |
| 293 |
|
| 294 |
func (c *Client) noders(ctx context.Context, table string, ids []uuid.UUID) ([]Noder, error) { |
| 295 |
noders := make([]Noder, len(ids)) |
| 296 |
idmap := make(map[uuid.UUID][]*Noder, len(ids)) |
| 297 |
for i, id := range ids { |
| 298 |
idmap[id] = append(idmap[id], &noders[i]) |
| 299 |
} |
| 300 |
switch table { |
| 301 |
case attestation.Table: |
| 302 |
query := c.Attestation.Query(). |
| 303 |
Where(attestation.IDIn(ids...)) |
| 304 |
query, err := query.CollectFields(ctx, attestationImplementors...) |
| 305 |
if err != nil { |
| 306 |
return nil, err |
| 307 |
} |
| 308 |
nodes, err := query.All(ctx) |
| 309 |
if err != nil { |
| 310 |
return nil, err |
| 311 |
} |
| 312 |
for _, node := range nodes { |
| 313 |
for _, noder := range idmap[node.ID] { |
| 314 |
*noder = node |
| 315 |
} |
| 316 |
} |
| 317 |
case attestationcollection.Table: |
| 318 |
query := c.AttestationCollection.Query(). |
| 319 |
Where(attestationcollection.IDIn(ids...)) |
| 320 |
query, err := query.CollectFields(ctx, attestationcollectionImplementors...) |
| 321 |
if err != nil { |
| 322 |
return nil, err |
| 323 |
} |
| 324 |
nodes, err := query.All(ctx) |
| 325 |
if err != nil { |
| 326 |
return nil, err |
| 327 |
} |
| 328 |
for _, node := range nodes { |
| 329 |
for _, noder := range idmap[node.ID] { |
| 330 |
*noder = node |
| 331 |
} |
| 332 |
} |
| 333 |
case attestationpolicy.Table: |
| 334 |
query := c.AttestationPolicy.Query(). |
| 335 |
Where(attestationpolicy.IDIn(ids...)) |
| 336 |
query, err := query.CollectFields(ctx, attestationpolicyImplementors...) |
| 337 |
if err != nil { |
| 338 |
return nil, err |
| 339 |
} |
| 340 |
nodes, err := query.All(ctx) |
| 341 |
if err != nil { |
| 342 |
return nil, err |
| 343 |
} |
| 344 |
for _, node := range nodes { |
| 345 |
for _, noder := range idmap[node.ID] { |
| 346 |
*noder = node |
| 347 |
} |
| 348 |
} |
| 349 |
case dsse.Table: |
| 350 |
query := c.Dsse.Query(). |
| 351 |
Where(dsse.IDIn(ids...)) |
| 352 |
query, err := query.CollectFields(ctx, dsseImplementors...) |
| 353 |
if err != nil { |
| 354 |
return nil, err |
| 355 |
} |
| 356 |
nodes, err := query.All(ctx) |
| 357 |
if err != nil { |
| 358 |
return nil, err |
| 359 |
} |
| 360 |
for _, node := range nodes { |
| 361 |
for _, noder := range idmap[node.ID] { |
| 362 |
*noder = node |
| 363 |
} |
| 364 |
} |
| 365 |
case payloaddigest.Table: |
| 366 |
query := c.PayloadDigest.Query(). |
| 367 |
Where(payloaddigest.IDIn(ids...)) |
| 368 |
query, err := query.CollectFields(ctx, payloaddigestImplementors...) |
| 369 |
if err != nil { |
| 370 |
return nil, err |
| 371 |
} |
| 372 |
nodes, err := query.All(ctx) |
| 373 |
if err != nil { |
| 374 |
return nil, err |
| 375 |
} |
| 376 |
for _, node := range nodes { |
| 377 |
for _, noder := range idmap[node.ID] { |
| 378 |
*noder = node |
| 379 |
} |
| 380 |
} |
| 381 |
case signature.Table: |
| 382 |
query := c.Signature.Query(). |
| 383 |
Where(signature.IDIn(ids...)) |
| 384 |
query, err := query.CollectFields(ctx, signatureImplementors...) |
| 385 |
if err != nil { |
| 386 |
return nil, err |
| 387 |
} |
| 388 |
nodes, err := query.All(ctx) |
| 389 |
if err != nil { |
| 390 |
return nil, err |
| 391 |
} |
| 392 |
for _, node := range nodes { |
| 393 |
for _, noder := range idmap[node.ID] { |
| 394 |
*noder = node |
| 395 |
} |
| 396 |
} |
| 397 |
case statement.Table: |
| 398 |
query := c.Statement.Query(). |
| 399 |
Where(statement.IDIn(ids...)) |
| 400 |
query, err := query.CollectFields(ctx, statementImplementors...) |
| 401 |
if err != nil { |
| 402 |
return nil, err |
| 403 |
} |
| 404 |
nodes, err := query.All(ctx) |
| 405 |
if err != nil { |
| 406 |
return nil, err |
| 407 |
} |
| 408 |
for _, node := range nodes { |
| 409 |
for _, noder := range idmap[node.ID] { |
| 410 |
*noder = node |
| 411 |
} |
| 412 |
} |
| 413 |
case subject.Table: |
| 414 |
query := c.Subject.Query(). |
| 415 |
Where(subject.IDIn(ids...)) |
| 416 |
query, err := query.CollectFields(ctx, subjectImplementors...) |
| 417 |
if err != nil { |
| 418 |
return nil, err |
| 419 |
} |
| 420 |
nodes, err := query.All(ctx) |
| 421 |
if err != nil { |
| 422 |
return nil, err |
| 423 |
} |
| 424 |
for _, node := range nodes { |
| 425 |
for _, noder := range idmap[node.ID] { |
| 426 |
*noder = node |
| 427 |
} |
| 428 |
} |
| 429 |
case subjectdigest.Table: |
| 430 |
query := c.SubjectDigest.Query(). |
| 431 |
Where(subjectdigest.IDIn(ids...)) |
| 432 |
query, err := query.CollectFields(ctx, subjectdigestImplementors...) |
| 433 |
if err != nil { |
| 434 |
return nil, err |
| 435 |
} |
| 436 |
nodes, err := query.All(ctx) |
| 437 |
if err != nil { |
| 438 |
return nil, err |
| 439 |
} |
| 440 |
for _, node := range nodes { |
| 441 |
for _, noder := range idmap[node.ID] { |
| 442 |
*noder = node |
| 443 |
} |
| 444 |
} |
| 445 |
case timestamp.Table: |
| 446 |
query := c.Timestamp.Query(). |
| 447 |
Where(timestamp.IDIn(ids...)) |
| 448 |
query, err := query.CollectFields(ctx, timestampImplementors...) |
| 449 |
if err != nil { |
| 450 |
return nil, err |
| 451 |
} |
| 452 |
nodes, err := query.All(ctx) |
| 453 |
if err != nil { |
| 454 |
return nil, err |
| 455 |
} |
| 456 |
for _, node := range nodes { |
| 457 |
for _, noder := range idmap[node.ID] { |
| 458 |
*noder = node |
| 459 |
} |
| 460 |
} |
| 461 |
default: |
| 462 |
return nil, fmt.Errorf("cannot resolve noders from table %q: %w", table, errNodeInvalidID) |
| 463 |
} |
| 464 |
return noders, nil |
| 465 |
} |