server

package
v0.0.0-...-18f9ab3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2023 License: MIT Imports: 3 Imported by: 0

README

Exercise 4: Using gRPC to validate data contract

In this exercise we will be creating and validating Data contracts. We will be using the protobuf + grpc infrastructure to add a first layer of validation. This is a great way to restrict all payloads into a predictable and structured format.

Note: This can also be accomplished using JSON schema. gRPC is more popular amoung go developers, but there is no conceptual advatange in using gRPC over JSON schema.

We are using the same data structures as in the previous streaming examples:

type Message struct {
	ID        int
	Type      string
	Body      UserAction
	Timestamp time.Time
}

// action types: login, logout, purchase, return, add to cart, remove from cart, view item, view cart, view purchase history
type UserAction struct {
	UserID    int
	Action    string
	Succeded  bool
	Error     string
	Timestamp time.Time
}

To complete this exercise, add the required logic to the functions found in solution.go to make the test pass. The tests are already implemented in server_test.go. The tests will start as all failing. You can check your test output by running

$ go test /ex-4-data-contracts -v

Solution

Follow-up Questions:

  • What fields are required according to our current data contract?
  • Should any additional fields be required?

Directories

Path Synopsis
gen

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL