Last active
August 1, 2017 09:51
-
-
Save adriansr/5082a396cb248c136743a9867b4573a2 to your computer and use it in GitHub Desktop.
REST resource code gen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
# Writes the body of a REST resource | |
# input is a list of <type> <name> variables, one per line: | |
# i.e: | |
# String nwDstAddress | |
# int nwDstLength | |
# UUID[] inPorts | |
# [...] | |
# helloWorld => HELLO_WORLD | |
def as_constant(name): | |
out = [] | |
prevWasLower = False | |
for c in name: | |
if c.isupper(): | |
if prevWasLower: | |
out.append('_') | |
out.append(c) | |
prevWasLower = False | |
else: | |
prevWasLower = True | |
out.append(c.upper()) | |
return ''.join(out) | |
fields = [] | |
for line in sys.stdin: | |
lst = line.split() | |
if len(lst)>0: | |
fields.append( lst + [ as_constant(lst[1]) ] ) | |
# Json field names | |
for field in fields: | |
print 'private static final String {} = "{}";'.format(field[2], | |
field[1]) | |
print '' | |
# private fields | |
for field in fields: | |
print 'private final {} {};'.format(field[0], field[1]) | |
print '' | |
# constructor | |
args = ',\n'.join([ '@JsonProperty({}) {} {}'.format(x[2],x[0],x[1]) | |
for x in fields ]) | |
print 'XXXConstructor({}) {{'.format(args) | |
# constructor body | |
for field in fields: | |
print ' this.{} = {};'.format(field[1], field[1]) | |
print '}' | |
print '' | |
# getters | |
for field in fields: | |
print '' | |
print '@JsonProperty({})'.format(field[2]) | |
print 'public {} {}() {{'.format(field[0], field[1]) | |
print ' return {};'.format(field[1]) | |
print '}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment