Discuss Apple's new schema driven Pkl config format #1944
Closed
kevinschaper
started this conversation in
General
Replies: 2 comments 1 reply
-
sort of like an imperative wrapper to javapoet example: MethodSpec main = MethodSpec.methodBuilder("main")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(void.class)
.addParameter(String[].class, "args")
.addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!")
.build();
TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addMethod(main)
.build();
JavaFile javaFile = JavaFile.builder("com.example.helloworld", helloWorld)
.build();
javaFile.writeTo(System.out); declarative ast: from ast import FunctionDef, Expr, Call, Name, Constant, ClassDef, Module
main = FunctionDef(
name="main",
body=[
Expr(
value=Call(
func=Name(id="print"),
args=[Constant(value="Hello, world!")],
)
)
],
decorator_list=[Name(id="staticmethod")],
)
myclass = ClassDef(
name="helloWorld",
body=[main],
)
mymodule = Module(body=[myclass], type_ignores=[]) so then something like class Builder():
def __init__(self, **kwargs):
self.fields = {}
self.fields.update(kwargs)
def __class_getitem__(cls, name: str):
subcls = type(name, (cls,), {"type": getattr(ast, f"{name}Def")})
return subcls
def __getattr__(self, val:str):
field = val.split('_')
if field[0] == 'add':
def adder(x):
if field[1] not in self.fields:
self.fields[field[1]] = [x]
else:
self.fields[field[1]].append(x)
return self
return adder
else:
raise ValueError(f'{val} {field} this is metaprogramming u expected it to make sense')
def build(self):
return self.type(**self.fields) for FunctionBuilder = Builder["Function"]
myfunc = FunctionBuilder(name="myfunction").add_body(ast.Pass())
myfunc.build() or whatevs |
Beta Was this translation helpful? Give feedback.
1 reply
-
I'm closing as we have a separate enhancement request for gen-pkl, but feel free to open a new discussion or reopen for discussion of more general leveraging of other libraries for codegen |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Apple has a new config format that looks to be schema driven (defined as classes), with generators for json, yaml and code generators for Java, Kotlin and GO. Let's discuss the impact this might have on LinkML.
Should we make a gen-pkl?
The Java and Kotlin generators don't use templates, but instead use javapoet and kotlinpoet code generation libraries. Does anything like this exist for other languages we're supporting or in the python ecosystem?
Beta Was this translation helpful? Give feedback.
All reactions