apigear-io / cli

The ApiGear client as go project
https://apigear.io
MIT License
1 stars 2 forks source link

imports #132

Open jryannel opened 6 months ago

jryannel commented 6 months ago

Allow importing other modules and have a way to reference data types from other modules.

module a 1.0

import b 1.0

interface A {
  v : b.T
}
jryannel commented 5 months ago

Now you can use an import e.g.

name: demo.bbb
version: "1.0"

import: demo.aaa

interfaces:
  - name: Counter
    properties:
      - name: value
        type: Value
        import: demo.aaa

And IDL:

module demo.bbb 1.0

import demo.aaa

interface Counter {
    value: demo.aaa.Value
}

This will allow to use imported types. The YAML side requires a split type (e.g. import: demo.aa and type: Value) and the IDL allows to have a unified type (demo.aaa.Value). If the import on the module is not stated, a warning will be logged.

In the filter, we check for import:

if schema.IsImported() {
    prefix = fmt.Sprintf("%s.", schema.ShortImportName())
}

And in the template we can add the module import:

{{ range .Module.Imports}}
import "{{$.System.Meta.go.module}}/{{path .Name}}"
{{- end }}

As go requires a root module we had to add it to the meta information in the solution doc:

schema: apigear.solution/1.0
targets:
  - name: demo
    imports: [meta.yaml]
    meta: # go module
      go:
        module: rpc
    inputs:
    - api/a.idl
    - api/b.idl
    output: out
    template: templ