Class ExpectationToGoSerializer

java.lang.Object
org.mockserver.serialization.code.ExpectationToGoSerializer

public class ExpectationToGoSerializer extends Object
Generates copy-paste-ready Go expectation code for the MockServer Go client (github.com/mock-server/mockserver-monorepo/mockserver-client-go).

Following the same JSON-wrap pattern as the JavaScript/Python generators, the generated code embeds each expectation's existing JSON serialization (the same bytes produced by format=JSON) and parses it at runtime with json.Unmarshal into a mockserver.Expectation, then passes it to client.Upsert(...).

Verified against the Go client source:

  • func New(host string, port int, opts ...Option) *Client
  • func (c *Client) Upsert(expectations ...Expectation) ([]Expectation, error)
  • type Expectation struct with json:"..." tags (so json.Unmarshal works)

The JSON is embedded in a Go raw string literal (backticks) which needs no escaping. If the JSON happens to contain a backtick (impossible for Jackson structural output but possible inside a string value), the generator falls back to a double-quoted interpreted string with the minimal Go escapes. Example output for a single expectation:

 package main

 import (
     "encoding/json"

     mockserver "github.com/mock-server/mockserver-monorepo/mockserver-client-go"
 )

 func main() {
     client := mockserver.New("localhost", 1080)

     var e mockserver.Expectation
     _ = json.Unmarshal([]byte(`{ ... }`), &e)
     client.Upsert(e)
 }
 
Author:
jamesdbloom
  • Constructor Details

    • ExpectationToGoSerializer

      public ExpectationToGoSerializer(ExpectationSerializer expectationSerializer)
  • Method Details