Match string to object property












0















I have a set of policies that I want to match a request. If the policy exists, I want to match the request and see if the value matches.



The policies are List<Policies> -> (key: String, value: String) and the request can contain different keys.



Example:



The policies are a set of rules that the request should match.



class Policy {
val key: String,
val value: String
}


The request is a data class that contains different values (all optional), for example surname, firstName, address, ++++



data class Request (
id: Long = 12,
firstName: String = "test",
surname: String = "test",
address: String = "somewhere"
...// more fields
)


The list of policies can look like this (List):



List<Policy> => [
{
key: "surname",
value: "test"
},
{
key: "firstName",
value: "test"
}
]


I don't know how I can match the policies with the request. The Policy.key is a String and the Request can contain all different variations of properties.



How do I match the List of policies with my data class Request?










share|improve this question




















  • 1





    Why don't you just translate your pseudo-code to code? We have no idea of what your classes look like, so it's hard to produce something other than basically the pseudo-code that you already have.

    – JB Nizet
    Nov 26 '18 at 11:18











  • Because I have no clue on how to implement this. I don't know how to match Policy.key (string) to Request.property.

    – Mr.Turtle
    Nov 26 '18 at 11:27











  • We don't know either because you didn't post your classes.

    – JB Nizet
    Nov 26 '18 at 11:29











  • @JBNizet Added my two classes. Still have no clue on how to match it though.

    – Mr.Turtle
    Nov 26 '18 at 11:39











  • OK. As is, you would need reflection to do what you want, which is generally a bad idea. The best thing to do would probably be, if possible, to change the Policy class so that, instead of storing a property name, it would store a function which allows getting a property of a Request, i.e. a (Request) -> Any. Or the Request class should be a Map with keys and values. Where do these requests and policies come from? How are they created?

    – JB Nizet
    Nov 26 '18 at 11:49


















0















I have a set of policies that I want to match a request. If the policy exists, I want to match the request and see if the value matches.



The policies are List<Policies> -> (key: String, value: String) and the request can contain different keys.



Example:



The policies are a set of rules that the request should match.



class Policy {
val key: String,
val value: String
}


The request is a data class that contains different values (all optional), for example surname, firstName, address, ++++



data class Request (
id: Long = 12,
firstName: String = "test",
surname: String = "test",
address: String = "somewhere"
...// more fields
)


The list of policies can look like this (List):



List<Policy> => [
{
key: "surname",
value: "test"
},
{
key: "firstName",
value: "test"
}
]


I don't know how I can match the policies with the request. The Policy.key is a String and the Request can contain all different variations of properties.



How do I match the List of policies with my data class Request?










share|improve this question




















  • 1





    Why don't you just translate your pseudo-code to code? We have no idea of what your classes look like, so it's hard to produce something other than basically the pseudo-code that you already have.

    – JB Nizet
    Nov 26 '18 at 11:18











  • Because I have no clue on how to implement this. I don't know how to match Policy.key (string) to Request.property.

    – Mr.Turtle
    Nov 26 '18 at 11:27











  • We don't know either because you didn't post your classes.

    – JB Nizet
    Nov 26 '18 at 11:29











  • @JBNizet Added my two classes. Still have no clue on how to match it though.

    – Mr.Turtle
    Nov 26 '18 at 11:39











  • OK. As is, you would need reflection to do what you want, which is generally a bad idea. The best thing to do would probably be, if possible, to change the Policy class so that, instead of storing a property name, it would store a function which allows getting a property of a Request, i.e. a (Request) -> Any. Or the Request class should be a Map with keys and values. Where do these requests and policies come from? How are they created?

    – JB Nizet
    Nov 26 '18 at 11:49
















0












0








0








I have a set of policies that I want to match a request. If the policy exists, I want to match the request and see if the value matches.



The policies are List<Policies> -> (key: String, value: String) and the request can contain different keys.



Example:



The policies are a set of rules that the request should match.



class Policy {
val key: String,
val value: String
}


The request is a data class that contains different values (all optional), for example surname, firstName, address, ++++



data class Request (
id: Long = 12,
firstName: String = "test",
surname: String = "test",
address: String = "somewhere"
...// more fields
)


The list of policies can look like this (List):



List<Policy> => [
{
key: "surname",
value: "test"
},
{
key: "firstName",
value: "test"
}
]


I don't know how I can match the policies with the request. The Policy.key is a String and the Request can contain all different variations of properties.



How do I match the List of policies with my data class Request?










share|improve this question
















I have a set of policies that I want to match a request. If the policy exists, I want to match the request and see if the value matches.



The policies are List<Policies> -> (key: String, value: String) and the request can contain different keys.



Example:



The policies are a set of rules that the request should match.



class Policy {
val key: String,
val value: String
}


The request is a data class that contains different values (all optional), for example surname, firstName, address, ++++



data class Request (
id: Long = 12,
firstName: String = "test",
surname: String = "test",
address: String = "somewhere"
...// more fields
)


The list of policies can look like this (List):



List<Policy> => [
{
key: "surname",
value: "test"
},
{
key: "firstName",
value: "test"
}
]


