String encodes the flake reference as a URL-like string. It normalizes the result such that if two Ref values are equal, then their strings will also be equal. There are multiple ways to encode a flake reference. String uses the following rules to normalize the result: - the URL-like form is alway
()
| 349 | // If r is missing a type or has any invalid fields, String returns an empty |
| 350 | // string. |
| 351 | func (r Ref) String() string { |
| 352 | switch r.Type { |
| 353 | case TypeFile: |
| 354 | if r.URL == "" { |
| 355 | return "" |
| 356 | } |
| 357 | |
| 358 | url, err := url.Parse("file+" + r.URL) |
| 359 | if err != nil { |
| 360 | // This should be rare and only happen if the caller |
| 361 | // messed with the parsed URL. |
| 362 | return "" |
| 363 | } |
| 364 | url.RawQuery = appendQueryString(url.Query(), |
| 365 | "lastModified", itoaOmitZero(r.LastModified), |
| 366 | "narHash", r.NARHash, |
| 367 | ) |
| 368 | return url.String() |
| 369 | case TypeGit: |
| 370 | if r.URL == "" { |
| 371 | return "" |
| 372 | } |
| 373 | if !strings.HasPrefix(r.URL, "git") { |
| 374 | r.URL = "git+" + r.URL |
| 375 | } |
| 376 | |
| 377 | // Nix removes "ref" and "rev" from the query string |
| 378 | // (but not other parameters) after parsing. If they're empty, |
| 379 | // we can skip parsing the URL. Otherwise, we need to add them |
| 380 | // back. |
| 381 | if r.Ref == "" && r.Rev == "" { |
| 382 | return r.URL |
| 383 | } |
| 384 | url, err := url.Parse(r.URL) |
| 385 | if err != nil { |
| 386 | // This should be rare and only happen if the caller |
| 387 | // messed with the parsed URL. |
| 388 | return "" |
| 389 | } |
| 390 | url.RawQuery = appendQueryString(url.Query(), "ref", r.Ref, "rev", r.Rev, "dir", r.Dir) |
| 391 | return url.String() |
| 392 | case TypeGitHub: |
| 393 | if r.Owner == "" || r.Repo == "" { |
| 394 | return "" |
| 395 | } |
| 396 | url := &url.URL{ |
| 397 | Scheme: "github", |
| 398 | Opaque: buildEscapedPath(r.Owner, r.Repo, cmp.Or(r.Rev, r.Ref)), |
| 399 | RawQuery: appendQueryString(nil, |
| 400 | "host", r.Host, |
| 401 | "dir", r.Dir, |
| 402 | "lastModified", itoaOmitZero(r.LastModified), |
| 403 | "narHash", r.NARHash, |
| 404 | ), |
| 405 | } |
| 406 | return url.String() |
| 407 | case TypeIndirect: |
| 408 | if r.ID == "" { |