Skip to content

Commit 2ddf9c7

Browse files
committed
fix desc and readme
1 parent 9c2fbb9 commit 2ddf9c7

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,53 @@ func main() {
108108
}
109109
```
110110

111+
## 基础介绍
112+
113+
本工具用于对象的构建,可以指定相同名字字段的copy,
114+
115+
也可以使用其他对象的成员变量计算生成,tag内文本语法为javascrip表达式。
116+
117+
---
118+
119+
* 基类 structor.BaseStructor
120+
121+
基类的 structor Tag 为逗号分割的字符串,用于指定构建对象需要用到的对象名字;
122+
123+
成员字段计算Tag中可以直接使用这些对象的名字来引用对象;
124+
125+
126+
内置功能字符串为:
127+
128+
|内置功能字符串 | 功能描述 |
129+
| --- | --- |
130+
| CopyByName | copy 相同名字的字段到目标对象上 |
131+
132+
demo 中 Farmer 的基类Tag 含义为:
133+
134+
从 Human 和 address 两个对象来构建自己,并将有相同名字的字段 copy过来。
135+
136+
---
137+
138+
* 成员字段计算Tag
139+
140+
tag内文本语法为javascrip表达式,可以使用的变量为基类中指定的对象;
141+
142+
另外 self变量指向本对象,可以使用self来引用CopyByName的字段,因为Copy逻辑在计算逻辑之前执行。
143+
144+
---
145+
146+
* Structor 对象 Set(name string, value interface{})
147+
148+
添加构建目标对象的资源, 需要指定名字,名字要与BaseStructor Tag 中的名字相同
149+
150+
---
151+
152+
* Structor Construct(target interface{})
153+
154+
构建目标方法,调用后立即执行,输入参数为需要构建的对象的地址
155+
156+
---
157+
111158
## Contributing
112159

113160
You can help to make the project better, check out [http://gorm.io/contribute.html](http://gorm.io/contribute.html) for things you can do.

structor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ type Structor struct {
1919
components map[string]interface{}
2020
}
2121

22+
// new Struct
2223
func New() *Structor {
2324
t := &Structor{}
2425
t.components = make(map[string]interface{})
2526
return t
2627
}
2728

29+
// Prepare named struct parameter for Structor
2830
func (s *Structor) Set(name string, value interface{}) *Structor {
2931
s.components[name] = value
3032
return s
@@ -73,6 +75,7 @@ func execute(langis *otto.Otto, script string) (interface{}, error) {
7375
return r, nil
7476
}
7577

78+
// construct and fill the target object
7679
func (s *Structor) Construct(target interface{}) error {
7780
base := s.getStructorBase(target)
7881
if base == nil {

0 commit comments

Comments
 (0)