Code Monkey home page Code Monkey logo

Comments (9)

translatenix avatar translatenix commented on May 24, 2024 2

I guess you want something like the following:

mappings.pkl:

tables: Mapping<String, DbTable>

typealias DbTable = Mapping<String, DbMapping>

class DbMapping {
  gp_table: String
  gp_field: String
  bc_table: String
  bc_field: String
}
amends "mappings.pkl"

tables {
  ["salesInvoices"] {
    ["documentNo_to_invoiceNumber"] {
      gp_table = "RM20101"
      gp_field = "DOCNUMBR"
      bc_table = "salesInvoices"
      bc_field = "externalDocumentNumber"
    }
  }
}

PS: This is a better fit for "Discussions" than "Issues".

from pkl.

translatenix avatar translatenix commented on May 24, 2024 1

The type alias only improves readability and reusability.
Otherwise it's the same as tables: Mapping<String, Mapping<String, DbMapping>>.
A Pkl Mapping is often called "map" or "dictionary" in other programming languages.

from pkl.

yesteryearer avatar yesteryearer commented on May 24, 2024

Even if there was a solution as the original poster said here, but with an undefined number of internal objects of the declared type, that would be wonderful. Not looking for an elegant solution.

from pkl.

holzensp avatar holzensp commented on May 24, 2024

I'm not too sure what

table String {
    mapping String {
            gp_table: String
            gp_field: String
            bc_table: String
            bc_field: String
    }
}

means. If you know the field names ahead of time, you don't want a Mapping, but rather a class that defines properties. Only when you don't know the names ahead of time do you tend to use a Mapping.

Be weary that import "mappings.pkl" doesn't actually do anything (you don't use mappings anywhere); at the moment, Pkl only checks that salesInvoices.documentNo_to_invoiceNumber is indeed a Mapping, but nothing else.

from pkl.

yesteryearer avatar yesteryearer commented on May 24, 2024

Mapping in this case is not as per the Pkl definition, but rather a mapping between two databases. Hence the different tables and fields.

The first code block was supposed to just illustrate the general form of the object I'm trying to create.

Does the second code block not define a class with specific properties?

And finally, the final code doesn't even run, it throws an error saying: annotated types can't be applied to non-local objects or something along those lines.

All I'm trying to do is enforce a specific form on to the objects of type 'table', where any one table can have one or many objects contained within it of the form:

class Mapping{
    gp_table: String
    gp_field: String
    bc_table: String
    bc_field: String
}

Where each gp_table, gp_field, etc, variables will have varying values attributing to them.

from pkl.

yesteryearer avatar yesteryearer commented on May 24, 2024

@translatenix

Thank you so much, this is excellent + exactly what I was looking for.

I understand the code off the bat, but if you are willing to give a better explanation of your thought process, I'm all ears. Does mapping instantiate a many-to-one relationship? The only thing I don't really get is the distinction between using the variable name such as tables and the keyword typealias, but I guess this is due to the fact that there is only a single tables object and many DbTables.

Also, I wasn't even aware of the "Discussions" section, so thanks again. First time I've made a forum post on GitHub.

from pkl.

StefMa avatar StefMa commented on May 24, 2024

There is also a Map type in Pkl with an richer API 🤓

from pkl.

holzensp avatar holzensp commented on May 24, 2024

Entirely with @translatenix's suggestions (both code and Discussion; but now that it's here, that's alright... do please close this Issue when you're fully satisfied, @yesteryearer).

Since you're talking about database tables and fields, I bet there are restrictions on the names of those things. It depends entirely on which database you're using what those restrictions are, but just for the example, I'm assuming names may not contain whitespace. This is to demonstrate type constraints and to further illustrate @translatenix's point about typealiases:

tables: Mapping<String, DbTable>

typealias DbTable = Mapping<String, DbMapping>

/// The database can't take arbitrary strings as names, so names must be restricted.
typealias DbName = String(!contains(" "))

class DbMapping {
  gp_table: DbName
  gp_field: DbName
  bc_table: DbName
  bc_field: DbName
}

from pkl.

yesteryearer avatar yesteryearer commented on May 24, 2024

So to clarify, the reason I originally posted this here was because of the following error:

–– Pkl Error ––
A non-local object property cannot have a type annotation.

At which point I didn't know a workaround exists.

Thanks for the help.

from pkl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.