Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/eugeis/ee
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinwijayaa committed Nov 15, 2023
2 parents a1450ce + 39ab9e6 commit d1098ae
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ open class DesignGoGenerator(private val models: List<StructureUnitI<*>>,

defineSuperUnitsAsAnonymousProps()

defineConstructorNoProps { constructors().isEmpty() && this !is CommandI<*> && this !is EventI<*> }
defineConstructorNoProps { constructors().isEmpty() && this !is Generic && this !is CommandI<*> && this !is EventI<*>}
}

open fun go(fileNamePrefix: String = ""): GeneratorContexts<StructureUnitI<*>> {
Expand Down
20 changes: 11 additions & 9 deletions ee-lang/src/main/kotlin/ee/lang/LangUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ fun TypeI<*>.propsNoMetaNoValue(): List<AttributeI<*>> = storage.getOrPut(this,
props().filter { !it.isMeta() && it.value() == null }
}

fun TypeI<*>.propsNoMetaNoGenericOrValue(): List<AttributeI<*>> = storage.getOrPut(this, "propsNoMetaNoValue") {
props().filter { !it.isMeta() && (it.type() !is Generic || it.value() != null) }
}

fun TypeI<*>.propsNoMetaNoValueNoId(): List<AttributeI<*>> = storage.getOrPut(this, "propsNoMetaNoValueNoId") {
props().filter { !it.isMeta() && it.value() == null && !it.isKey() }
}
Expand Down Expand Up @@ -554,8 +558,8 @@ fun <T : CompositeI<*>> T.declareAsBaseWithNonImplementedOperation() {
}

fun <T : CompositeI<*>> T.prepareAttributesOfEnums() {
findDownByType(EnumTypeI::class.java).forEach {
it.props().forEach { it.replaceable(false).initByDefaultTypeValue(false) }
findDownByType(EnumTypeI::class.java).forEach { enumTypeI ->
enumTypeI.props().forEach { it.replaceable(false).initByDefaultTypeValue(false) }
}
}

Expand All @@ -565,12 +569,11 @@ fun <B : TypeI<B>> B.superUnit(value: TypeI<*>): B = superUnits(value)
fun <T : TypeI<*>> T.constructorOwnPropsOnly(adapt: ConstructorI<*>.() -> Unit = {}): ConstructorI<*> {
val primary = this is EnumTypeI<*>
storage.reset(this)
val parent = this
return constr {
parent(parent)
parent(this@constructorOwnPropsOnly)
primary(primary).params(*propsNoMeta().toTypedArray())
namespace(this@constructorOwnPropsOnly.namespace())
superUnit(parent.superUnit().primaryOrFirstConstructorOrFull())
superUnit(this@constructorOwnPropsOnly.superUnit().primaryOrFirstConstructorOrFull())
adapt()
}
}
Expand All @@ -596,13 +599,12 @@ fun <T : TypeI<*>> T.constructorNoProps(adapt: ConstructorI<*>.() -> Unit = {}):
p(it).default(true).anonymous(it.isAnonymous())
}
storage.reset(this)
val parent = this
constr {
name("Default")
parent(parent)
parent(this@constructorNoProps)
primary(true).params(*constrProps.toTypedArray())
namespace(parent.namespace())
superUnit(parent.superUnit().primaryOrFirstConstructorOrFull())
namespace(this@constructorNoProps.namespace())
superUnit(this@constructorNoProps.superUnit().primaryOrFirstConstructorOrFull())
adapt()
}
} else Constructor.EMPTY
Expand Down
57 changes: 43 additions & 14 deletions ee-lang/src/main/kotlin/ee/lang/gen/go/GoCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,30 @@ fun AttributeI<*>.toGoInitForConstructorFunc(c: GenerationContext, derived: Stri
fun <T : AttributeI<*>> T.toGoTypeDef(c: GenerationContext, api: String): String =
"${type().toGo(c, api)}${toGoMacrosAfterBody(c, api, api)}"


fun TypeI<*>.toGoGenerics(c: GenerationContext, derived: String,
subTypeGenerics: List<GenericI<*>>? = null): String =
generics().joinWrappedToString(", ", "", "[", "]") { myGen ->
if (subTypeGenerics == null) {
"${myGen.name()}${
myGen.type().isNotEMPTY().then { " ${myGen.type().toGo(c, derived)}" }}"
} else if (subTypeGenerics.find { it.name() == myGen.name() } != null) {
myGen.name()
} else {
myGen.type().toGo(c, derived)
}
}

