🔧 SetProperty
Sets top-level properties of composer.json, such as project name, description, type, keywords, license, etc.
When to use
Use this when you need to initialize or modify project metadata in code. This method performs type-safe assignment against a whitelist of properties, avoiding hand-written JSON. For keys inside the config section, use SetConfig instead.
Signature
go
func (c *Composer) SetProperty(property string, value interface{}) errorParameters
| Parameter | Type | Description |
|---|---|---|
property | string | Property name, see the table below |
value | interface{} | Property value; the type must match the property (silently ignored on mismatch) |
Supported properties and their value types:
| Property | Value type |
|---|---|
name | string (vendor/package) |
description | string |
type | string (library/project/metapackage, etc.) |
keywords | []string |
homepage | string (URL) |
license | interface{} (string or array of strings) |
minimum-stability | string (stable/RC/beta/alpha/dev) |
prefer-stable | bool |
Return value
error: returned on read/write failure, or when the property name is not on the whitelist (returns anunsupported propertyerror).
Example
go
package main
import (
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatal(err)
}
if err := comp.SetProperty("name", "myvendor/mypackage"); err != nil {
log.Fatal(err)
}
if err := comp.SetProperty("description", "An awesome PHP library"); err != nil {
log.Fatal(err)
}
if err := comp.SetProperty("keywords", []string{"php", "library", "awesome"}); err != nil {
log.Fatal(err)
}
if err := comp.SetProperty("license", "MIT"); err != nil {
log.Fatal(err)
}
if err := comp.SetProperty("prefer-stable", true); err != nil {
log.Fatal(err)
}
}Advanced
- ⚠️ Property names not listed return
unsupported property; when thevaluetype assertion fails (e.g. passing aninttoname), the method silently skips it without reporting an error, so the caller must ensure the type is correct. - 🔗 For keys inside the
configsection, useSetConfig; for repositories, stability, and similar settings, use dedicated methods such asSetMinimumStability,SetPreferredInstall. - 📖 For the underlying structure, see
ComposerJSON(read-composer-json).