Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate document source #45

Open
anti-social opened this issue Nov 9, 2021 · 1 comment
Open

Generate document source #45

anti-social opened this issue Nov 9, 2021 · 1 comment

Comments

@anti-social
Copy link
Owner

Research a way how we could generate a document source from a document.

An example how it may look like:

@DocSource
object UserDoc : Document() {
    @Required
    val id by int()
    @RequiredListOfRequiredValues
    val groups by int()
    @Required
    val login by keyword()
    val about by text()
}

Should generate document source that matches:

class UserDocSource : DocSource() {
    val id: Int by UserDoc.id.required()
    val groups: List<Int> by UserDoc.groups.required().list().required()
    val login: String by UserDoc.login.required()
    val about: String? by UserDoc.about()
}

Also we could generate more effective fersion. See an example:

Links:

@anti-social
Copy link
Owner Author

anti-social commented Apr 24, 2023

Possibly it is worth to generate data class for a document source. Generated code could look like:

data class GroupDocSource(
    val id: Int,
    val name: String,
) {
    constructor(src: Deserializer.ObjectCtx) :
        this(
            // TODO: What if there are multiple GroupDoc fields in a parent document?
            id = UserDoc.group.id.getFieldType().deserialize(src.any("id")),
            name = UserDoc.group.name.getFieldType().deserialize(src.any("name")),
        )

    override fun toSource(ctx: Serializer.ObjectCtx) {
        ctx.field("id", id)
        ctx.field("name", name)
    }
}

data class UserDocSource(
    val id: Int,
    val groups: List<GroupDocSource>,
    val login: String,
    val about: String?,
) : ToSource {
    companion object {
        private val groupsFieldType = RequiredListType(UserDoc.groups.getFieldType())
    }
    
    constructor(src: Deserializer.ObjectCtx) :
        this(
            id = UserDoc.id.getFieldType().deserialize(src.any("id")),
            groups = groupsFieldType.deserialize(src.any("groups")),
            login = UserDoc.login.getFieldType().deserialize(src.any("login")),
            about = src.anyOrNull("about")?.let(UserDoc.about.getFieldType()::deserialize),
        )

    override fun toSource(ctx: Serializer.ObjectCtx) {
        ctx.field("id", id)
        ctx.array("groups") {
            for (groupsItem in groups) {
                obj {
                    groupsItem.toSource(this)
                }
            }
        }
        ctx.field("login", login)
        ctx.field("about", about)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant