Giter VIP home page Giter VIP logo

Comments (6)

mpvl avatar mpvl commented on September 23, 2024

The parser considers comments as first-class citizens, exactly for the purpose you describe, and they are accessible through the ast API.

This is a bit low level, so maybe you referring to the being able to access doc comments in the higher level API as well?

The only tricky bit is what to do if multiple fields are documented. Go does the same thing for package documentation, and we could probably copy that strategy.

The trickier part is individual fields. In CUE, a field can be defined many times, each time with different documentation. So the question is which one to pick. My best guess is to provide a slice with all unique comment groups, starting with the most high-level declarations (imported, then merged from parent dirs, then templates over individual fields).

Let me know if the ast approach suffices, or whether having doc comments accessible through cuelang.org/go/cue.

from cue.

pavlos-christoforou avatar pavlos-christoforou commented on September 23, 2024

from cue.

mpvl avatar mpvl commented on September 23, 2024

Early on, the first two large potential customers indicated they wanted it to use for code generation of exactly the ilk you describe here. So this is certainly a main focus. In fact, the recent addition of attributes to the language was to accommodate this use case. It allows adding meta information to influence generators for particular languages.

Anyway, it is good to learn of more use cases. And yes, exposing docs in the high-level API was on my todo list.

I think for individual field docs a possible approach would be to follow
the same logic cue follows for multiple values for consistency.

In the example you gave, there won't be much ambiguity and I think my described approach works well (the first comment will be pretty much always the one you need). But I'm open to other suggestions. I'm not sure if you were suggesting something different here.

What we find very useful is that we use such definitions to auto generate
csv parsers, SQL schemas with field docs, example data sets in csv with
each field documented and domains./constrains listed as well as
autogenerated validation functions for the most basic checks (mandary
fields, type compatibility, domain restrictions).

Yes, that is the idea. There are currently some CLs going in mostly the opposite direction (e.g. generating CUE templates from Kubernetes Go code, which treats Go as the source of truth), but code generation in the opposite direction is certainly part of the plan. The idea is that it would not only allow data types but also detailed validation to be shared across languages.

So in CUE, the above first few fields would look something like:

// which legal entities and currency pairs to include in the calculation.
LegalEntityFormat: Spec & {
    version: 1

    // The fund legal entity to include in the calculation
    FundLegalEntity: string

    // currency aliases a regexp constraint of a currency type.
    // TODO: define in other package.
    currency = =~ "^[A-Z0-9]{3}$"

    // The target currency for the calculation. For feeder type
    // funds this is the share class currency. For master or standalone type funds
    // this is usually the non base currency involved in the calculation
    CalculationCurrency: currency  @domain("CURRENCY_DOMAIN_SET")

    // The risk currency the particular share class, master fund or
    // standalone fund is exposed to
    RiskCurrency: currency  @domain("CURRENCY_DOMAIN_SET")

    // The trade currency on which we will peg the hedging trade
    TradeCurrency: currency  @domain("CURRENCY_DOMAIN_SET")

    // The settlement account associated with the fund base currency
    FundBaseCurrencySettlementAccount?: string

    ...
}

Note the question mark to distinguish between optional and mandatory fields. I didn't know what domain meant, so I added an attribute as meta info. For giggles, I also restricted the currency types to be a three-letter ISO currency code.

from cue.

pavlos-christoforou avatar pavlos-christoforou commented on September 23, 2024

from cue.

pavlos-christoforou avatar pavlos-christoforou commented on September 23, 2024

from cue.

cueckoo avatar cueckoo commented on September 23, 2024

This issue has been migrated to cue-lang/cue#36.

For more details about CUE's migration to a new home, please see cue-lang/cue#1078.

from cue.

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.