Skip to content

Instantly share code, notes, and snippets.

@upangka
Created December 27, 2019 07:15
Show Gist options
  • Save upangka/d78512d269adb26a01dbd39b292b9389 to your computer and use it in GitHub Desktop.
Save upangka/d78512d269adb26a01dbd39b292b9389 to your computer and use it in GitHub Desktop.
builder生成脚本
import os
print("=======================Generate Record Builder========================================")
print(" Author D艺: 黄壮壮 ")
print("=======================Generate Record Builder========================================")
AttributesEp = '''
Integer id, Integer userId, Integer oldCardId, Integer newCardId, String oldGrade, String newGrade, String oldCardName, String newCardName, String gradeCondition, String operate, Timestamp createTime, Timestamp updateTime
\n
'''
Attributes = input('please input attributes.(Such as below infomation): \n'+AttributesEp)
data = Attributes.strip('\n').split(',')
resultData = []
for attribute in data:
resultData.append(attribute.strip(' ').split(' '))
recordName = input('Please input record name. (Such as CardUpgradeRecord): \n').strip(' ')
classHeader = '''
public class {recordName}Builder {{
private {recordName} record;
private {recordName}Builder(){{
record = new {recordName}();
}}
private {recordName}Builder({recordName} record) {{
this.record = record;
}}
public static {recordName}Builder create() {{
return new {recordName}Builder();
}}
public static {recordName}Builder create({recordName} record) {{
return new {recordName}Builder(record);
}}
'''
methodsTemplate = '''
public {recordName}Builder {attribute} ({pType} {attribute}) {{
record.set{Attribute}({attribute});
return this;
}}
'''
classEnd = '''
public {recordName} build() {{
return record;
}}
}}
'''
resultArgs = []
for attribute in resultData:
title = attribute[1][0].capitalize()+attribute[1][1:]
print(title)
argsT = {
'recordName': recordName,
'attribute': attribute[1],
'pType': attribute[0],
'Attribute': title
}
print(argsT)
resultArgs.append(argsT)
# write head
fileName = recordName+'Builder.java'
with open(fileName,'w') as f:
f.write(classHeader.format(recordName=recordName))
# write methods
for arg in resultArgs:
with open(fileName,'a') as f:
f.write(methodsTemplate.format(**arg))
# write end
with open(fileName,'a') as f:
f.write(classEnd.format(recordName=recordName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment