{"id":25776544,"url":"https://github.com/mguymon/model-citizen","last_synced_at":"2025-02-27T06:06:41.893Z","repository":{"id":536444,"uuid":"1556938","full_name":"mguymon/model-citizen","owner":"mguymon","description":"Annotation based model factory for Java","archived":false,"fork":false,"pushed_at":"2024-02-28T18:26:54.000Z","size":1676,"stargazers_count":94,"open_issues_count":9,"forks_count":13,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-02-28T19:40:38.014Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mguymon.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2011-04-01T19:07:15.000Z","updated_at":"2024-01-13T23:50:16.000Z","dependencies_parsed_at":"2024-02-28T19:50:09.397Z","dependency_job_id":null,"html_url":"https://github.com/mguymon/model-citizen","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mguymon%2Fmodel-citizen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mguymon%2Fmodel-citizen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mguymon%2Fmodel-citizen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mguymon%2Fmodel-citizen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mguymon","download_url":"https://codeload.github.com/mguymon/model-citizen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240987435,"owners_count":19889334,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-27T06:01:26.282Z","updated_at":"2025-02-27T06:06:41.887Z","avatar_url":"https://github.com/mguymon.png","language":"Java","funding_links":[],"categories":["测试"],"sub_categories":[],"readme":"# Model Citizen\n\n[![Java CI with Maven](https://github.com/mguymon/model-citizen/actions/workflows/maven.yml/badge.svg)](https://github.com/mguymon/model-citizen/actions/workflows/maven.yml)\n\nModel Citizen is an annotation based model factory for Java, inspired by [FactoryGirl](https://github.com/thoughtbot/factory_girl)\n\nA Model is mapped by a _@Blueprint_ using annotated fields. Blueprints contain\ndefault values and references to other @Blueprinted models. The _ModelFactory_ can\ncreate models based on registered blueprints. A model already created can be\npassed into the _ModelFactory_ as a reference model, which will be used as the basis\nfor the new Model.\n\n\n\u003chttps://github.com/mguymon/model-citizen\u003e\n\n[JavaDoc](http://mguymon.github.com/model-citizen/apidocs/index.html)\n\n\n## Install\n\n    \u003cdependency\u003e\n      \u003cgroupId\u003ecom.tobedevoured.modelcitizen\u003c/groupId\u003e\n      \u003cartifactId\u003ecore\u003c/artifactId\u003e\n      \u003cversion\u003e0.8.2\u003c/version\u003e\n    \u003c/dependency\u003e\n\n## Blueprint\n\nA blueprint is a Class annotated with _@Blueprint( Class )_ and contains annotated fields. Everything else is ignored by the _ModelFactory_.\nModel Citizen [own blueprints](https://github.com/mguymon/model-citizen/tree/master/core/src/test/java/com/tobedevoured/modelcitizen/blueprint) are a great example of how they work.\n\n**@Blueprint**: Class annotation\n   * _value_: [Class] The target model class for this blueprint.\n   * _template_: [Class] The [template](https://github.com/mguymon/model-citizen/blob/master/core/src/main/java/com/tobedevoured/modelcitizen/template/BlueprintTemplate.java) use to construct and inject the model. Defaults to [JavaBeanTemplate](https://github.com/mguymon/model-citizen/blob/master/core/src/main/java/com/tobedevoured/modelcitizen/template/JavaBeanTemplate.java)\n\n\n### Field Annotations\n**@Default**: The default value for the field.\n   * _force_:  [boolean] Force the value of the Default to always be set, even if the target field already has a value. Default is false. This is useful for overriding primatives or Collections.\n\n**@Mapped**: The value is mapped to another @Blueprint, default is the blueprint for\n               matching field's Class. Mapped class can be set by the _target_ param.\n\n**@MappedList**: Creates a List of Models mapped to another blueprint.\n  * _size_: [int] Number of Models to be created by the ModelFactory and added to List, defaults to 1.\n  * _target_: [Class] The target Blueprint Class used to create Models\n  * _targetList_: [Class] The List created, defaults to ArrayList\n  * _ignoreEmpty_: [boolean] If true, do not create Models for an empty Set. Defaults to true.\n  * _force_: [boolean] Force the value of the MappedList to always be set, even if the target field already has a value. Default is false.\n\n**@MappedSet**: Creates a Set of Models mapped to another blueprint.\n  * _size_: [int] Number of Models to be created by the ModelFactory and added to Set, defaults to 1.\n  * _target_: [Class] The target Blueprint Class used to create Models\n  * _targetSet_: [Class] The Set created, defaults to HashSet\n  * _ignoreEmpty_: [boolean] If true, do not create Models for an empty Set. Defaults to true.\n  * _force_:  [boolean] Force the value of the MappedSet to always be set, even if the target field already has a value. Default is false.\n\n**@Nullable**: Specifies this field can be null and not to set a value by the ModelFactory.\n\n### Callbacks\n\nCallback fields can be used to inject values at the various stages of the model creation lifecycle:\n\n**ConstructorCallback**: Use when constructing a new instance of the model, the model return by the callback is used by the ModelFactory. ([example blueprint](https://github.com/mguymon/model-citizen/blob/master/core/src/test/java/com/tobedevoured/modelcitizen/blueprint/SpareTireBlueprint.java)).\n\n**FieldCallback**: Injects the return of the callback for the annotated field value ([example blueprint](https://github.com/mguymon/model-citizen/blob/master/core/src/test/java/com/tobedevoured/modelcitizen/blueprint/UserBlueprint.java)).\n\n**AfterCreateCallback**: Executed after the model has been constructed and all the fields are set ([example blueprint](https://github.com/mguymon/model-citizen/blob/master/core/src/test/java/com/tobedevoured/modelcitizen/blueprint/SpareTireBlueprint.java)).\n\n### Inheritance\n\nA Blueprint will inherit the fields of its parent, except for `ConstructorCallback`.\n\n## Model\n\nPresently only supports [template for JavaBean Models](https://github.com/mguymon/model-citizen/blob/master/core/src/main/java/com/tobedevoured/modelcitizen/template/JavaBeanTemplate.java).\nFor annotations to work with the template, the model must follow the [JavaBean](http://en.wikibooks.org/wiki/Java_Programming/Java_Beans) getter and setters\nfor fields.\n\n### Working with primitives\n\n[Primitive fields are intialized as zero](http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5) and\nwill never be null. This will cause the _ModelFactory_ to think that a value has already been assigned to the model and\nnot assign one from the blueprint. To work around this, use the `force=true` in the blueprint to force that value is always assigned from the blueprint, for example:\n\n    @Blueprint(Car.class)\n    public class CarBlueprint {\n\n    @Default(force=true)\n    float mileage = 100.1f;\n\n## ModelFactory\n\nCreates new instances of Models based on registered Blueprints. It is possible to\n[register Blueprints by package](https://github.com/mguymon/model-citizen/wiki/Register-By-Package).\n\nA new instance is always constructed, unless specified by a\n[Policy](https://github.com/mguymon/model-citizen/wiki/Policy).\n\nExample of creating a new _ModelFactory_ and registering the _CarBlueprint_\n\n    ModelFactory modelFactory = new ModelFactory();\n    modelFactory.registerBlueprint( CarBlueprint.class );\n\nA Model with a registered Blueprint can then be created by Class:\n\n    modelFactory.createModel(Car.class);\n\nor by passing a Model directly with override values:\n\n    Car car = new Car();\n    car.setMake( \"Truck\" );\n\n    # create a new Model using blueprint defaults, but overriding the Make to be Truck.\n    car = modelFactory.createModel(car);\n\n## A Simple Example\n\n### Creating a model\n\n    ModelFactory modelFactory = new ModelFactory();\n    modelFactory.registerBlueprint( CarBlueprint.class );\n    Car car = modelFactory.createModel(Car.class);\n\n### Creating a model overriding defaults\n\n    Car car = new Car()\n    car.setMake( \"Caddie\" );\n    car = modelFactory.createModel(car);\n\n### Car model's Blueprint\n\n    @Blueprint(Car.class)\n    public class CarBlueprint {\n\n        @Default\n        String make = \"car make\";\n\n        @Default\n        String manufacturer = \"car manufacturer\";\n\n        @Default\n        Integer mileage = 100;\n\n        @Default\n        Map status = new HashMap();\n    }\n\n### The Car Model\n\n    public class Car {\n        private String make;\n        private String manufacturer;\n        private Integer mileage;\n        private Map status;\n\n        public String getMake() {\n            return make;\n        }\n\n        public void setMake(String make) {\n            this.make = make;\n        }\n\n        public String getManufacturer() {\n            return manufacturer;\n        }\n\n        public void setManufacturer(String manufacturer) {\n            this.manufacturer = manufacturer;\n        }\n\n        public Integer getMileage() {\n            return mileage;\n        }\n\n        public void setMileage(Integer mileage) {\n            this.milage = mileage;\n        }\n\n        public Map getStatus() {\n            return status;\n        }\n\n        public void setStatus(Map status) {\n            this.status = status;\n        }\n    }\n\n[Wiki](https://github.com/mguymon/model-citizen/wiki) of examples that\nincludes [Callbacks](https://github.com/mguymon/model-citizen/wiki/Callback-Example), [Policies](https://github.com/mguymon/model-citizen/wiki/Policy), and [Package scanning for Blueprints](https://github.com/mguymon/model-citizen/wiki/Register-By-Package).\n\n## Spring Support\n\nThere is an optional jar that provides additional support for Spring:\n\n\n    \u003cdependency\u003e\n      \u003cgroupId\u003ecom.tobedevoured.modelcitizen\u003c/groupId\u003e\n      \u003cartifactId\u003espring\u003c/artifactId\u003e\n      \u003cversion\u003e0.8.1\u003c/version\u003e\n    \u003c/dependency\u003e\n\n### Avoiding Spring jar collisions\n\nModelFactory should work with Spring 3.x, so you can easily exclude ModelFactory's Spring depedency and use the existing one in your pom.\n\n    \u003cdependency\u003e\n      \u003cgroupId\u003ecom.tobedevoured.modelcitizen\u003c/groupId\u003e\n      \u003cartifactId\u003espring\u003c/artifactId\u003e\n      \u003cversion\u003e0.8.1\u003c/version\u003e\n      \u003cexclusions\u003e\n        \u003cexclusion\u003e\n            \u003cgroupId\u003eorg.springframework\u003c/groupId\u003e\n            \u003cartifactId\u003espring-context\u003c/artifactId\u003e\n        \u003c/exclusion\u003e\n      \u003c/exclusions\u003e\n    \u003c/dependency\u003e\n\n### What does this give me?\n\nThis provides a new class [ModelFactoryBean](https://github.com/mguymon/model-citizen/blob/master/spring/src/main/java/com/tobedevoured/modelcitizen/spring/ModelFactoryBean.java) and annotation [@SpringBlueprint](https://github.com/mguymon/model-citizen/blob/master/spring/src/main/java/com/tobedevoured/modelcitizen/spring/annotation/SpringBlueprint.java). When the ModelFactoryBean is registered with Spring, it allows the `@SpringBlueprint` and models created by the ModelFactoryBean to be injected by Spring.\n\n#### Example ModelFactoryBean\n\n    \u003cbean id=\"modelFactory\" class=\"com.tobedevoured.modelcitizen.spring.ModelFactoryBean\" \u003e\n        \u003cproperty name=\"registerBlueprintsByPackage\" value=\"com.tobedevoured.modelcitizen\" /\u003e\n    \u003c/bean\u003e\n\n#### Example @SpringBlueprint with Spring support\n\n    @Blueprint(SportsCar.class)\n    @SpringBlueprint\n    public class SportsCarBlueprint {\n\n        @Autowired\n        SportsCarRepository sportsCarRepository;\n\n        @Default\n        Integer topSpeed = 100;\n    }\n\n\n## License\n\nLicensed to the Apache Software Foundation (ASF) under one or more\ncontributor license agreements.  See the NOTICE file distributed with this\nwork for additional information regarding copyright ownership.  The ASF\nlicenses this file to you under the Apache License, Version 2.0 (the\n\"License\"); you may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\nWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the\nLicense for the specific language governing permissions and limitations under\nthe License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmguymon%2Fmodel-citizen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmguymon%2Fmodel-citizen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmguymon%2Fmodel-citizen/lists"}