I don't know how I can match the policies with the request. The Policy.key is a String and the Request can contain all different variations of properties.



How do I match the List of policies with my data class Request?







kotlin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 11:36







Mr.Turtle

















asked Nov 26 '18 at 11:10









Mr.TurtleMr.Turtle

1,17331329




1,17331329








  • 1





    Why don't you just translate your pseudo-code to code? We have no idea of what your classes look like, so it's hard to produce something other than basically the pseudo-code that you already have.

    – JB Nizet
    Nov 26 '18 at 11:18











  • Because I have no clue on how to implement this. I don't know how to match Policy.key (string) to Request.property.

    – Mr.Turtle
    Nov 26 '18 at 11:27











  • We don't know either because you didn't post your classes.

    – JB Nizet
    Nov 26 '18 at 11:29











  • @JBNizet Added my two classes. Still have no clue on how to match it though.

    – Mr.Turtle
    Nov 26 '18 at 11:39











  • OK. As is, you would need reflection to do what you want, which is generally a bad idea. The best thing to do would probably be, if possible, to change the Policy class so that, instead of storing a property name, it would store a function which allows getting a property of a Request, i.e. a (Request) -> Any. Or the Request class should be a Map with keys and values. Where do these requests and policies come from? How are they created?

    – JB Nizet
    Nov 26 '18 at 11:49
















  • 1





    Why don't you just translate your pseudo-code to code? We have no idea of what your classes look like, so it's hard to produce something other than basically the pseudo-code that you already have.

    – JB Nizet
    Nov 26 '18 at 11:18











  • Because I have no clue on how to implement this. I don't know how to match Policy.key (string) to Request.property.

    – Mr.Turtle
    Nov 26 '18 at 11:27











  • We don't know either because you didn't post your classes.

    – JB Nizet
    Nov 26 '18 at 11:29











  • @JBNizet Added my two classes. Still have no clue on how to match it though.

    – Mr.Turtle
    Nov 26 '18 at 11:39











  • OK. As is, you would need reflection to do what you want, which is generally a bad idea. The best thing to do would probably be, if possible, to change the Policy class so that, instead of storing a property name, it would store a function which allows getting a property of a Request, i.e. a (Request) -> Any. Or the Request class should be a Map with keys and values. Where do these requests and policies come from? How are they created?

    – JB Nizet
    Nov 26 '18 at 11:49










1




1





Why don't you just translate your pseudo-code to code? We have no idea of what your classes look like, so it's hard to produce something other than basically the pseudo-code that you already have.

– JB Nizet
Nov 26 '18 at 11:18





Why don't you just translate your pseudo-code to code? We have no idea of what your classes look like, so it's hard to produce something other than basically the pseudo-code that you already have.

– JB Nizet
Nov 26 '18 at 11:18













Because I have no clue on how to implement this. I don't know how to match Policy.key (string) to Request.property.

– Mr.Turtle
Nov 26 '18 at 11:27





Because I have no clue on how to implement this. I don't know how to match Policy.key (string) to Request.property.

– Mr.Turtle
Nov 26 '18 at 11:27













We don't know either because you didn't post your classes.

– JB Nizet
Nov 26 '18 at 11:29





We don't know either because you didn't post your classes.

– JB Nizet
Nov 26 '18 at 11:29













@JBNizet Added my two classes. Still have no clue on how to match it though.

– Mr.Turtle
Nov 26 '18 at 11:39





@JBNizet Added my two classes. Still have no clue on how to match it though.

– Mr.Turtle
Nov 26 '18 at 11:39













OK. As is, you would need reflection to do what you want, which is generally a bad idea. The best thing to do would probably be, if possible, to change the Policy class so that, instead of storing a property name, it would store a function which allows getting a property of a Request, i.e. a (Request) -> Any. Or the Request class should be a Map with keys and values. Where do these requests and policies come from? How are they created?

– JB Nizet
Nov 26 '18 at 11:49







OK. As is, you would need reflection to do what you want, which is generally a bad idea. The best thing to do would probably be, if possible, to change the Policy class so that, instead of storing a property name, it would store a function which allows getting a property of a Request, i.e. a (Request) -> Any. Or the Request class should be a Map with keys and values. Where do these requests and policies come from? How are they created?

– JB Nizet
Nov 26 '18 at 11:49














1 Answer
1






active

oldest

votes


















1














For your puroposes you need use reflection (you want to find field by name and get value), or change something in your model.



Solution with reflection can be like following:



data class Policy(
val key: String,
val value: String?
)

data class Request(
val id: Int,
val firstName: String? = null,
val surname: String? = null,
val address: String? = null
)

class PolicyException : Exception()

fun checkPolicies(request: Request, policies: List<Policy>) {
policies.forEach { policy ->
val member = request::class.members.find { member -> member.name == policy.key }
val requestMemberValue = member?.call(request)

if (requestMemberValue != policy.value) throw PolicyException()
}
}

fun main(args: Array<String>) {
println("Hello, reflection!")
checkPolicies(Request(id = 0, firstName = "Johnn"), listOf(Policy("firstName", "John")))
}


