Skip to content

Commit

Permalink
message/catalog: separate concerns of firstInSequence
Browse files Browse the repository at this point in the history
The code really should be in catmsg.
Expose the functionality there and remove from
pacakge catalog. Use type aliases to make this
work in a more convenient way.

Change-Id: I250a5ae193e890ec0e20e8b04f3bd26c06448c7b
Reviewed-on: https://go-review.googlesource.com/80595
Run-TryBot: Marcel van Lohuizen <[email protected]>
Reviewed-by: Nigel Tao <[email protected]>
  • Loading branch information
mpvl committed Nov 30, 2017
1 parent 5ff271f commit ded9dd9
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 32 deletions.
24 changes: 17 additions & 7 deletions internal/catmsg/catmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@ import (
// A Handle refers to a registered message type.
type Handle int

// First is used as a Handle to EncodeMessageType, followed by a series of calls
// to EncodeMessage, to implement selecting the first matching Message.
//
// TODO: this can be removed once we either can use type aliases or if the
// internals of this package are merged with the catalog package.
var First Handle = msgFirst

// A Handler decodes and evaluates data compiled by a Message and sends the
// result to the Decoder. The output may depend on the value of the substitution
// arguments, accessible by the Decoder's Arg method. The Handler returns false
Expand Down Expand Up @@ -236,6 +229,23 @@ func Compile(tag language.Tag, macros Dictionary, m Message) (data string, err e
return string(buf), err
}

// FirstOf is a message type that prints the first message in the sequence that
// resolves to a match for the given substitution arguments.
type FirstOf []Message

// Compile implements Message.
func (s FirstOf) Compile(e *Encoder) error {
e.EncodeMessageType(msgFirst)
err := ErrIncomplete
for i, m := range s {
if err == nil {
return fmt.Errorf("catalog: message argument %d is complete and blocks subsequent messages", i-1)
}
err = e.EncodeMessage(m)
}
return err
}

// Var defines a message that can be substituted for a placeholder of the same
// name. If an expression does not result in a string after evaluation, Name is
// used as the substitution. For example:
Expand Down
2 changes: 1 addition & 1 deletion internal/catmsg/catmsg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ type seq []Message

func (s seq) Compile(e *Encoder) (err error) {
err = ErrIncomplete
e.EncodeMessageType(First)
e.EncodeMessageType(msgFirst)
for _, m := range s {
// Pass only the last error, but allow erroneous or complete messages
// here to allow testing different scenarios.
Expand Down
2 changes: 1 addition & 1 deletion internal/catmsg/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (e *Encoder) addVar(key string, m Message) error {
case err == ErrIncomplete:
if Handle(e.buf[0]) != msgFirst {
seq := &Encoder{root: e.root, parent: e}
seq.EncodeMessageType(First)
seq.EncodeMessageType(msgFirst)
e.flushTo(seq)
e = seq
}
Expand Down
23 changes: 0 additions & 23 deletions message/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ package catalog // import "golang.org/x/text/message/catalog"

import (
"errors"
"fmt"

"golang.org/x/text/internal/catmsg"
"golang.org/x/text/language"
Expand Down Expand Up @@ -223,12 +222,6 @@ func (c *Catalog) SetMacro(tag language.Tag, name string, msg ...Message) error
// ErrNotFound indicates there was no message for the given key.
var ErrNotFound = errors.New("catalog: message not found")

// A Message holds a collection of translations for the same phrase that may
// vary based on the values of substitution arguments.
type Message interface {
catmsg.Message
}

// String specifies a plain message string. It can be used as fallback if no
// other strings match or as a simple standalone message.
//
Expand All @@ -247,22 +240,6 @@ func Var(name string, msg ...Message) Message {
return &catmsg.Var{Name: name, Message: firstInSequence(msg)}
}

// firstInSequence is a message type that prints the first message in the
// sequence that resolves to a match for the given substitution arguments.
type firstInSequence []Message

func (s firstInSequence) Compile(e *catmsg.Encoder) error {
e.EncodeMessageType(catmsg.First)
err := catmsg.ErrIncomplete
for i, m := range s {
if err == nil {
return fmt.Errorf("catalog: message argument %d is complete and blocks subsequent messages", i-1)
}
err = e.EncodeMessage(m)
}
return err
}

// Context returns a Context for formatting messages.
// Only one Message may be formatted per context at any given time.
func (c *Catalog) Context(tag language.Tag, r catmsg.Renderer) *Context {
Expand Down
15 changes: 15 additions & 0 deletions message/catalog/go19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.9

package catalog

import "golang.org/x/text/internal/catmsg"

// A Message holds a collection of translations for the same phrase that may
// vary based on the values of substitution arguments.
type Message = catmsg.Message

type firstInSequence = catmsg.FirstOf
23 changes: 23 additions & 0 deletions message/catalog/gopre19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !go1.9

package catalog

import "golang.org/x/text/internal/catmsg"

// A Message holds a collection of translations for the same phrase that may
// vary based on the values of substitution arguments.
type Message interface {
catmsg.Message
}

func firstInSequence(m []Message) catmsg.Message {
a := []catmsg.Message{}
for _, m := range m {
a = append(a, m)
}
return catmsg.FirstOf(a)
}

0 comments on commit ded9dd9

Please sign in to comment.