fun TypeI<*>.toGoGenericNames(c: GenerationContext, derived: String): String =
generics().joinWrappedToString(", ", "", "[", "]") {
it.toGoTypeName(c, derived)
}

fun GenericI<*>.toGoTypeName(c: GenerationContext, derived: String): String = this.name()

fun GenericI<*>.toGoName(c: GenerationContext, derived: String): String =
c.n(this, derived)

fun <T : TypeI<*>> T.toGoDefault(
c: GenerationContext, derived: String, attr: AttributeI<*>, parentConstrName: String = ""
): String {
Expand Down Expand Up @@ -140,27 +164,30 @@ fun <T : TypeI<*>> T.toGoDefault(

fun <T : LogicUnitI<*>> T.toGoCallValueByPropName(
c: GenerationContext, derived: String, api: String,
salt: String, parentConstrName: String = ""
salt: String, parentConstrName: String = "", genericNames: String = ""
): String =
if (isNotEMPTY()) {
val logicUnitName = c.n(this, derived)
"""${logicUnitName}(${
"""${logicUnitName}$genericNames(${
params().nonDefaultAndNonDerived()
.toGoCallValueByPropName(c, api, salt, parentConstrName)
})"""
} else ""

fun List<AttributeI<*>>.toGoCallValueByPropName(
c: GenerationContext, api: String, salt: String, parentConstrName: String = ""
c: GenerationContext, api: String, salt: String, parentConstrName: String = "", genericNames: String = ""
): String =

joinWrappedToString(", ", " ") {
it.toGoValueByPropName(c, api, salt, parentConstrName)
it.toGoValueByPropName(c, api, salt, parentConstrName, genericNames)
}

fun <T : AttributeI<*>> T.toGoTestInstance(
c: GenerationContext, derived: String, salt: String, parentConstrName: String = ""
): String {
if (value() != null) {
return value().toString()
}

val baseType = type().findDerivedOrThis()
return if (baseType is n.List) {
val firstGenericType = type().generics().first().type()
Expand All @@ -169,19 +196,20 @@ fun <T : AttributeI<*>> T.toGoTestInstance(
} else {
val constr = firstGenericType.findByNameOrPrimaryOrFirstConstructorFull(parentConstrName)
val constrName = "${c.n(constr, derived).toPlural()}ByPropNames"
"${constrName}(salt, childrenPropCount)"
"$constrName(salt, childrenPropCount)"
}
} else if (baseType is NativeTypeI<*> || baseType is EnumTypeI<*>) {
toGoValueByPropName(c, derived, salt, parentConstrName)
} else {
val genericNames = type().toGoGenericNames(c, derived)
val constr = type().findByNameOrPrimaryOrFirstConstructorFull(parentConstrName)
val constrName = "${c.n(constr, derived)}ByPropNames"
"${constrName}(salt, childrenPropCount)"
"$constrName$genericNames(salt, childrenPropCount)"
}
}

fun <T : AttributeI<*>> T.toGoValueByPropName(
c: GenerationContext, derived: String, salt: String, parentConstrName: String = ""
c: GenerationContext, derived: String, salt: String, parentConstrName: String = "", genericNames: String = ""
): String {
val baseType = type().findDerivedOrThis()
val ret = when (baseType) {
Expand Down Expand Up @@ -217,7 +245,7 @@ fun <T : AttributeI<*>> T.toGoValueByPropName(
baseType.literals().first().toGoValue(c, derived)
} else if (baseType is TypeI<*>) {
type().findByNameOrPrimaryOrFirstConstructorFull(parentConstrName)
.toGoCallValueByPropName(c, derived, derived, salt, parentConstrName)
.toGoCallValueByPropName(c, derived, derived, salt, parentConstrName, genericNames)
} else {
(this.parent() == n).ifElse("\"\"") { "${c.n(this, derived)}.EMPTY" }
}
Expand Down Expand Up @@ -247,7 +275,8 @@ fun <T : TypeI<*>> T.toGoIfNative(c: GenerationContext, derived: String): String
n.Blob -> "[]byte"
n.Exception, n.Error -> "error"
n.Void -> ""
n.Any -> "interface{}"
n.Any -> "any"
n.Comparable -> "comparable"
n.Url -> c.n(j.net.URL)
n.UUID -> c.n(g.google.uuid.UUID)
n.List -> "[]${generics()[0].toGo(c, derived)}"
Expand All @@ -271,7 +300,7 @@ fun <T : TypeI<*>> T.toGoNilOrEmpty(c: GenerationContext): String {
fun GenericI<*>.toGo(c: GenerationContext, derived: String): String = type().toGo(c, derived)

fun <T : TypeI<*>> T.toGo(c: GenerationContext, derived: String): String =
toGoIfNative(c, derived) ?: "${(isIfc()).not().then("*")}${c.n(this, derived)}"
toGoIfNative(c, derived) ?: "${(isIfc()).not().then("*")}${c.n(this, derived)}${toGoGenericNames(c, derived)}"

fun OperationI<*>.toGoIfc(c: GenerationContext, api: String = LangDerivedKind.API): String {
return """
Expand Down Expand Up @@ -333,14 +362,14 @@ fun <T : ConstructorI<*>> T.toGo(c: GenerationContext, derived: String, api: Str
val type = findParentMust(CompilationUnitI::class.java)
val name = c.n(type, derived)
return if (isNotEMPTY()) """${toGoMacrosBefore(c, derived, api)}
func ${c.n(this, derived)}(${
func ${c.n(this, derived)}${type.toGoGenerics(c, derived)}(${
params().nonDefaultAndNonDerived().joinWrappedToString(
", ",
" "
) {
it.toGoSignature(c, api)
}
}) (ret *$name${isErrorHandling().then(", err error")}) {${toGoMacrosBeforeBody(c, derived, api)}${
}) (ret *$name${type.toGoGenericNames(c, derived)}${isErrorHandling().then(", err error")}) {${toGoMacrosBeforeBody(c, derived, api)}${
macrosBody().isNotEmpty().ifElse({
"""
${toGoMacrosBody(c, derived, api)}"""
Expand All @@ -351,7 +380,7 @@ func ${c.n(this, derived)}(${
${it.toGoInitVariables(c, derived, name())}"""
}
}
ret = &$name{${
ret = &$name${type.toGoGenericNames(c, derived)}{${
paramsForType().joinSurroundIfNotEmptyToString(
",$nL ", "$nL ", ",$nL "
) {
Expand Down
32 changes: 18 additions & 14 deletions ee-lang/src/main/kotlin/ee/lang/gen/go/GoPojos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ fun <T : CompilationUnitI<*>> T.toGoIfc(

val name = c.n(this, derived)
return """${toGoMacrosBefore(c, derived, api)}
type $name interface {${toGoMacrosBeforeBody(c, derived, api)}${
type $name${toGoGenerics(c, derived)} interface {${toGoMacrosBeforeBody(c, derived, api)}${
superUnits().joinSurroundIfNotEmptyToString(nL, nL) {
" ${c.n(it, derived)}"
}
Expand All @@ -241,9 +241,10 @@ fun <T : CompilationUnitI<*>> T.toGoImpl(
): String {

val name = c.n(this, derived)
val nameWithGenericNames = "$name${toGoGenericNames(c, derived)}"
val currentProps = excludePropsWithValue.ifElse({ props().filter { it.value() == null } }, props())
return """${toGoMacrosBefore(c, derived, api)}
type $name struct {${toGoMacrosBeforeBody(c, derived, api)}${
type $name${toGoGenerics(c, derived)} struct {${toGoMacrosBeforeBody(c, derived, api)}${
currentProps.joinSurroundIfNotEmptyToString(
nL,
prefix = nL
Expand All @@ -260,19 +261,19 @@ type $name struct {${toGoMacrosBeforeBody(c, derived, api)}${
}${
currentProps.filter { it.isAccessible().setAndTrue() && !it.isMutable().setAndTrue() }
.joinSurroundIfNotEmptyToString(nL, prefix = nL) {
it.toGoGetMethod(name, c, derived)
it.toGoGetMethod(nameWithGenericNames, c, derived)
}
}${
currentProps.filter { it.type().isOrDerived(n.List) }
.joinSurroundIfNotEmptyToString(nL, prefix = nL) {
"""${it.toGoAddMethod(name, c, derived)}
${it.toGoRemoveMethod(name, c, derived)}
${it.toGoReplaceMethod(name, c, derived)}
${it.toGoFindMethod(name, c, derived)}"""
"""${it.toGoAddMethod(nameWithGenericNames, c, derived)}
${it.toGoRemoveMethod(nameWithGenericNames, c, derived)}
${it.toGoReplaceMethod(nameWithGenericNames, c, derived)}
${it.toGoFindMethod(nameWithGenericNames, c, derived)}"""
}
}${
operations().joinSurroundIfNotEmptyToString(nL, prefix = nL) {
it.toGoImpl(name, c, api)
it.toGoImpl(nameWithGenericNames, c, api)
}
}"""
}
Expand All @@ -291,25 +292,28 @@ private fun <T : CompilationUnitI<*>> T.toGoNewTestInstance(
): String {

val name = c.n(this, derived)
val genericNames = toGoGenericNames(c, derived)
val generics = toGoGenerics(c, derived)
val nameWithGenericNames = "$name$genericNames"
val constrName = "${c.n(constr, derived)}ByPropNames"
val constrNames = "${c.n(constr, derived).toPlural()}ByPropNames"

return """
func $constrNames(salt int, count int) []*$name {
items := make([]*$name, count)
func $constrNames$generics(salt int, count int) []*$nameWithGenericNames {
items := make([]*$nameWithGenericNames, count)
for i := 0; i < count; i++ {
items[i] = $constrName(i + salt, count)
items[i] = $constrName$genericNames(i + salt, count)
}
return items
}
func $constrName(salt int, childrenPropCount int) (ret *$name) {
func $constrName$generics(salt int, childrenPropCount int) (ret *$nameWithGenericNames) {
ret = ${
findByNameOrPrimaryOrFirstConstructorFull(constr.name())
.toGoCallValueByPropName(c, derived, api, "salt", constr.name())
.toGoCallValueByPropName(c, derived, api, "salt", constr.name(), genericNames)
}
${
propsNoMetaNoValue().joinSurroundIfNotEmptyToString("\n ") { prop ->
propsNoMetaNoGenericOrValue().joinSurroundIfNotEmptyToString("\n ") { prop ->
if (!prop.isAnonymous()) {
"ret.${prop.name()
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }} = ${
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ open class LangGoContextFactory(targetAsSingleModule: Boolean) : LangCommonConte
}

override fun buildName(item: ItemI<*>, kind: String): String {
return if (item is ConstructorI) {
val ret = if (item is ConstructorI) {
buildNameForConstructor(item, kind)
} else if (item is OperationI) {
buildNameForOperation(item, kind)
} else {
super.buildName(item, kind)
}
return ret
}

override fun buildNameForConstructor(item: ConstructorI<*>, kind: String) =
Expand Down
1 change: 1 addition & 0 deletions ee-lang/src/main/kotlin/ee/lang/n.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ object n : StructureUnit({ name("native") }) {
//types
val Void = NativeType()
val Any = NativeType()
val Comparable = NativeType()
val Path = NativeType()
val Text = NativeType()
val Blob = NativeType()
Expand Down
3 changes: 2 additions & 1 deletion ee-lang_item/src/main/kotlin/ee/lang/ItemApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ abstract class MultiHolder<I, B : MultiHolderI<I, B>>(private val _type: Class<I
if (child.isInternal() || !this.isInternal()) {
child.parent(this)
} else {
child.parent(findParentNonInternal() ?: this)
val parent = findParentNonInternal() ?: this
child.parent(parent)
}
if (isInitialized() && !child.isInitialized()) {
child.init()
Expand Down

0 comments on commit d1098ae

Please sign in to comment.