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

value keyword idea / proposal #122

Open
pkieltyka opened this issue Oct 27, 2022 · 2 comments
Open

value keyword idea / proposal #122

pkieltyka opened this issue Oct 27, 2022 · 2 comments

Comments

@pkieltyka
Copy link
Member

It's convenient to use webrpc as a schema source of truth for types, but sometimes it's nice to do use it for simple constants / configs / values across clients.

I'm proposing a new "value" type,

RIDL

# we can just define some json here
value NetworkNames: map<string,Chain> = '''{
  "blah": { etc }
}'''

value AuthorName: string = "Peter"

JSON

  "values": [
    {
      "name": "AuthorName",
      "type": "string",
      "value": "Peter"
    },
    {
      "name": "AuthorCredits",
      "type": "map<string,User>",
      "value": {
        "joe": {
          "id": 1,
          "username": "joe",
          "role": "friend",
          "kind": "USER"
        }
      }
    }
  ]
@VojtechVitek
Copy link
Contributor

VojtechVitek commented Nov 8, 2022

OpenAPI has examples - values that can be useful for documentation or for creating stub servers.

https://swagger.io/docs/specification/adding-examples/

I wonder if we could find an overlap between these two - example values vs. something that would make it to the generated code as constants too.

@VojtechVitek
Copy link
Contributor

See potential implementation from Peter's branch:

https://github.com/webrpc/webrpc/blob/1bbee6ae9b44fb74cdd31c9d8bd596715c0e6e8b/schema/_value.go

package schema

import (
	"fmt"
	"strings"
)

// in the future.. see: https://github.com/webrpc/webrpc/issues/122

type Value struct {
	Name  string      `json:"name"`
	Type  string      `json:"type"`
	Value interface{} `json:"value"`

	// Schema *WebRPCSchema `json:"-"` // denormalize/back-reference
}

func (s *Value) Parse(schema *WebRPCSchema) error {
	if s.Name == "" {
		return fmt.Errorf("schema error: name cannot be empty")
	}
	if s.Value == nil {
		return fmt.Errorf("schema error: value cannot be empty")
	}

	// check for duplicate names
	nameList := map[string]struct{}{}
	for _, sval := range schema.Values {
		nName := strings.ToLower(sval.Name)
		if _, ok := nameList[nName]; ok {
			return fmt.Errorf("schema error: detected duplicate value name of '%s'", sval.Name)
		}
		nameList[nName] = struct{}{}
	}

	return nil
}

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

2 participants