appendPackage appends a package to the packages field.
(name, version string)
| 99 | |
| 100 | // appendPackage appends a package to the packages field. |
| 101 | func (c *configAST) appendPackage(name, version string) { |
| 102 | pkgs := c.packagesField(false) |
| 103 | switch val := pkgs.Value.Value.(type) { |
| 104 | case *hujson.Object: |
| 105 | c.appendPackageToObject(val, name, version) |
| 106 | case *hujson.Array: |
| 107 | c.appendPackageToArray(val, joinNameVersion(name, version)) |
| 108 | default: |
| 109 | panic("packages field must be an object or array") |
| 110 | } |
| 111 | |
| 112 | // Ensure the packages field is on its own line. |
| 113 | if !slices.Contains(pkgs.Name.BeforeExtra, '\n') { |
| 114 | pkgs.Name.BeforeExtra = append(pkgs.Name.BeforeExtra, '\n') |
| 115 | } |
| 116 | c.root.Format() |
| 117 | } |
| 118 | |
| 119 | func (c *configAST) appendPackageToObject(pkgs *hujson.Object, name, version string) { |
| 120 | i := c.memberIndex(pkgs, name) |
no test coverage detected