Also, I changed your policy model to handle nullable values (and still handling properly "null" as string).



But, with this solution you have to be very careful with changing model names. And remeber to do not obfuscate your models.



Also, quite better soltuion is adding Annotation which keeps policy name as annotation property (then problem with changing field name in app will disappear).






share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479862%2fmatch-string-to-object-property%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    For your puroposes you need use reflection (you want to find field by name and get value), or change something in your model.



    Solution with reflection can be like following:



    data class Policy(
    val key: String,
    val value: String?
    )

    data class Request(
    val id: Int,
    val firstName: String? = null,
    val surname: String? = null,
    val address: String? = null
    )

    class PolicyException : Exception()

    fun checkPolicies(request: Request, policies: List<Policy>) {
    policies.forEach { policy ->
    val member = request::class.members.find { member -> member.name == policy.key }
    val requestMemberValue = member?.call(request)

    if (requestMemberValue != policy.value) throw PolicyException()
    }
    }

    fun main(args: Array<String>) {
    println("Hello, reflection!")
    checkPolicies(Request(id = 0, firstName = "Johnn"), listOf(Policy("firstName", "John")))
    }


    Also, I changed your policy model to handle nullable values (and still handling properly "null" as string).



    But, with this solution you have to be very careful with changing model names. And remeber to do not obfuscate your models.



    Also, quite better soltuion is adding Annotation which keeps policy name as annotation property (then problem with changing field name in app will disappear).






    share|improve this answer






























      1














      For your puroposes you need use reflection (you want to find field by name and get value), or change something in your model.



      Solution with reflection can be like following:



      data class Policy(
      val key: String,
      val value: String?
      )

      data class Request(
      val id: Int,
      val firstName: String? = null,
      val surname: String? = null,
      val address: String? = null
      )

      class PolicyException : Exception()

      fun checkPolicies(request: Request, policies: List<Policy>) {
      policies.forEach { policy ->
      val member = request::class.members.find { member -> member.name == policy.key }
      val requestMemberValue = member?.call(request)

      if (requestMemberValue != policy.value) throw PolicyException()
      }
      }

      fun main(args: Array<String>) {
      println("Hello, reflection!")
      checkPolicies(Request(id = 0, firstName = "Johnn"), listOf(Policy("firstName", "John")))
      }


      Also, I changed your policy model to handle nullable values (and still handling properly "null" as string).



      But, with this solution you have to be very careful with changing model names. And remeber to do not obfuscate your models.



      Also, quite better soltuion is adding Annotation which keeps policy name as annotation property (then problem with changing field name in app will disappear).






      share|improve this answer




























        1












        1








        1







        For your puroposes you need use reflection (you want to find field by name and get value), or change something in your model.



        Solution with reflection can be like following:



        data class Policy(
        val key: String,
        val value: String?
        )

        data class Request(
        val id: Int,
        val firstName: String? = null,
        val surname: String? = null,
        val address: String? = null
        )

        class PolicyException : Exception()

        fun checkPolicies(request: Request, policies: List<Policy>) {
        policies.forEach { policy ->
        val member = request::class.members.find { member -> member.name == policy.key }
        val requestMemberValue = member?.call(request)

        if (requestMemberValue != policy.value) throw PolicyException()
        }
        }

        fun main(args: Array<String>) {
        println("Hello, reflection!")
        checkPolicies(Request(id = 0, firstName = "Johnn"), listOf(Policy("firstName", "John")))
        }


        Also, I changed your policy model to handle nullable values (and still handling properly "null" as string).



        But, with this solution you have to be very careful with changing model names. And remeber to do not obfuscate your models.



        Also, quite better soltuion is adding Annotation which keeps policy name as annotation property (then problem with changing field name in app will disappear).






        share|improve this answer















        For your puroposes you need use reflection (you want to find field by name and get value), or change something in your model.



        Solution with reflection can be like following:



        data class Policy(
        val key: String,
        val value: String?
        )

        data class Request(
        val id: Int,
        val firstName: String? = null,
        val surname: String? = null,
        val address: String? = null
        )

        class PolicyException : Exception()

        fun checkPolicies(request: Request, policies: List<Policy>) {
        policies.forEach { policy ->
        val member = request::class.members.find { member -> member.name == policy.key }
        val requestMemberValue = member?.call(request)

        if (requestMemberValue != policy.value) throw PolicyException()
        }
        }

        fun main(args: Array<String>) {
        println("Hello, reflection!")
        checkPolicies(Request(id = 0, firstName = "Johnn"), listOf(Policy("firstName", "John")))
        }


        Also, I changed your policy model to handle nullable values (and still handling properly "null" as string).



        But, with this solution you have to be very careful with changing model names. And remeber to do not obfuscate your models.



        Also, quite better soltuion is adding Annotation which keeps policy name as annotation property (then problem with changing field name in app will disappear).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 12:33

























        answered Nov 26 '18 at 12:10









        CililingCililing

        776215




        776215
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479862%2fmatch-string-to-object-property%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen